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/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/init.h>
22 #include <linux/kallsyms.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/sched.h>
39 #include <linux/stop_machine.h>
40 #include <linux/device.h>
41 #include <linux/string.h>
42 #include <linux/mutex.h>
43 #include <linux/unwind.h>
44 #include <asm/uaccess.h>
45 #include <asm/semaphore.h>
46 #include <asm/cacheflush.h>
47 #include <linux/license.h>
49 extern int module_sysfs_initialized;
54 #define DEBUGP(fmt , a...)
57 #ifndef ARCH_SHF_SMALL
58 #define ARCH_SHF_SMALL 0
61 /* If this is set, the section belongs in the init part of the module */
62 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
64 /* Protects module list */
65 static DEFINE_SPINLOCK(modlist_lock);
67 /* List of modules, protected by module_mutex AND modlist_lock */
68 static DEFINE_MUTEX(module_mutex);
69 static LIST_HEAD(modules);
71 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
73 int register_module_notifier(struct notifier_block * nb)
75 return blocking_notifier_chain_register(&module_notify_list, nb);
77 EXPORT_SYMBOL(register_module_notifier);
79 int unregister_module_notifier(struct notifier_block * nb)
81 return blocking_notifier_chain_unregister(&module_notify_list, nb);
83 EXPORT_SYMBOL(unregister_module_notifier);
85 /* We require a truly strong try_module_get() */
86 static inline int strong_try_module_get(struct module *mod)
88 if (mod && mod->state == MODULE_STATE_COMING)
90 return try_module_get(mod);
93 static inline void add_taint_module(struct module *mod, unsigned flag)
100 * A thread that wants to hold a reference to a module only while it
101 * is running can call this to safely exit. nfsd and lockd use this.
103 void __module_put_and_exit(struct module *mod, long code)
108 EXPORT_SYMBOL(__module_put_and_exit);
110 /* Find a module section: 0 means not found. */
111 static unsigned int find_sec(Elf_Ehdr *hdr,
113 const char *secstrings,
118 for (i = 1; i < hdr->e_shnum; i++)
119 /* Alloc bit cleared means "ignore it." */
120 if ((sechdrs[i].sh_flags & SHF_ALLOC)
121 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
126 /* Provided by the linker */
127 extern const struct kernel_symbol __start___ksymtab[];
128 extern const struct kernel_symbol __stop___ksymtab[];
129 extern const struct kernel_symbol __start___ksymtab_gpl[];
130 extern const struct kernel_symbol __stop___ksymtab_gpl[];
131 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
132 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
133 extern const struct kernel_symbol __start___ksymtab_unused[];
134 extern const struct kernel_symbol __stop___ksymtab_unused[];
135 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
136 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
137 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
138 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
139 extern const unsigned long __start___kcrctab[];
140 extern const unsigned long __start___kcrctab_gpl[];
141 extern const unsigned long __start___kcrctab_gpl_future[];
142 extern const unsigned long __start___kcrctab_unused[];
143 extern const unsigned long __start___kcrctab_unused_gpl[];
145 #ifndef CONFIG_MODVERSIONS
146 #define symversion(base, idx) NULL
148 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
151 /* lookup symbol in given range of kernel_symbols */
152 static const struct kernel_symbol *lookup_symbol(const char *name,
153 const struct kernel_symbol *start,
154 const struct kernel_symbol *stop)
156 const struct kernel_symbol *ks = start;
157 for (; ks < stop; ks++)
158 if (strcmp(ks->name, name) == 0)
163 static void printk_unused_warning(const char *name)
165 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
166 "however this module is using it.\n", name);
167 printk(KERN_WARNING "This symbol will go away in the future.\n");
168 printk(KERN_WARNING "Please evalute if this is the right api to use, "
169 "and if it really is, submit a report the linux kernel "
170 "mailinglist together with submitting your code for "
174 /* Find a symbol, return value, crc and module which owns it */
175 static unsigned long __find_symbol(const char *name,
176 struct module **owner,
177 const unsigned long **crc,
181 const struct kernel_symbol *ks;
183 /* Core kernel first. */
185 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
187 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
191 ks = lookup_symbol(name, __start___ksymtab_gpl,
192 __stop___ksymtab_gpl);
194 *crc = symversion(__start___kcrctab_gpl,
195 (ks - __start___ksymtab_gpl));
199 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
200 __stop___ksymtab_gpl_future);
203 printk(KERN_WARNING "Symbol %s is being used "
204 "by a non-GPL module, which will not "
205 "be allowed in the future\n", name);
206 printk(KERN_WARNING "Please see the file "
207 "Documentation/feature-removal-schedule.txt "
208 "in the kernel source tree for more "
211 *crc = symversion(__start___kcrctab_gpl_future,
212 (ks - __start___ksymtab_gpl_future));
216 ks = lookup_symbol(name, __start___ksymtab_unused,
217 __stop___ksymtab_unused);
219 printk_unused_warning(name);
220 *crc = symversion(__start___kcrctab_unused,
221 (ks - __start___ksymtab_unused));
226 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
227 __stop___ksymtab_unused_gpl);
229 printk_unused_warning(name);
230 *crc = symversion(__start___kcrctab_unused_gpl,
231 (ks - __start___ksymtab_unused_gpl));
235 /* Now try modules. */
236 list_for_each_entry(mod, &modules, list) {
238 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
240 *crc = symversion(mod->crcs, (ks - mod->syms));
245 ks = lookup_symbol(name, mod->gpl_syms,
246 mod->gpl_syms + mod->num_gpl_syms);
248 *crc = symversion(mod->gpl_crcs,
249 (ks - mod->gpl_syms));
253 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
255 printk_unused_warning(name);
256 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
261 ks = lookup_symbol(name, mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
264 printk_unused_warning(name);
265 *crc = symversion(mod->unused_gpl_crcs,
266 (ks - mod->unused_gpl_syms));
270 ks = lookup_symbol(name, mod->gpl_future_syms,
271 (mod->gpl_future_syms +
272 mod->num_gpl_future_syms));
275 printk(KERN_WARNING "Symbol %s is being used "
276 "by a non-GPL module, which will not "
277 "be allowed in the future\n", name);
278 printk(KERN_WARNING "Please see the file "
279 "Documentation/feature-removal-schedule.txt "
280 "in the kernel source tree for more "
283 *crc = symversion(mod->gpl_future_crcs,
284 (ks - mod->gpl_future_syms));
288 DEBUGP("Failed to find symbol %s\n", name);
292 /* Search for module by name: must hold module_mutex. */
293 static struct module *find_module(const char *name)
297 list_for_each_entry(mod, &modules, list) {
298 if (strcmp(mod->name, name) == 0)
305 /* Number of blocks used and allocated. */
306 static unsigned int pcpu_num_used, pcpu_num_allocated;
307 /* Size of each block. -ve means used. */
308 static int *pcpu_size;
310 static int split_block(unsigned int i, unsigned short size)
312 /* Reallocation required? */
313 if (pcpu_num_used + 1 > pcpu_num_allocated) {
316 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
321 pcpu_num_allocated *= 2;
325 /* Insert a new subblock */
326 memmove(&pcpu_size[i+1], &pcpu_size[i],
327 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
330 pcpu_size[i+1] -= size;
335 static inline unsigned int block_size(int val)
342 /* Created by linker magic */
343 extern char __per_cpu_start[], __per_cpu_end[];
345 static void *percpu_modalloc(unsigned long size, unsigned long align,
352 if (align > PAGE_SIZE) {
353 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
354 name, align, PAGE_SIZE);
358 ptr = __per_cpu_start;
359 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
360 /* Extra for alignment requirement. */
361 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
362 BUG_ON(i == 0 && extra != 0);
364 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
367 /* Transfer extra to previous block. */
368 if (pcpu_size[i-1] < 0)
369 pcpu_size[i-1] -= extra;
371 pcpu_size[i-1] += extra;
372 pcpu_size[i] -= extra;
375 /* Split block if warranted */
376 if (pcpu_size[i] - size > sizeof(unsigned long))
377 if (!split_block(i, size))
381 pcpu_size[i] = -pcpu_size[i];
385 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
390 static void percpu_modfree(void *freeme)
393 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
395 /* First entry is core kernel percpu data. */
396 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
398 pcpu_size[i] = -pcpu_size[i];
405 /* Merge with previous? */
406 if (pcpu_size[i-1] >= 0) {
407 pcpu_size[i-1] += pcpu_size[i];
409 memmove(&pcpu_size[i], &pcpu_size[i+1],
410 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
413 /* Merge with next? */
414 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
415 pcpu_size[i] += pcpu_size[i+1];
417 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
418 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
422 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
424 const char *secstrings)
426 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
429 static int percpu_modinit(void)
432 pcpu_num_allocated = 2;
433 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
435 /* Static in-kernel percpu data (used). */
436 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
438 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
439 if (pcpu_size[1] < 0) {
440 printk(KERN_ERR "No per-cpu room for modules.\n");
446 __initcall(percpu_modinit);
447 #else /* ... !CONFIG_SMP */
448 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
453 static inline void percpu_modfree(void *pcpuptr)
457 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
459 const char *secstrings)
463 static inline void percpu_modcopy(void *pcpudst, const void *src,
466 /* pcpusec should be 0, and size of that section should be 0. */
469 #endif /* CONFIG_SMP */
471 #define MODINFO_ATTR(field) \
472 static void setup_modinfo_##field(struct module *mod, const char *s) \
474 mod->field = kstrdup(s, GFP_KERNEL); \
476 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
477 struct module *mod, char *buffer) \
479 return sprintf(buffer, "%s\n", mod->field); \
481 static int modinfo_##field##_exists(struct module *mod) \
483 return mod->field != NULL; \
485 static void free_modinfo_##field(struct module *mod) \
490 static struct module_attribute modinfo_##field = { \
491 .attr = { .name = __stringify(field), .mode = 0444, \
492 .owner = THIS_MODULE }, \
493 .show = show_modinfo_##field, \
494 .setup = setup_modinfo_##field, \
495 .test = modinfo_##field##_exists, \
496 .free = free_modinfo_##field, \
499 MODINFO_ATTR(version);
500 MODINFO_ATTR(srcversion);
502 #ifdef CONFIG_MODULE_UNLOAD
503 /* Init the unload section of the module. */
504 static void module_unload_init(struct module *mod)
508 INIT_LIST_HEAD(&mod->modules_which_use_me);
509 for (i = 0; i < NR_CPUS; i++)
510 local_set(&mod->ref[i].count, 0);
511 /* Hold reference count during initialization. */
512 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
513 /* Backwards compatibility macros put refcount during init. */
514 mod->waiter = current;
517 /* modules using other modules */
520 struct list_head list;
521 struct module *module_which_uses;
524 /* Does a already use b? */
525 static int already_uses(struct module *a, struct module *b)
527 struct module_use *use;
529 list_for_each_entry(use, &b->modules_which_use_me, list) {
530 if (use->module_which_uses == a) {
531 DEBUGP("%s uses %s!\n", a->name, b->name);
535 DEBUGP("%s does not use %s!\n", a->name, b->name);
539 /* Module a uses b */
540 static int use_module(struct module *a, struct module *b)
542 struct module_use *use;
545 if (b == NULL || already_uses(a, b)) return 1;
547 if (!strong_try_module_get(b))
550 DEBUGP("Allocating new usage for %s.\n", a->name);
551 use = kmalloc(sizeof(*use), GFP_ATOMIC);
553 printk("%s: out of memory loading\n", a->name);
558 use->module_which_uses = a;
559 list_add(&use->list, &b->modules_which_use_me);
560 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
564 /* Clear the unload stuff of the module. */
565 static void module_unload_free(struct module *mod)
569 list_for_each_entry(i, &modules, list) {
570 struct module_use *use;
572 list_for_each_entry(use, &i->modules_which_use_me, list) {
573 if (use->module_which_uses == mod) {
574 DEBUGP("%s unusing %s\n", mod->name, i->name);
576 list_del(&use->list);
578 sysfs_remove_link(i->holders_dir, mod->name);
579 /* There can be at most one match. */
586 #ifdef CONFIG_MODULE_FORCE_UNLOAD
587 static inline int try_force_unload(unsigned int flags)
589 int ret = (flags & O_TRUNC);
591 add_taint(TAINT_FORCED_RMMOD);
595 static inline int try_force_unload(unsigned int flags)
599 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
608 /* Whole machine is stopped with interrupts off when this runs. */
609 static int __try_stop_module(void *_sref)
611 struct stopref *sref = _sref;
613 /* If it's not unused, quit unless we are told to block. */
614 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
615 if (!(*sref->forced = try_force_unload(sref->flags)))
619 /* Mark it as dying. */
620 sref->mod->state = MODULE_STATE_GOING;
624 static int try_stop_module(struct module *mod, int flags, int *forced)
626 struct stopref sref = { mod, flags, forced };
628 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
631 unsigned int module_refcount(struct module *mod)
633 unsigned int i, total = 0;
635 for (i = 0; i < NR_CPUS; i++)
636 total += local_read(&mod->ref[i].count);
639 EXPORT_SYMBOL(module_refcount);
641 /* This exists whether we can unload or not */
642 static void free_module(struct module *mod);
644 static void wait_for_zero_refcount(struct module *mod)
646 /* Since we might sleep for some time, drop the semaphore first */
647 mutex_unlock(&module_mutex);
649 DEBUGP("Looking at refcount...\n");
650 set_current_state(TASK_UNINTERRUPTIBLE);
651 if (module_refcount(mod) == 0)
655 current->state = TASK_RUNNING;
656 mutex_lock(&module_mutex);
660 sys_delete_module(const char __user *name_user, unsigned int flags)
663 char name[MODULE_NAME_LEN];
666 if (!capable(CAP_SYS_MODULE))
669 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
671 name[MODULE_NAME_LEN-1] = '\0';
673 if (mutex_lock_interruptible(&module_mutex) != 0)
676 mod = find_module(name);
682 if (!list_empty(&mod->modules_which_use_me)) {
683 /* Other modules depend on us: get rid of them first. */
688 /* Doing init or already dying? */
689 if (mod->state != MODULE_STATE_LIVE) {
690 /* FIXME: if (force), slam module count and wake up
692 DEBUGP("%s already dying\n", mod->name);
697 /* If it has an init func, it must have an exit func to unload */
698 if ((mod->init != NULL && mod->exit == NULL)
700 forced = try_force_unload(flags);
702 /* This module can't be removed */
708 /* Set this up before setting mod->state */
709 mod->waiter = current;
711 /* Stop the machine so refcounts can't move and disable module. */
712 ret = try_stop_module(mod, flags, &forced);
716 /* Never wait if forced. */
717 if (!forced && module_refcount(mod) != 0)
718 wait_for_zero_refcount(mod);
720 /* Final destruction now noone is using it. */
721 if (mod->exit != NULL) {
722 mutex_unlock(&module_mutex);
724 mutex_lock(&module_mutex);
729 mutex_unlock(&module_mutex);
733 static void print_unload_info(struct seq_file *m, struct module *mod)
735 struct module_use *use;
736 int printed_something = 0;
738 seq_printf(m, " %u ", module_refcount(mod));
740 /* Always include a trailing , so userspace can differentiate
741 between this and the old multi-field proc format. */
742 list_for_each_entry(use, &mod->modules_which_use_me, list) {
743 printed_something = 1;
744 seq_printf(m, "%s,", use->module_which_uses->name);
748 printed_something = 1;
749 seq_printf(m, "[unsafe],");
752 if (mod->init != NULL && mod->exit == NULL) {
753 printed_something = 1;
754 seq_printf(m, "[permanent],");
757 if (!printed_something)
761 void __symbol_put(const char *symbol)
763 struct module *owner;
765 const unsigned long *crc;
767 spin_lock_irqsave(&modlist_lock, flags);
768 if (!__find_symbol(symbol, &owner, &crc, 1))
771 spin_unlock_irqrestore(&modlist_lock, flags);
773 EXPORT_SYMBOL(__symbol_put);
775 void symbol_put_addr(void *addr)
777 struct module *modaddr;
779 if (core_kernel_text((unsigned long)addr))
782 if (!(modaddr = module_text_address((unsigned long)addr)))
786 EXPORT_SYMBOL_GPL(symbol_put_addr);
788 static ssize_t show_refcnt(struct module_attribute *mattr,
789 struct module *mod, char *buffer)
791 /* sysfs holds a reference */
792 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
795 static struct module_attribute refcnt = {
796 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
800 void module_put(struct module *module)
803 unsigned int cpu = get_cpu();
804 local_dec(&module->ref[cpu].count);
805 /* Maybe they're waiting for us to drop reference? */
806 if (unlikely(!module_is_live(module)))
807 wake_up_process(module->waiter);
811 EXPORT_SYMBOL(module_put);
813 #else /* !CONFIG_MODULE_UNLOAD */
814 static void print_unload_info(struct seq_file *m, struct module *mod)
816 /* We don't know the usage count, or what modules are using. */
817 seq_printf(m, " - -");
820 static inline void module_unload_free(struct module *mod)
824 static inline int use_module(struct module *a, struct module *b)
826 return strong_try_module_get(b);
829 static inline void module_unload_init(struct module *mod)
832 #endif /* CONFIG_MODULE_UNLOAD */
834 static ssize_t show_initstate(struct module_attribute *mattr,
835 struct module *mod, char *buffer)
837 const char *state = "unknown";
839 switch (mod->state) {
840 case MODULE_STATE_LIVE:
843 case MODULE_STATE_COMING:
846 case MODULE_STATE_GOING:
850 return sprintf(buffer, "%s\n", state);
853 static struct module_attribute initstate = {
854 .attr = { .name = "initstate", .mode = 0444, .owner = THIS_MODULE },
855 .show = show_initstate,
858 static struct module_attribute *modinfo_attrs[] = {
862 #ifdef CONFIG_MODULE_UNLOAD
868 static const char vermagic[] = VERMAGIC_STRING;
870 #ifdef CONFIG_MODVERSIONS
871 static int check_version(Elf_Shdr *sechdrs,
872 unsigned int versindex,
875 const unsigned long *crc)
877 unsigned int i, num_versions;
878 struct modversion_info *versions;
880 /* Exporting module didn't supply crcs? OK, we're already tainted. */
884 versions = (void *) sechdrs[versindex].sh_addr;
885 num_versions = sechdrs[versindex].sh_size
886 / sizeof(struct modversion_info);
888 for (i = 0; i < num_versions; i++) {
889 if (strcmp(versions[i].name, symname) != 0)
892 if (versions[i].crc == *crc)
894 printk("%s: disagrees about version of symbol %s\n",
896 DEBUGP("Found checksum %lX vs module %lX\n",
897 *crc, versions[i].crc);
900 /* Not in module's version table. OK, but that taints the kernel. */
901 if (!(tainted & TAINT_FORCED_MODULE))
902 printk("%s: no version for \"%s\" found: kernel tainted.\n",
904 add_taint_module(mod, TAINT_FORCED_MODULE);
908 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
909 unsigned int versindex,
912 const unsigned long *crc;
913 struct module *owner;
915 if (!__find_symbol("struct_module", &owner, &crc, 1))
917 return check_version(sechdrs, versindex, "struct_module", mod,
921 /* First part is kernel version, which we ignore. */
922 static inline int same_magic(const char *amagic, const char *bmagic)
924 amagic += strcspn(amagic, " ");
925 bmagic += strcspn(bmagic, " ");
926 return strcmp(amagic, bmagic) == 0;
929 static inline int check_version(Elf_Shdr *sechdrs,
930 unsigned int versindex,
933 const unsigned long *crc)
938 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
939 unsigned int versindex,
945 static inline int same_magic(const char *amagic, const char *bmagic)
947 return strcmp(amagic, bmagic) == 0;
949 #endif /* CONFIG_MODVERSIONS */
951 /* Resolve a symbol for this module. I.e. if we find one, record usage.
952 Must be holding module_mutex. */
953 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
954 unsigned int versindex,
958 struct module *owner;
960 const unsigned long *crc;
962 ret = __find_symbol(name, &owner, &crc,
963 !(mod->taints & TAINT_PROPRIETARY_MODULE));
965 /* use_module can fail due to OOM, or module unloading */
966 if (!check_version(sechdrs, versindex, name, mod, crc) ||
967 !use_module(mod, owner))
975 * /sys/module/foo/sections stuff
976 * J. Corbet <corbet@lwn.net>
978 #ifdef CONFIG_KALLSYMS
979 static ssize_t module_sect_show(struct module_attribute *mattr,
980 struct module *mod, char *buf)
982 struct module_sect_attr *sattr =
983 container_of(mattr, struct module_sect_attr, mattr);
984 return sprintf(buf, "0x%lx\n", sattr->address);
987 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
991 for (section = 0; section < sect_attrs->nsections; section++)
992 kfree(sect_attrs->attrs[section].name);
996 static void add_sect_attrs(struct module *mod, unsigned int nsect,
997 char *secstrings, Elf_Shdr *sechdrs)
999 unsigned int nloaded = 0, i, size[2];
1000 struct module_sect_attrs *sect_attrs;
1001 struct module_sect_attr *sattr;
1002 struct attribute **gattr;
1004 /* Count loaded sections and allocate structures */
1005 for (i = 0; i < nsect; i++)
1006 if (sechdrs[i].sh_flags & SHF_ALLOC)
1008 size[0] = ALIGN(sizeof(*sect_attrs)
1009 + nloaded * sizeof(sect_attrs->attrs[0]),
1010 sizeof(sect_attrs->grp.attrs[0]));
1011 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1012 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1013 if (sect_attrs == NULL)
1016 /* Setup section attributes. */
1017 sect_attrs->grp.name = "sections";
1018 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1020 sect_attrs->nsections = 0;
1021 sattr = §_attrs->attrs[0];
1022 gattr = §_attrs->grp.attrs[0];
1023 for (i = 0; i < nsect; i++) {
1024 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1026 sattr->address = sechdrs[i].sh_addr;
1027 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1029 if (sattr->name == NULL)
1031 sect_attrs->nsections++;
1032 sattr->mattr.show = module_sect_show;
1033 sattr->mattr.store = NULL;
1034 sattr->mattr.attr.name = sattr->name;
1035 sattr->mattr.attr.owner = mod;
1036 sattr->mattr.attr.mode = S_IRUGO;
1037 *(gattr++) = &(sattr++)->mattr.attr;
1041 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1044 mod->sect_attrs = sect_attrs;
1047 free_sect_attrs(sect_attrs);
1050 static void remove_sect_attrs(struct module *mod)
1052 if (mod->sect_attrs) {
1053 sysfs_remove_group(&mod->mkobj.kobj,
1054 &mod->sect_attrs->grp);
1055 /* We are positive that no one is using any sect attrs
1056 * at this point. Deallocate immediately. */
1057 free_sect_attrs(mod->sect_attrs);
1058 mod->sect_attrs = NULL;
1064 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1065 char *sectstrings, Elf_Shdr *sechdrs)
1069 static inline void remove_sect_attrs(struct module *mod)
1072 #endif /* CONFIG_KALLSYMS */
1075 int module_add_modinfo_attrs(struct module *mod)
1077 struct module_attribute *attr;
1078 struct module_attribute *temp_attr;
1082 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1083 (ARRAY_SIZE(modinfo_attrs) + 1)),
1085 if (!mod->modinfo_attrs)
1088 temp_attr = mod->modinfo_attrs;
1089 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1091 (attr->test && attr->test(mod))) {
1092 memcpy(temp_attr, attr, sizeof(*temp_attr));
1093 temp_attr->attr.owner = mod;
1094 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1101 void module_remove_modinfo_attrs(struct module *mod)
1103 struct module_attribute *attr;
1106 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1107 /* pick a field to test for end of list */
1108 if (!attr->attr.name)
1110 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1114 kfree(mod->modinfo_attrs);
1119 int mod_sysfs_init(struct module *mod)
1123 if (!module_sysfs_initialized) {
1124 printk(KERN_ERR "%s: module sysfs not initialized\n",
1129 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1130 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1133 kobj_set_kset_s(&mod->mkobj, module_subsys);
1134 mod->mkobj.mod = mod;
1136 kobject_init(&mod->mkobj.kobj);
1142 int mod_sysfs_setup(struct module *mod,
1143 struct kernel_param *kparam,
1144 unsigned int num_params)
1148 /* delay uevent until full sysfs population */
1149 err = kobject_add(&mod->mkobj.kobj);
1153 mod->holders_dir = kobject_add_dir(&mod->mkobj.kobj, "holders");
1154 if (!mod->holders_dir) {
1159 err = module_param_sysfs_setup(mod, kparam, num_params);
1161 goto out_unreg_holders;
1163 err = module_add_modinfo_attrs(mod);
1165 goto out_unreg_param;
1167 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1171 module_param_sysfs_remove(mod);
1173 kobject_unregister(mod->holders_dir);
1175 kobject_del(&mod->mkobj.kobj);
1176 kobject_put(&mod->mkobj.kobj);
1182 static void mod_kobject_remove(struct module *mod)
1184 module_remove_modinfo_attrs(mod);
1185 module_param_sysfs_remove(mod);
1186 kobject_unregister(mod->mkobj.drivers_dir);
1187 kobject_unregister(mod->holders_dir);
1188 kobject_unregister(&mod->mkobj.kobj);
1192 * unlink the module with the whole machine is stopped with interrupts off
1193 * - this defends against kallsyms not taking locks
1195 static int __unlink_module(void *_mod)
1197 struct module *mod = _mod;
1198 list_del(&mod->list);
1202 /* Free a module, remove from lists, etc (must hold module_mutex). */
1203 static void free_module(struct module *mod)
1205 /* Delete from various lists */
1206 stop_machine_run(__unlink_module, mod, NR_CPUS);
1207 remove_sect_attrs(mod);
1208 mod_kobject_remove(mod);
1210 unwind_remove_table(mod->unwind_info, 0);
1212 /* Arch-specific cleanup. */
1213 module_arch_cleanup(mod);
1215 /* Module unload stuff */
1216 module_unload_free(mod);
1218 /* This may be NULL, but that's OK */
1219 module_free(mod, mod->module_init);
1222 percpu_modfree(mod->percpu);
1224 /* Free lock-classes: */
1225 lockdep_free_key_range(mod->module_core, mod->core_size);
1227 /* Finally, free the core (containing the module structure) */
1228 module_free(mod, mod->module_core);
1231 void *__symbol_get(const char *symbol)
1233 struct module *owner;
1234 unsigned long value, flags;
1235 const unsigned long *crc;
1237 spin_lock_irqsave(&modlist_lock, flags);
1238 value = __find_symbol(symbol, &owner, &crc, 1);
1239 if (value && !strong_try_module_get(owner))
1241 spin_unlock_irqrestore(&modlist_lock, flags);
1243 return (void *)value;
1245 EXPORT_SYMBOL_GPL(__symbol_get);
1248 * Ensure that an exported symbol [global namespace] does not already exist
1249 * in the kernel or in some other module's exported symbol table.
1251 static int verify_export_symbols(struct module *mod)
1253 const char *name = NULL;
1254 unsigned long i, ret = 0;
1255 struct module *owner;
1256 const unsigned long *crc;
1258 for (i = 0; i < mod->num_syms; i++)
1259 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
1260 name = mod->syms[i].name;
1265 for (i = 0; i < mod->num_gpl_syms; i++)
1266 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
1267 name = mod->gpl_syms[i].name;
1274 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1275 mod->name, name, module_name(owner));
1280 /* Change all symbols so that sh_value encodes the pointer directly. */
1281 static int simplify_symbols(Elf_Shdr *sechdrs,
1282 unsigned int symindex,
1284 unsigned int versindex,
1285 unsigned int pcpuindex,
1288 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1289 unsigned long secbase;
1290 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1293 for (i = 1; i < n; i++) {
1294 switch (sym[i].st_shndx) {
1296 /* We compiled with -fno-common. These are not
1297 supposed to happen. */
1298 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1299 printk("%s: please compile with -fno-common\n",
1305 /* Don't need to do anything */
1306 DEBUGP("Absolute symbol: 0x%08lx\n",
1307 (long)sym[i].st_value);
1312 = resolve_symbol(sechdrs, versindex,
1313 strtab + sym[i].st_name, mod);
1315 /* Ok if resolved. */
1316 if (sym[i].st_value != 0)
1319 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1322 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1323 mod->name, strtab + sym[i].st_name);
1328 /* Divert to percpu allocation if a percpu var. */
1329 if (sym[i].st_shndx == pcpuindex)
1330 secbase = (unsigned long)mod->percpu;
1332 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1333 sym[i].st_value += secbase;
1341 /* Update size with this section: return offset. */
1342 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1346 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1347 *size = ret + sechdr->sh_size;
1351 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1352 might -- code, read-only data, read-write data, small data. Tally
1353 sizes, and place the offsets into sh_entsize fields: high bit means it
1355 static void layout_sections(struct module *mod,
1356 const Elf_Ehdr *hdr,
1358 const char *secstrings)
1360 static unsigned long const masks[][2] = {
1361 /* NOTE: all executable code must be the first section
1362 * in this array; otherwise modify the text_size
1363 * finder in the two loops below */
1364 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1365 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1366 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1367 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1371 for (i = 0; i < hdr->e_shnum; i++)
1372 sechdrs[i].sh_entsize = ~0UL;
1374 DEBUGP("Core section allocation order:\n");
1375 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1376 for (i = 0; i < hdr->e_shnum; ++i) {
1377 Elf_Shdr *s = &sechdrs[i];
1379 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1380 || (s->sh_flags & masks[m][1])
1381 || s->sh_entsize != ~0UL
1382 || strncmp(secstrings + s->sh_name,
1385 s->sh_entsize = get_offset(&mod->core_size, s);
1386 DEBUGP("\t%s\n", secstrings + s->sh_name);
1389 mod->core_text_size = mod->core_size;
1392 DEBUGP("Init section allocation order:\n");
1393 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1394 for (i = 0; i < hdr->e_shnum; ++i) {
1395 Elf_Shdr *s = &sechdrs[i];
1397 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1398 || (s->sh_flags & masks[m][1])
1399 || s->sh_entsize != ~0UL
1400 || strncmp(secstrings + s->sh_name,
1403 s->sh_entsize = (get_offset(&mod->init_size, s)
1404 | INIT_OFFSET_MASK);
1405 DEBUGP("\t%s\n", secstrings + s->sh_name);
1408 mod->init_text_size = mod->init_size;
1412 static void set_license(struct module *mod, const char *license)
1415 license = "unspecified";
1417 if (!license_is_gpl_compatible(license)) {
1418 if (!(tainted & TAINT_PROPRIETARY_MODULE))
1419 printk(KERN_WARNING "%s: module license '%s' taints "
1420 "kernel.\n", mod->name, license);
1421 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1425 /* Parse tag=value strings from .modinfo section */
1426 static char *next_string(char *string, unsigned long *secsize)
1428 /* Skip non-zero chars */
1431 if ((*secsize)-- <= 1)
1435 /* Skip any zero padding. */
1436 while (!string[0]) {
1438 if ((*secsize)-- <= 1)
1444 static char *get_modinfo(Elf_Shdr *sechdrs,
1449 unsigned int taglen = strlen(tag);
1450 unsigned long size = sechdrs[info].sh_size;
1452 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1453 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1454 return p + taglen + 1;
1459 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1460 unsigned int infoindex)
1462 struct module_attribute *attr;
1465 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1468 get_modinfo(sechdrs,
1474 #ifdef CONFIG_KALLSYMS
1475 static int is_exported(const char *name, const struct module *mod)
1477 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1480 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1487 static char elf_type(const Elf_Sym *sym,
1489 const char *secstrings,
1492 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1493 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1498 if (sym->st_shndx == SHN_UNDEF)
1500 if (sym->st_shndx == SHN_ABS)
1502 if (sym->st_shndx >= SHN_LORESERVE)
1504 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1506 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1507 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1508 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1510 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1515 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1516 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1521 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1522 ".debug", strlen(".debug")) == 0)
1527 static void add_kallsyms(struct module *mod,
1529 unsigned int symindex,
1530 unsigned int strindex,
1531 const char *secstrings)
1535 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1536 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1537 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1539 /* Set types up while we still have access to sections. */
1540 for (i = 0; i < mod->num_symtab; i++)
1541 mod->symtab[i].st_info
1542 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1545 static inline void add_kallsyms(struct module *mod,
1547 unsigned int symindex,
1548 unsigned int strindex,
1549 const char *secstrings)
1552 #endif /* CONFIG_KALLSYMS */
1554 /* Allocate and load the module: note that size of section 0 is always
1555 zero, and we rely on this for optional sections. */
1556 static struct module *load_module(void __user *umod,
1558 const char __user *uargs)
1562 char *secstrings, *args, *modmagic, *strtab = NULL;
1564 unsigned int symindex = 0;
1565 unsigned int strindex = 0;
1566 unsigned int setupindex;
1567 unsigned int exindex;
1568 unsigned int exportindex;
1569 unsigned int modindex;
1570 unsigned int obsparmindex;
1571 unsigned int infoindex;
1572 unsigned int gplindex;
1573 unsigned int crcindex;
1574 unsigned int gplcrcindex;
1575 unsigned int versindex;
1576 unsigned int pcpuindex;
1577 unsigned int gplfutureindex;
1578 unsigned int gplfuturecrcindex;
1579 unsigned int unwindex = 0;
1580 unsigned int unusedindex;
1581 unsigned int unusedcrcindex;
1582 unsigned int unusedgplindex;
1583 unsigned int unusedgplcrcindex;
1586 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1587 struct exception_table_entry *extable;
1588 mm_segment_t old_fs;
1590 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1592 if (len < sizeof(*hdr))
1593 return ERR_PTR(-ENOEXEC);
1595 /* Suck in entire file: we'll want most of it. */
1596 /* vmalloc barfs on "unusual" numbers. Check here */
1597 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1598 return ERR_PTR(-ENOMEM);
1599 if (copy_from_user(hdr, umod, len) != 0) {
1604 /* Sanity checks against insmoding binaries or wrong arch,
1605 weird elf version */
1606 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1607 || hdr->e_type != ET_REL
1608 || !elf_check_arch(hdr)
1609 || hdr->e_shentsize != sizeof(*sechdrs)) {
1614 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1617 /* Convenience variables */
1618 sechdrs = (void *)hdr + hdr->e_shoff;
1619 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1620 sechdrs[0].sh_addr = 0;
1622 for (i = 1; i < hdr->e_shnum; i++) {
1623 if (sechdrs[i].sh_type != SHT_NOBITS
1624 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1627 /* Mark all sections sh_addr with their address in the
1629 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1631 /* Internal symbols and strings. */
1632 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1634 strindex = sechdrs[i].sh_link;
1635 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1637 #ifndef CONFIG_MODULE_UNLOAD
1638 /* Don't load .exit sections */
1639 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1640 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1644 modindex = find_sec(hdr, sechdrs, secstrings,
1645 ".gnu.linkonce.this_module");
1647 printk(KERN_WARNING "No module found in object\n");
1651 mod = (void *)sechdrs[modindex].sh_addr;
1653 if (symindex == 0) {
1654 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1660 /* Optional sections */
1661 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1662 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1663 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
1664 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1665 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
1666 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1667 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1668 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
1669 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1670 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
1671 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1672 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1673 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1674 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1675 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1676 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1677 #ifdef ARCH_UNWIND_SECTION_NAME
1678 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1681 /* Don't keep modinfo section */
1682 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1683 #ifdef CONFIG_KALLSYMS
1684 /* Keep symbol and string tables for decoding later. */
1685 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1686 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1689 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1691 /* Check module struct version now, before we try to use module. */
1692 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1697 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1698 /* This is allowed: modprobe --force will invalidate it. */
1700 add_taint_module(mod, TAINT_FORCED_MODULE);
1701 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1703 } else if (!same_magic(modmagic, vermagic)) {
1704 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1705 mod->name, modmagic, vermagic);
1710 /* Now copy in args */
1711 args = strndup_user(uargs, ~0UL >> 1);
1713 err = PTR_ERR(args);
1717 if (find_module(mod->name)) {
1722 mod->state = MODULE_STATE_COMING;
1724 /* Allow arches to frob section contents and sizes. */
1725 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1730 /* We have a special allocation for this section. */
1731 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1732 sechdrs[pcpuindex].sh_addralign,
1738 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1739 mod->percpu = percpu;
1742 /* Determine total sizes, and put offsets in sh_entsize. For now
1743 this is done generically; there doesn't appear to be any
1744 special cases for the architectures. */
1745 layout_sections(mod, hdr, sechdrs, secstrings);
1747 /* Do the allocs. */
1748 ptr = module_alloc(mod->core_size);
1753 memset(ptr, 0, mod->core_size);
1754 mod->module_core = ptr;
1756 ptr = module_alloc(mod->init_size);
1757 if (!ptr && mod->init_size) {
1761 memset(ptr, 0, mod->init_size);
1762 mod->module_init = ptr;
1764 /* Transfer each section which specifies SHF_ALLOC */
1765 DEBUGP("final section addresses:\n");
1766 for (i = 0; i < hdr->e_shnum; i++) {
1769 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1772 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1773 dest = mod->module_init
1774 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1776 dest = mod->module_core + sechdrs[i].sh_entsize;
1778 if (sechdrs[i].sh_type != SHT_NOBITS)
1779 memcpy(dest, (void *)sechdrs[i].sh_addr,
1780 sechdrs[i].sh_size);
1781 /* Update sh_addr to point to copy in image. */
1782 sechdrs[i].sh_addr = (unsigned long)dest;
1783 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1785 /* Module has been moved. */
1786 mod = (void *)sechdrs[modindex].sh_addr;
1788 /* Now we've moved module, initialize linked lists, etc. */
1789 module_unload_init(mod);
1791 /* Initialize kobject, so we can reference it. */
1792 if (mod_sysfs_init(mod) != 0)
1795 /* Set up license info based on the info section */
1796 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1798 if (strcmp(mod->name, "ndiswrapper") == 0)
1799 add_taint(TAINT_PROPRIETARY_MODULE);
1800 if (strcmp(mod->name, "driverloader") == 0)
1801 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1803 /* Set up MODINFO_ATTR fields */
1804 setup_modinfo(mod, sechdrs, infoindex);
1806 /* Fix up syms, so that st_value is a pointer to location. */
1807 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1812 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1813 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1814 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1816 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1817 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1818 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1820 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1821 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1822 sizeof(*mod->gpl_future_syms);
1823 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
1824 sizeof(*mod->unused_syms);
1825 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
1826 sizeof(*mod->unused_gpl_syms);
1827 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1828 if (gplfuturecrcindex)
1829 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
1831 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
1833 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
1834 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
1835 if (unusedgplcrcindex)
1836 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
1838 #ifdef CONFIG_MODVERSIONS
1839 if ((mod->num_syms && !crcindex) ||
1840 (mod->num_gpl_syms && !gplcrcindex) ||
1841 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
1842 (mod->num_unused_syms && !unusedcrcindex) ||
1843 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
1844 printk(KERN_WARNING "%s: No versions for exported symbols."
1845 " Tainting kernel.\n", mod->name);
1846 add_taint_module(mod, TAINT_FORCED_MODULE);
1850 /* Now do relocations. */
1851 for (i = 1; i < hdr->e_shnum; i++) {
1852 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1853 unsigned int info = sechdrs[i].sh_info;
1855 /* Not a valid relocation section? */
1856 if (info >= hdr->e_shnum)
1859 /* Don't bother with non-allocated sections */
1860 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1863 if (sechdrs[i].sh_type == SHT_REL)
1864 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1865 else if (sechdrs[i].sh_type == SHT_RELA)
1866 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1872 /* Find duplicate symbols */
1873 err = verify_export_symbols(mod);
1878 /* Set up and sort exception table */
1879 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1880 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1881 sort_extable(extable, extable + mod->num_exentries);
1883 /* Finally, copy percpu area over. */
1884 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1885 sechdrs[pcpuindex].sh_size);
1887 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1889 err = module_finalize(hdr, sechdrs, mod);
1893 /* flush the icache in correct context */
1898 * Flush the instruction cache, since we've played with text.
1899 * Do it before processing of module parameters, so the module
1900 * can provide parameter accessor functions of its own.
1902 if (mod->module_init)
1903 flush_icache_range((unsigned long)mod->module_init,
1904 (unsigned long)mod->module_init
1906 flush_icache_range((unsigned long)mod->module_core,
1907 (unsigned long)mod->module_core + mod->core_size);
1913 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
1916 /* Size of section 0 is 0, so this works well if no params */
1917 err = parse_args(mod->name, mod->args,
1918 (struct kernel_param *)
1919 sechdrs[setupindex].sh_addr,
1920 sechdrs[setupindex].sh_size
1921 / sizeof(struct kernel_param),
1926 err = mod_sysfs_setup(mod,
1927 (struct kernel_param *)
1928 sechdrs[setupindex].sh_addr,
1929 sechdrs[setupindex].sh_size
1930 / sizeof(struct kernel_param));
1933 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1935 /* Size of section 0 is 0, so this works well if no unwind info. */
1936 mod->unwind_info = unwind_add_table(mod,
1937 (void *)sechdrs[unwindex].sh_addr,
1938 sechdrs[unwindex].sh_size);
1940 /* Get rid of temporary copy */
1947 module_arch_cleanup(mod);
1949 module_unload_free(mod);
1950 module_free(mod, mod->module_init);
1952 module_free(mod, mod->module_core);
1955 percpu_modfree(percpu);
1960 return ERR_PTR(err);
1963 printk(KERN_ERR "Module len %lu truncated\n", len);
1969 * link the module with the whole machine is stopped with interrupts off
1970 * - this defends against kallsyms not taking locks
1972 static int __link_module(void *_mod)
1974 struct module *mod = _mod;
1975 list_add(&mod->list, &modules);
1979 /* This is where the real work happens */
1981 sys_init_module(void __user *umod,
1983 const char __user *uargs)
1988 /* Must have permission */
1989 if (!capable(CAP_SYS_MODULE))
1992 /* Only one module load at a time, please */
1993 if (mutex_lock_interruptible(&module_mutex) != 0)
1996 /* Do all the hard work */
1997 mod = load_module(umod, len, uargs);
1999 mutex_unlock(&module_mutex);
2000 return PTR_ERR(mod);
2003 /* Now sew it into the lists. They won't access us, since
2004 strong_try_module_get() will fail. */
2005 stop_machine_run(__link_module, mod, NR_CPUS);
2007 /* Drop lock so they can recurse */
2008 mutex_unlock(&module_mutex);
2010 blocking_notifier_call_chain(&module_notify_list,
2011 MODULE_STATE_COMING, mod);
2013 /* Start the module */
2014 if (mod->init != NULL)
2017 /* Init routine failed: abort. Try to protect us from
2018 buggy refcounters. */
2019 mod->state = MODULE_STATE_GOING;
2020 synchronize_sched();
2022 printk(KERN_ERR "%s: module is now stuck!\n",
2026 mutex_lock(&module_mutex);
2028 mutex_unlock(&module_mutex);
2033 /* Now it's a first class citizen! */
2034 mutex_lock(&module_mutex);
2035 mod->state = MODULE_STATE_LIVE;
2036 /* Drop initial reference. */
2038 unwind_remove_table(mod->unwind_info, 1);
2039 module_free(mod, mod->module_init);
2040 mod->module_init = NULL;
2042 mod->init_text_size = 0;
2043 mutex_unlock(&module_mutex);
2048 static inline int within(unsigned long addr, void *start, unsigned long size)
2050 return ((void *)addr >= start && (void *)addr < start + size);
2053 #ifdef CONFIG_KALLSYMS
2055 * This ignores the intensely annoying "mapping symbols" found
2056 * in ARM ELF files: $a, $t and $d.
2058 static inline int is_arm_mapping_symbol(const char *str)
2060 return str[0] == '$' && strchr("atd", str[1])
2061 && (str[2] == '\0' || str[2] == '.');
2064 static const char *get_ksymbol(struct module *mod,
2066 unsigned long *size,
2067 unsigned long *offset)
2069 unsigned int i, best = 0;
2070 unsigned long nextval;
2072 /* At worse, next value is at end of module */
2073 if (within(addr, mod->module_init, mod->init_size))
2074 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2076 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2078 /* Scan for closest preceeding symbol, and next symbol. (ELF
2079 starts real symbols at 1). */
2080 for (i = 1; i < mod->num_symtab; i++) {
2081 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2084 /* We ignore unnamed symbols: they're uninformative
2085 * and inserted at a whim. */
2086 if (mod->symtab[i].st_value <= addr
2087 && mod->symtab[i].st_value > mod->symtab[best].st_value
2088 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2089 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2091 if (mod->symtab[i].st_value > addr
2092 && mod->symtab[i].st_value < nextval
2093 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2094 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2095 nextval = mod->symtab[i].st_value;
2102 *size = nextval - mod->symtab[best].st_value;
2104 *offset = addr - mod->symtab[best].st_value;
2105 return mod->strtab + mod->symtab[best].st_name;
2108 /* For kallsyms to ask for address resolution. NULL means not found.
2109 We don't lock, as this is used for oops resolution and races are a
2111 const char *module_address_lookup(unsigned long addr,
2112 unsigned long *size,
2113 unsigned long *offset,
2118 list_for_each_entry(mod, &modules, list) {
2119 if (within(addr, mod->module_init, mod->init_size)
2120 || within(addr, mod->module_core, mod->core_size)) {
2122 *modname = mod->name;
2123 return get_ksymbol(mod, addr, size, offset);
2129 int lookup_module_symbol_name(unsigned long addr, char *symname)
2133 mutex_lock(&module_mutex);
2134 list_for_each_entry(mod, &modules, list) {
2135 if (within(addr, mod->module_init, mod->init_size) ||
2136 within(addr, mod->module_core, mod->core_size)) {
2139 sym = get_ksymbol(mod, addr, NULL, NULL);
2142 strlcpy(symname, sym, KSYM_NAME_LEN + 1);
2143 mutex_unlock(&module_mutex);
2148 mutex_unlock(&module_mutex);
2152 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2153 unsigned long *offset, char *modname, char *name)
2157 mutex_lock(&module_mutex);
2158 list_for_each_entry(mod, &modules, list) {
2159 if (within(addr, mod->module_init, mod->init_size) ||
2160 within(addr, mod->module_core, mod->core_size)) {
2163 sym = get_ksymbol(mod, addr, size, offset);
2167 strlcpy(modname, mod->name, MODULE_NAME_LEN + 1);
2169 strlcpy(name, sym, KSYM_NAME_LEN + 1);
2170 mutex_unlock(&module_mutex);
2175 mutex_unlock(&module_mutex);
2179 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2180 char *name, char *module_name, int *exported)
2184 mutex_lock(&module_mutex);
2185 list_for_each_entry(mod, &modules, list) {
2186 if (symnum < mod->num_symtab) {
2187 *value = mod->symtab[symnum].st_value;
2188 *type = mod->symtab[symnum].st_info;
2189 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2191 strlcpy(module_name, mod->name, MODULE_NAME_LEN + 1);
2192 *exported = is_exported(name, mod);
2193 mutex_unlock(&module_mutex);
2196 symnum -= mod->num_symtab;
2198 mutex_unlock(&module_mutex);
2202 static unsigned long mod_find_symname(struct module *mod, const char *name)
2206 for (i = 0; i < mod->num_symtab; i++)
2207 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2208 mod->symtab[i].st_info != 'U')
2209 return mod->symtab[i].st_value;
2213 /* Look for this name: can be of form module:name. */
2214 unsigned long module_kallsyms_lookup_name(const char *name)
2218 unsigned long ret = 0;
2220 /* Don't lock: we're in enough trouble already. */
2221 if ((colon = strchr(name, ':')) != NULL) {
2223 if ((mod = find_module(name)) != NULL)
2224 ret = mod_find_symname(mod, colon+1);
2227 list_for_each_entry(mod, &modules, list)
2228 if ((ret = mod_find_symname(mod, name)) != 0)
2233 #endif /* CONFIG_KALLSYMS */
2235 /* Called by the /proc file system to return a list of modules. */
2236 static void *m_start(struct seq_file *m, loff_t *pos)
2238 struct list_head *i;
2241 mutex_lock(&module_mutex);
2242 list_for_each(i, &modules) {
2251 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2253 struct list_head *i = p;
2255 if (i->next == &modules)
2260 static void m_stop(struct seq_file *m, void *p)
2262 mutex_unlock(&module_mutex);
2265 static char *taint_flags(unsigned int taints, char *buf)
2271 if (taints & TAINT_PROPRIETARY_MODULE)
2273 if (taints & TAINT_FORCED_MODULE)
2276 * TAINT_FORCED_RMMOD: could be added.
2277 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2287 static int m_show(struct seq_file *m, void *p)
2289 struct module *mod = list_entry(p, struct module, list);
2292 seq_printf(m, "%s %lu",
2293 mod->name, mod->init_size + mod->core_size);
2294 print_unload_info(m, mod);
2296 /* Informative for users. */
2297 seq_printf(m, " %s",
2298 mod->state == MODULE_STATE_GOING ? "Unloading":
2299 mod->state == MODULE_STATE_COMING ? "Loading":
2301 /* Used by oprofile and other similar tools. */
2302 seq_printf(m, " 0x%p", mod->module_core);
2306 seq_printf(m, " %s", taint_flags(mod->taints, buf));
2308 seq_printf(m, "\n");
2312 /* Format: modulename size refcount deps address
2314 Where refcount is a number or -, and deps is a comma-separated list
2317 const struct seq_operations modules_op = {
2324 /* Given an address, look for it in the module exception tables. */
2325 const struct exception_table_entry *search_module_extables(unsigned long addr)
2327 unsigned long flags;
2328 const struct exception_table_entry *e = NULL;
2331 spin_lock_irqsave(&modlist_lock, flags);
2332 list_for_each_entry(mod, &modules, list) {
2333 if (mod->num_exentries == 0)
2336 e = search_extable(mod->extable,
2337 mod->extable + mod->num_exentries - 1,
2342 spin_unlock_irqrestore(&modlist_lock, flags);
2344 /* Now, if we found one, we are running inside it now, hence
2345 we cannot unload the module, hence no refcnt needed. */
2350 * Is this a valid module address?
2352 int is_module_address(unsigned long addr)
2354 unsigned long flags;
2357 spin_lock_irqsave(&modlist_lock, flags);
2359 list_for_each_entry(mod, &modules, list) {
2360 if (within(addr, mod->module_core, mod->core_size)) {
2361 spin_unlock_irqrestore(&modlist_lock, flags);
2366 spin_unlock_irqrestore(&modlist_lock, flags);
2372 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2373 struct module *__module_text_address(unsigned long addr)
2377 list_for_each_entry(mod, &modules, list)
2378 if (within(addr, mod->module_init, mod->init_text_size)
2379 || within(addr, mod->module_core, mod->core_text_size))
2384 struct module *module_text_address(unsigned long addr)
2387 unsigned long flags;
2389 spin_lock_irqsave(&modlist_lock, flags);
2390 mod = __module_text_address(addr);
2391 spin_unlock_irqrestore(&modlist_lock, flags);
2396 /* Don't grab lock, we're oopsing. */
2397 void print_modules(void)
2402 printk("Modules linked in:");
2403 list_for_each_entry(mod, &modules, list)
2404 printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
2409 static char *make_driver_name(struct device_driver *drv)
2413 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2418 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2422 static void module_create_drivers_dir(struct module_kobject *mk)
2424 if (!mk || mk->drivers_dir)
2427 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
2430 void module_add_driver(struct module *mod, struct device_driver *drv)
2434 struct module_kobject *mk = NULL;
2441 else if (drv->mod_name) {
2442 struct kobject *mkobj;
2444 /* Lookup built-in module entry in /sys/modules */
2445 mkobj = kset_find_obj(&module_subsys, drv->mod_name);
2447 mk = container_of(mkobj, struct module_kobject, kobj);
2448 /* remember our module structure */
2450 /* kset_find_obj took a reference */
2458 /* Don't check return codes; these calls are idempotent */
2459 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
2460 driver_name = make_driver_name(drv);
2462 module_create_drivers_dir(mk);
2463 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
2468 EXPORT_SYMBOL(module_add_driver);
2470 void module_remove_driver(struct device_driver *drv)
2472 struct module_kobject *mk = NULL;
2478 sysfs_remove_link(&drv->kobj, "module");
2481 mk = &drv->owner->mkobj;
2482 else if (drv->mkobj)
2484 if (mk && mk->drivers_dir) {
2485 driver_name = make_driver_name(drv);
2487 sysfs_remove_link(mk->drivers_dir, driver_name);
2492 EXPORT_SYMBOL(module_remove_driver);
2495 #ifdef CONFIG_MODVERSIONS
2496 /* Generate the signature for struct module here, too, for modversions. */
2497 void struct_module(struct module *mod) { return; }
2498 EXPORT_SYMBOL(struct_module);