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 <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>
52 #define DEBUGP(fmt , a...)
55 #ifndef ARCH_SHF_SMALL
56 #define ARCH_SHF_SMALL 0
59 /* If this is set, the section belongs in the init part of the module */
60 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
62 /* Protects module list */
63 static DEFINE_SPINLOCK(modlist_lock);
65 /* List of modules, protected by module_mutex AND modlist_lock */
66 static DEFINE_MUTEX(module_mutex);
67 static LIST_HEAD(modules);
69 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
71 int register_module_notifier(struct notifier_block * nb)
73 return blocking_notifier_chain_register(&module_notify_list, nb);
75 EXPORT_SYMBOL(register_module_notifier);
77 int unregister_module_notifier(struct notifier_block * nb)
79 return blocking_notifier_chain_unregister(&module_notify_list, nb);
81 EXPORT_SYMBOL(unregister_module_notifier);
83 /* We require a truly strong try_module_get() */
84 static inline int strong_try_module_get(struct module *mod)
86 if (mod && mod->state == MODULE_STATE_COMING)
88 return try_module_get(mod);
91 /* A thread that wants to hold a reference to a module only while it
92 * is running can call ths to safely exit.
93 * nfsd and lockd use this.
95 void __module_put_and_exit(struct module *mod, long code)
100 EXPORT_SYMBOL(__module_put_and_exit);
102 /* Find a module section: 0 means not found. */
103 static unsigned int find_sec(Elf_Ehdr *hdr,
105 const char *secstrings,
110 for (i = 1; i < hdr->e_shnum; i++)
111 /* Alloc bit cleared means "ignore it." */
112 if ((sechdrs[i].sh_flags & SHF_ALLOC)
113 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
118 /* Provided by the linker */
119 extern const struct kernel_symbol __start___ksymtab[];
120 extern const struct kernel_symbol __stop___ksymtab[];
121 extern const struct kernel_symbol __start___ksymtab_gpl[];
122 extern const struct kernel_symbol __stop___ksymtab_gpl[];
123 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
124 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
125 extern const struct kernel_symbol __start___ksymtab_unused[];
126 extern const struct kernel_symbol __stop___ksymtab_unused[];
127 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
128 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
129 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
130 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
131 extern const unsigned long __start___kcrctab[];
132 extern const unsigned long __start___kcrctab_gpl[];
133 extern const unsigned long __start___kcrctab_gpl_future[];
134 extern const unsigned long __start___kcrctab_unused[];
135 extern const unsigned long __start___kcrctab_unused_gpl[];
137 #ifndef CONFIG_MODVERSIONS
138 #define symversion(base, idx) NULL
140 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
143 /* lookup symbol in given range of kernel_symbols */
144 static const struct kernel_symbol *lookup_symbol(const char *name,
145 const struct kernel_symbol *start,
146 const struct kernel_symbol *stop)
148 const struct kernel_symbol *ks = start;
149 for (; ks < stop; ks++)
150 if (strcmp(ks->name, name) == 0)
155 static void printk_unused_warning(const char *name)
157 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
158 "however this module is using it.\n", name);
159 printk(KERN_WARNING "This symbol will go away in the future.\n");
160 printk(KERN_WARNING "Please evalute if this is the right api to use, "
161 "and if it really is, submit a report the linux kernel "
162 "mailinglist together with submitting your code for "
166 /* Find a symbol, return value, crc and module which owns it */
167 static unsigned long __find_symbol(const char *name,
168 struct module **owner,
169 const unsigned long **crc,
173 const struct kernel_symbol *ks;
175 /* Core kernel first. */
177 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
179 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
183 ks = lookup_symbol(name, __start___ksymtab_gpl,
184 __stop___ksymtab_gpl);
186 *crc = symversion(__start___kcrctab_gpl,
187 (ks - __start___ksymtab_gpl));
191 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
192 __stop___ksymtab_gpl_future);
195 printk(KERN_WARNING "Symbol %s is being used "
196 "by a non-GPL module, which will not "
197 "be allowed in the future\n", name);
198 printk(KERN_WARNING "Please see the file "
199 "Documentation/feature-removal-schedule.txt "
200 "in the kernel source tree for more "
203 *crc = symversion(__start___kcrctab_gpl_future,
204 (ks - __start___ksymtab_gpl_future));
208 ks = lookup_symbol(name, __start___ksymtab_unused,
209 __stop___ksymtab_unused);
211 printk_unused_warning(name);
212 *crc = symversion(__start___kcrctab_unused,
213 (ks - __start___ksymtab_unused));
218 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
219 __stop___ksymtab_unused_gpl);
221 printk_unused_warning(name);
222 *crc = symversion(__start___kcrctab_unused_gpl,
223 (ks - __start___ksymtab_unused_gpl));
227 /* Now try modules. */
228 list_for_each_entry(mod, &modules, list) {
230 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
232 *crc = symversion(mod->crcs, (ks - mod->syms));
237 ks = lookup_symbol(name, mod->gpl_syms,
238 mod->gpl_syms + mod->num_gpl_syms);
240 *crc = symversion(mod->gpl_crcs,
241 (ks - mod->gpl_syms));
245 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
247 printk_unused_warning(name);
248 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
253 ks = lookup_symbol(name, mod->unused_gpl_syms,
254 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
256 printk_unused_warning(name);
257 *crc = symversion(mod->unused_gpl_crcs,
258 (ks - mod->unused_gpl_syms));
262 ks = lookup_symbol(name, mod->gpl_future_syms,
263 (mod->gpl_future_syms +
264 mod->num_gpl_future_syms));
267 printk(KERN_WARNING "Symbol %s is being used "
268 "by a non-GPL module, which will not "
269 "be allowed in the future\n", name);
270 printk(KERN_WARNING "Please see the file "
271 "Documentation/feature-removal-schedule.txt "
272 "in the kernel source tree for more "
275 *crc = symversion(mod->gpl_future_crcs,
276 (ks - mod->gpl_future_syms));
280 DEBUGP("Failed to find symbol %s\n", name);
284 /* Search for module by name: must hold module_mutex. */
285 static struct module *find_module(const char *name)
289 list_for_each_entry(mod, &modules, list) {
290 if (strcmp(mod->name, name) == 0)
297 /* Number of blocks used and allocated. */
298 static unsigned int pcpu_num_used, pcpu_num_allocated;
299 /* Size of each block. -ve means used. */
300 static int *pcpu_size;
302 static int split_block(unsigned int i, unsigned short size)
304 /* Reallocation required? */
305 if (pcpu_num_used + 1 > pcpu_num_allocated) {
306 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
311 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
312 pcpu_num_allocated *= 2;
317 /* Insert a new subblock */
318 memmove(&pcpu_size[i+1], &pcpu_size[i],
319 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
322 pcpu_size[i+1] -= size;
327 static inline unsigned int block_size(int val)
334 /* Created by linker magic */
335 extern char __per_cpu_start[], __per_cpu_end[];
337 static void *percpu_modalloc(unsigned long size, unsigned long align,
344 if (align > SMP_CACHE_BYTES) {
345 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
346 name, align, SMP_CACHE_BYTES);
347 align = SMP_CACHE_BYTES;
350 ptr = __per_cpu_start;
351 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
352 /* Extra for alignment requirement. */
353 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
354 BUG_ON(i == 0 && extra != 0);
356 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
359 /* Transfer extra to previous block. */
360 if (pcpu_size[i-1] < 0)
361 pcpu_size[i-1] -= extra;
363 pcpu_size[i-1] += extra;
364 pcpu_size[i] -= extra;
367 /* Split block if warranted */
368 if (pcpu_size[i] - size > sizeof(unsigned long))
369 if (!split_block(i, size))
373 pcpu_size[i] = -pcpu_size[i];
377 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
382 static void percpu_modfree(void *freeme)
385 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
387 /* First entry is core kernel percpu data. */
388 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
390 pcpu_size[i] = -pcpu_size[i];
397 /* Merge with previous? */
398 if (pcpu_size[i-1] >= 0) {
399 pcpu_size[i-1] += pcpu_size[i];
401 memmove(&pcpu_size[i], &pcpu_size[i+1],
402 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
405 /* Merge with next? */
406 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
407 pcpu_size[i] += pcpu_size[i+1];
409 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
410 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
414 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
416 const char *secstrings)
418 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
421 static int percpu_modinit(void)
424 pcpu_num_allocated = 2;
425 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
427 /* Static in-kernel percpu data (used). */
428 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
430 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
431 if (pcpu_size[1] < 0) {
432 printk(KERN_ERR "No per-cpu room for modules.\n");
438 __initcall(percpu_modinit);
439 #else /* ... !CONFIG_SMP */
440 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
445 static inline void percpu_modfree(void *pcpuptr)
449 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
451 const char *secstrings)
455 static inline void percpu_modcopy(void *pcpudst, const void *src,
458 /* pcpusec should be 0, and size of that section should be 0. */
461 #endif /* CONFIG_SMP */
463 #define MODINFO_ATTR(field) \
464 static void setup_modinfo_##field(struct module *mod, const char *s) \
466 mod->field = kstrdup(s, GFP_KERNEL); \
468 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
469 struct module *mod, char *buffer) \
471 return sprintf(buffer, "%s\n", mod->field); \
473 static int modinfo_##field##_exists(struct module *mod) \
475 return mod->field != NULL; \
477 static void free_modinfo_##field(struct module *mod) \
482 static struct module_attribute modinfo_##field = { \
483 .attr = { .name = __stringify(field), .mode = 0444, \
484 .owner = THIS_MODULE }, \
485 .show = show_modinfo_##field, \
486 .setup = setup_modinfo_##field, \
487 .test = modinfo_##field##_exists, \
488 .free = free_modinfo_##field, \
491 MODINFO_ATTR(version);
492 MODINFO_ATTR(srcversion);
494 #ifdef CONFIG_MODULE_UNLOAD
495 /* Init the unload section of the module. */
496 static void module_unload_init(struct module *mod)
500 INIT_LIST_HEAD(&mod->modules_which_use_me);
501 for (i = 0; i < NR_CPUS; i++)
502 local_set(&mod->ref[i].count, 0);
503 /* Hold reference count during initialization. */
504 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
505 /* Backwards compatibility macros put refcount during init. */
506 mod->waiter = current;
509 /* modules using other modules */
512 struct list_head list;
513 struct module *module_which_uses;
516 /* Does a already use b? */
517 static int already_uses(struct module *a, struct module *b)
519 struct module_use *use;
521 list_for_each_entry(use, &b->modules_which_use_me, list) {
522 if (use->module_which_uses == a) {
523 DEBUGP("%s uses %s!\n", a->name, b->name);
527 DEBUGP("%s does not use %s!\n", a->name, b->name);
531 /* Module a uses b */
532 static int use_module(struct module *a, struct module *b)
534 struct module_use *use;
535 if (b == NULL || already_uses(a, b)) return 1;
537 if (!strong_try_module_get(b))
540 DEBUGP("Allocating new usage for %s.\n", a->name);
541 use = kmalloc(sizeof(*use), GFP_ATOMIC);
543 printk("%s: out of memory loading\n", a->name);
548 use->module_which_uses = a;
549 list_add(&use->list, &b->modules_which_use_me);
553 /* Clear the unload stuff of the module. */
554 static void module_unload_free(struct module *mod)
558 list_for_each_entry(i, &modules, list) {
559 struct module_use *use;
561 list_for_each_entry(use, &i->modules_which_use_me, list) {
562 if (use->module_which_uses == mod) {
563 DEBUGP("%s unusing %s\n", mod->name, i->name);
565 list_del(&use->list);
567 /* There can be at most one match. */
574 #ifdef CONFIG_MODULE_FORCE_UNLOAD
575 static inline int try_force_unload(unsigned int flags)
577 int ret = (flags & O_TRUNC);
579 add_taint(TAINT_FORCED_RMMOD);
583 static inline int try_force_unload(unsigned int flags)
587 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
596 /* Whole machine is stopped with interrupts off when this runs. */
597 static int __try_stop_module(void *_sref)
599 struct stopref *sref = _sref;
601 /* If it's not unused, quit unless we are told to block. */
602 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
603 if (!(*sref->forced = try_force_unload(sref->flags)))
607 /* Mark it as dying. */
608 sref->mod->state = MODULE_STATE_GOING;
612 static int try_stop_module(struct module *mod, int flags, int *forced)
614 struct stopref sref = { mod, flags, forced };
616 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
619 unsigned int module_refcount(struct module *mod)
621 unsigned int i, total = 0;
623 for (i = 0; i < NR_CPUS; i++)
624 total += local_read(&mod->ref[i].count);
627 EXPORT_SYMBOL(module_refcount);
629 /* This exists whether we can unload or not */
630 static void free_module(struct module *mod);
632 static void wait_for_zero_refcount(struct module *mod)
634 /* Since we might sleep for some time, drop the semaphore first */
635 mutex_unlock(&module_mutex);
637 DEBUGP("Looking at refcount...\n");
638 set_current_state(TASK_UNINTERRUPTIBLE);
639 if (module_refcount(mod) == 0)
643 current->state = TASK_RUNNING;
644 mutex_lock(&module_mutex);
648 sys_delete_module(const char __user *name_user, unsigned int flags)
651 char name[MODULE_NAME_LEN];
654 if (!capable(CAP_SYS_MODULE))
657 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
659 name[MODULE_NAME_LEN-1] = '\0';
661 if (mutex_lock_interruptible(&module_mutex) != 0)
664 mod = find_module(name);
670 if (!list_empty(&mod->modules_which_use_me)) {
671 /* Other modules depend on us: get rid of them first. */
676 /* Doing init or already dying? */
677 if (mod->state != MODULE_STATE_LIVE) {
678 /* FIXME: if (force), slam module count and wake up
680 DEBUGP("%s already dying\n", mod->name);
685 /* If it has an init func, it must have an exit func to unload */
686 if ((mod->init != NULL && mod->exit == NULL)
688 forced = try_force_unload(flags);
690 /* This module can't be removed */
696 /* Set this up before setting mod->state */
697 mod->waiter = current;
699 /* Stop the machine so refcounts can't move and disable module. */
700 ret = try_stop_module(mod, flags, &forced);
704 /* Never wait if forced. */
705 if (!forced && module_refcount(mod) != 0)
706 wait_for_zero_refcount(mod);
708 /* Final destruction now noone is using it. */
709 if (mod->exit != NULL) {
710 mutex_unlock(&module_mutex);
712 mutex_lock(&module_mutex);
717 mutex_unlock(&module_mutex);
721 static void print_unload_info(struct seq_file *m, struct module *mod)
723 struct module_use *use;
724 int printed_something = 0;
726 seq_printf(m, " %u ", module_refcount(mod));
728 /* Always include a trailing , so userspace can differentiate
729 between this and the old multi-field proc format. */
730 list_for_each_entry(use, &mod->modules_which_use_me, list) {
731 printed_something = 1;
732 seq_printf(m, "%s,", use->module_which_uses->name);
736 printed_something = 1;
737 seq_printf(m, "[unsafe],");
740 if (mod->init != NULL && mod->exit == NULL) {
741 printed_something = 1;
742 seq_printf(m, "[permanent],");
745 if (!printed_something)
749 void __symbol_put(const char *symbol)
751 struct module *owner;
753 const unsigned long *crc;
755 spin_lock_irqsave(&modlist_lock, flags);
756 if (!__find_symbol(symbol, &owner, &crc, 1))
759 spin_unlock_irqrestore(&modlist_lock, flags);
761 EXPORT_SYMBOL(__symbol_put);
763 void symbol_put_addr(void *addr)
765 struct module *modaddr;
767 if (core_kernel_text((unsigned long)addr))
770 if (!(modaddr = module_text_address((unsigned long)addr)))
774 EXPORT_SYMBOL_GPL(symbol_put_addr);
776 static ssize_t show_refcnt(struct module_attribute *mattr,
777 struct module *mod, char *buffer)
779 /* sysfs holds a reference */
780 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
783 static struct module_attribute refcnt = {
784 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
788 #else /* !CONFIG_MODULE_UNLOAD */
789 static void print_unload_info(struct seq_file *m, struct module *mod)
791 /* We don't know the usage count, or what modules are using. */
792 seq_printf(m, " - -");
795 static inline void module_unload_free(struct module *mod)
799 static inline int use_module(struct module *a, struct module *b)
801 return strong_try_module_get(b);
804 static inline void module_unload_init(struct module *mod)
807 #endif /* CONFIG_MODULE_UNLOAD */
809 static struct module_attribute *modinfo_attrs[] = {
812 #ifdef CONFIG_MODULE_UNLOAD
818 static const char vermagic[] = VERMAGIC_STRING;
820 #ifdef CONFIG_MODVERSIONS
821 static int check_version(Elf_Shdr *sechdrs,
822 unsigned int versindex,
825 const unsigned long *crc)
827 unsigned int i, num_versions;
828 struct modversion_info *versions;
830 /* Exporting module didn't supply crcs? OK, we're already tainted. */
834 versions = (void *) sechdrs[versindex].sh_addr;
835 num_versions = sechdrs[versindex].sh_size
836 / sizeof(struct modversion_info);
838 for (i = 0; i < num_versions; i++) {
839 if (strcmp(versions[i].name, symname) != 0)
842 if (versions[i].crc == *crc)
844 printk("%s: disagrees about version of symbol %s\n",
846 DEBUGP("Found checksum %lX vs module %lX\n",
847 *crc, versions[i].crc);
850 /* Not in module's version table. OK, but that taints the kernel. */
851 if (!(tainted & TAINT_FORCED_MODULE)) {
852 printk("%s: no version for \"%s\" found: kernel tainted.\n",
854 add_taint(TAINT_FORCED_MODULE);
859 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
860 unsigned int versindex,
863 const unsigned long *crc;
864 struct module *owner;
866 if (!__find_symbol("struct_module", &owner, &crc, 1))
868 return check_version(sechdrs, versindex, "struct_module", mod,
872 /* First part is kernel version, which we ignore. */
873 static inline int same_magic(const char *amagic, const char *bmagic)
875 amagic += strcspn(amagic, " ");
876 bmagic += strcspn(bmagic, " ");
877 return strcmp(amagic, bmagic) == 0;
880 static inline int check_version(Elf_Shdr *sechdrs,
881 unsigned int versindex,
884 const unsigned long *crc)
889 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
890 unsigned int versindex,
896 static inline int same_magic(const char *amagic, const char *bmagic)
898 return strcmp(amagic, bmagic) == 0;
900 #endif /* CONFIG_MODVERSIONS */
902 /* Resolve a symbol for this module. I.e. if we find one, record usage.
903 Must be holding module_mutex. */
904 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
905 unsigned int versindex,
909 struct module *owner;
911 const unsigned long *crc;
913 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
915 /* use_module can fail due to OOM, or module unloading */
916 if (!check_version(sechdrs, versindex, name, mod, crc) ||
917 !use_module(mod, owner))
925 * /sys/module/foo/sections stuff
926 * J. Corbet <corbet@lwn.net>
928 #ifdef CONFIG_KALLSYMS
929 static ssize_t module_sect_show(struct module_attribute *mattr,
930 struct module *mod, char *buf)
932 struct module_sect_attr *sattr =
933 container_of(mattr, struct module_sect_attr, mattr);
934 return sprintf(buf, "0x%lx\n", sattr->address);
937 static void add_sect_attrs(struct module *mod, unsigned int nsect,
938 char *secstrings, Elf_Shdr *sechdrs)
940 unsigned int nloaded = 0, i, size[2];
941 struct module_sect_attrs *sect_attrs;
942 struct module_sect_attr *sattr;
943 struct attribute **gattr;
945 /* Count loaded sections and allocate structures */
946 for (i = 0; i < nsect; i++)
947 if (sechdrs[i].sh_flags & SHF_ALLOC)
949 size[0] = ALIGN(sizeof(*sect_attrs)
950 + nloaded * sizeof(sect_attrs->attrs[0]),
951 sizeof(sect_attrs->grp.attrs[0]));
952 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
953 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
956 /* Setup section attributes. */
957 sect_attrs->grp.name = "sections";
958 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
960 sattr = §_attrs->attrs[0];
961 gattr = §_attrs->grp.attrs[0];
962 for (i = 0; i < nsect; i++) {
963 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
965 sattr->address = sechdrs[i].sh_addr;
966 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
967 MODULE_SECT_NAME_LEN);
968 sattr->mattr.show = module_sect_show;
969 sattr->mattr.store = NULL;
970 sattr->mattr.attr.name = sattr->name;
971 sattr->mattr.attr.owner = mod;
972 sattr->mattr.attr.mode = S_IRUGO;
973 *(gattr++) = &(sattr++)->mattr.attr;
977 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
980 mod->sect_attrs = sect_attrs;
986 static void remove_sect_attrs(struct module *mod)
988 if (mod->sect_attrs) {
989 sysfs_remove_group(&mod->mkobj.kobj,
990 &mod->sect_attrs->grp);
991 /* We are positive that no one is using any sect attrs
992 * at this point. Deallocate immediately. */
993 kfree(mod->sect_attrs);
994 mod->sect_attrs = NULL;
1000 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1001 char *sectstrings, Elf_Shdr *sechdrs)
1005 static inline void remove_sect_attrs(struct module *mod)
1008 #endif /* CONFIG_KALLSYMS */
1010 static int module_add_modinfo_attrs(struct module *mod)
1012 struct module_attribute *attr;
1013 struct module_attribute *temp_attr;
1017 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1018 (ARRAY_SIZE(modinfo_attrs) + 1)),
1020 if (!mod->modinfo_attrs)
1023 temp_attr = mod->modinfo_attrs;
1024 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1026 (attr->test && attr->test(mod))) {
1027 memcpy(temp_attr, attr, sizeof(*temp_attr));
1028 temp_attr->attr.owner = mod;
1029 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1036 static void module_remove_modinfo_attrs(struct module *mod)
1038 struct module_attribute *attr;
1041 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1042 /* pick a field to test for end of list */
1043 if (!attr->attr.name)
1045 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1049 kfree(mod->modinfo_attrs);
1052 static int mod_sysfs_setup(struct module *mod,
1053 struct kernel_param *kparam,
1054 unsigned int num_params)
1058 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1059 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1062 kobj_set_kset_s(&mod->mkobj, module_subsys);
1063 mod->mkobj.mod = mod;
1064 err = kobject_register(&mod->mkobj.kobj);
1068 err = module_param_sysfs_setup(mod, kparam, num_params);
1072 err = module_add_modinfo_attrs(mod);
1079 kobject_unregister(&mod->mkobj.kobj);
1084 static void mod_kobject_remove(struct module *mod)
1086 module_remove_modinfo_attrs(mod);
1087 module_param_sysfs_remove(mod);
1089 kobject_unregister(&mod->mkobj.kobj);
1093 * unlink the module with the whole machine is stopped with interrupts off
1094 * - this defends against kallsyms not taking locks
1096 static int __unlink_module(void *_mod)
1098 struct module *mod = _mod;
1099 list_del(&mod->list);
1103 /* Free a module, remove from lists, etc (must hold module mutex). */
1104 static void free_module(struct module *mod)
1106 /* Delete from various lists */
1107 stop_machine_run(__unlink_module, mod, NR_CPUS);
1108 remove_sect_attrs(mod);
1109 mod_kobject_remove(mod);
1111 unwind_remove_table(mod->unwind_info, 0);
1113 /* Arch-specific cleanup. */
1114 module_arch_cleanup(mod);
1116 /* Module unload stuff */
1117 module_unload_free(mod);
1119 /* This may be NULL, but that's OK */
1120 module_free(mod, mod->module_init);
1123 percpu_modfree(mod->percpu);
1125 /* Finally, free the core (containing the module structure) */
1126 module_free(mod, mod->module_core);
1129 void *__symbol_get(const char *symbol)
1131 struct module *owner;
1132 unsigned long value, flags;
1133 const unsigned long *crc;
1135 spin_lock_irqsave(&modlist_lock, flags);
1136 value = __find_symbol(symbol, &owner, &crc, 1);
1137 if (value && !strong_try_module_get(owner))
1139 spin_unlock_irqrestore(&modlist_lock, flags);
1141 return (void *)value;
1143 EXPORT_SYMBOL_GPL(__symbol_get);
1146 * Ensure that an exported symbol [global namespace] does not already exist
1147 * in the Kernel or in some other modules exported symbol table.
1149 static int verify_export_symbols(struct module *mod)
1151 const char *name = NULL;
1152 unsigned long i, ret = 0;
1153 struct module *owner;
1154 const unsigned long *crc;
1156 for (i = 0; i < mod->num_syms; i++)
1157 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
1158 name = mod->syms[i].name;
1163 for (i = 0; i < mod->num_gpl_syms; i++)
1164 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
1165 name = mod->gpl_syms[i].name;
1172 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1173 mod->name, name, module_name(owner));
1178 /* Change all symbols so that sh_value encodes the pointer directly. */
1179 static int simplify_symbols(Elf_Shdr *sechdrs,
1180 unsigned int symindex,
1182 unsigned int versindex,
1183 unsigned int pcpuindex,
1186 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1187 unsigned long secbase;
1188 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1191 for (i = 1; i < n; i++) {
1192 switch (sym[i].st_shndx) {
1194 /* We compiled with -fno-common. These are not
1195 supposed to happen. */
1196 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1197 printk("%s: please compile with -fno-common\n",
1203 /* Don't need to do anything */
1204 DEBUGP("Absolute symbol: 0x%08lx\n",
1205 (long)sym[i].st_value);
1210 = resolve_symbol(sechdrs, versindex,
1211 strtab + sym[i].st_name, mod);
1213 /* Ok if resolved. */
1214 if (sym[i].st_value != 0)
1217 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1220 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1221 mod->name, strtab + sym[i].st_name);
1226 /* Divert to percpu allocation if a percpu var. */
1227 if (sym[i].st_shndx == pcpuindex)
1228 secbase = (unsigned long)mod->percpu;
1230 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1231 sym[i].st_value += secbase;
1239 /* Update size with this section: return offset. */
1240 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1244 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1245 *size = ret + sechdr->sh_size;
1249 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1250 might -- code, read-only data, read-write data, small data. Tally
1251 sizes, and place the offsets into sh_entsize fields: high bit means it
1253 static void layout_sections(struct module *mod,
1254 const Elf_Ehdr *hdr,
1256 const char *secstrings)
1258 static unsigned long const masks[][2] = {
1259 /* NOTE: all executable code must be the first section
1260 * in this array; otherwise modify the text_size
1261 * finder in the two loops below */
1262 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1263 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1264 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1265 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1269 for (i = 0; i < hdr->e_shnum; i++)
1270 sechdrs[i].sh_entsize = ~0UL;
1272 DEBUGP("Core section allocation order:\n");
1273 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1274 for (i = 0; i < hdr->e_shnum; ++i) {
1275 Elf_Shdr *s = &sechdrs[i];
1277 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1278 || (s->sh_flags & masks[m][1])
1279 || s->sh_entsize != ~0UL
1280 || strncmp(secstrings + s->sh_name,
1283 s->sh_entsize = get_offset(&mod->core_size, s);
1284 DEBUGP("\t%s\n", secstrings + s->sh_name);
1287 mod->core_text_size = mod->core_size;
1290 DEBUGP("Init section allocation order:\n");
1291 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1292 for (i = 0; i < hdr->e_shnum; ++i) {
1293 Elf_Shdr *s = &sechdrs[i];
1295 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1296 || (s->sh_flags & masks[m][1])
1297 || s->sh_entsize != ~0UL
1298 || strncmp(secstrings + s->sh_name,
1301 s->sh_entsize = (get_offset(&mod->init_size, s)
1302 | INIT_OFFSET_MASK);
1303 DEBUGP("\t%s\n", secstrings + s->sh_name);
1306 mod->init_text_size = mod->init_size;
1310 static void set_license(struct module *mod, const char *license)
1313 license = "unspecified";
1315 mod->license_gplok = license_is_gpl_compatible(license);
1316 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1317 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1318 mod->name, license);
1319 add_taint(TAINT_PROPRIETARY_MODULE);
1323 /* Parse tag=value strings from .modinfo section */
1324 static char *next_string(char *string, unsigned long *secsize)
1326 /* Skip non-zero chars */
1329 if ((*secsize)-- <= 1)
1333 /* Skip any zero padding. */
1334 while (!string[0]) {
1336 if ((*secsize)-- <= 1)
1342 static char *get_modinfo(Elf_Shdr *sechdrs,
1347 unsigned int taglen = strlen(tag);
1348 unsigned long size = sechdrs[info].sh_size;
1350 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1351 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1352 return p + taglen + 1;
1357 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1358 unsigned int infoindex)
1360 struct module_attribute *attr;
1363 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1366 get_modinfo(sechdrs,
1372 #ifdef CONFIG_KALLSYMS
1373 int is_exported(const char *name, const struct module *mod)
1375 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1378 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1385 static char elf_type(const Elf_Sym *sym,
1387 const char *secstrings,
1390 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1391 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1396 if (sym->st_shndx == SHN_UNDEF)
1398 if (sym->st_shndx == SHN_ABS)
1400 if (sym->st_shndx >= SHN_LORESERVE)
1402 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1404 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1405 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1406 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1408 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1413 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1414 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1419 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1420 ".debug", strlen(".debug")) == 0)
1425 static void add_kallsyms(struct module *mod,
1427 unsigned int symindex,
1428 unsigned int strindex,
1429 const char *secstrings)
1433 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1434 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1435 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1437 /* Set types up while we still have access to sections. */
1438 for (i = 0; i < mod->num_symtab; i++)
1439 mod->symtab[i].st_info
1440 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1443 static inline void add_kallsyms(struct module *mod,
1445 unsigned int symindex,
1446 unsigned int strindex,
1447 const char *secstrings)
1450 #endif /* CONFIG_KALLSYMS */
1452 /* Allocate and load the module: note that size of section 0 is always
1453 zero, and we rely on this for optional sections. */
1454 static struct module *load_module(void __user *umod,
1456 const char __user *uargs)
1460 char *secstrings, *args, *modmagic, *strtab = NULL;
1462 unsigned int symindex = 0;
1463 unsigned int strindex = 0;
1464 unsigned int setupindex;
1465 unsigned int exindex;
1466 unsigned int exportindex;
1467 unsigned int modindex;
1468 unsigned int obsparmindex;
1469 unsigned int infoindex;
1470 unsigned int gplindex;
1471 unsigned int crcindex;
1472 unsigned int gplcrcindex;
1473 unsigned int versindex;
1474 unsigned int pcpuindex;
1475 unsigned int gplfutureindex;
1476 unsigned int gplfuturecrcindex;
1477 unsigned int unwindex = 0;
1478 unsigned int unusedindex;
1479 unsigned int unusedcrcindex;
1480 unsigned int unusedgplindex;
1481 unsigned int unusedgplcrcindex;
1484 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1485 struct exception_table_entry *extable;
1486 mm_segment_t old_fs;
1488 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1490 if (len < sizeof(*hdr))
1491 return ERR_PTR(-ENOEXEC);
1493 /* Suck in entire file: we'll want most of it. */
1494 /* vmalloc barfs on "unusual" numbers. Check here */
1495 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1496 return ERR_PTR(-ENOMEM);
1497 if (copy_from_user(hdr, umod, len) != 0) {
1502 /* Sanity checks against insmoding binaries or wrong arch,
1503 weird elf version */
1504 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1505 || hdr->e_type != ET_REL
1506 || !elf_check_arch(hdr)
1507 || hdr->e_shentsize != sizeof(*sechdrs)) {
1512 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1515 /* Convenience variables */
1516 sechdrs = (void *)hdr + hdr->e_shoff;
1517 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1518 sechdrs[0].sh_addr = 0;
1520 for (i = 1; i < hdr->e_shnum; i++) {
1521 if (sechdrs[i].sh_type != SHT_NOBITS
1522 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1525 /* Mark all sections sh_addr with their address in the
1527 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1529 /* Internal symbols and strings. */
1530 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1532 strindex = sechdrs[i].sh_link;
1533 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1535 #ifndef CONFIG_MODULE_UNLOAD
1536 /* Don't load .exit sections */
1537 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1538 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1542 modindex = find_sec(hdr, sechdrs, secstrings,
1543 ".gnu.linkonce.this_module");
1545 printk(KERN_WARNING "No module found in object\n");
1549 mod = (void *)sechdrs[modindex].sh_addr;
1551 if (symindex == 0) {
1552 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1558 /* Optional sections */
1559 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1560 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1561 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
1562 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1563 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
1564 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1565 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1566 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
1567 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1568 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
1569 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1570 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1571 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1572 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1573 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1574 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1575 #ifdef ARCH_UNWIND_SECTION_NAME
1576 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1579 /* Don't keep modinfo section */
1580 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1581 #ifdef CONFIG_KALLSYMS
1582 /* Keep symbol and string tables for decoding later. */
1583 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1584 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1587 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1589 /* Check module struct version now, before we try to use module. */
1590 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1595 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1596 /* This is allowed: modprobe --force will invalidate it. */
1598 add_taint(TAINT_FORCED_MODULE);
1599 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1601 } else if (!same_magic(modmagic, vermagic)) {
1602 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1603 mod->name, modmagic, vermagic);
1608 /* Now copy in args */
1609 args = strndup_user(uargs, ~0UL >> 1);
1611 err = PTR_ERR(args);
1615 if (find_module(mod->name)) {
1620 mod->state = MODULE_STATE_COMING;
1622 /* Allow arches to frob section contents and sizes. */
1623 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1628 /* We have a special allocation for this section. */
1629 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1630 sechdrs[pcpuindex].sh_addralign,
1636 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1637 mod->percpu = percpu;
1640 /* Determine total sizes, and put offsets in sh_entsize. For now
1641 this is done generically; there doesn't appear to be any
1642 special cases for the architectures. */
1643 layout_sections(mod, hdr, sechdrs, secstrings);
1645 /* Do the allocs. */
1646 ptr = module_alloc(mod->core_size);
1651 memset(ptr, 0, mod->core_size);
1652 mod->module_core = ptr;
1654 ptr = module_alloc(mod->init_size);
1655 if (!ptr && mod->init_size) {
1659 memset(ptr, 0, mod->init_size);
1660 mod->module_init = ptr;
1662 /* Transfer each section which specifies SHF_ALLOC */
1663 DEBUGP("final section addresses:\n");
1664 for (i = 0; i < hdr->e_shnum; i++) {
1667 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1670 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1671 dest = mod->module_init
1672 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1674 dest = mod->module_core + sechdrs[i].sh_entsize;
1676 if (sechdrs[i].sh_type != SHT_NOBITS)
1677 memcpy(dest, (void *)sechdrs[i].sh_addr,
1678 sechdrs[i].sh_size);
1679 /* Update sh_addr to point to copy in image. */
1680 sechdrs[i].sh_addr = (unsigned long)dest;
1681 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1683 /* Module has been moved. */
1684 mod = (void *)sechdrs[modindex].sh_addr;
1686 /* Now we've moved module, initialize linked lists, etc. */
1687 module_unload_init(mod);
1689 /* Set up license info based on the info section */
1690 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1692 if (strcmp(mod->name, "ndiswrapper") == 0)
1693 add_taint(TAINT_PROPRIETARY_MODULE);
1694 if (strcmp(mod->name, "driverloader") == 0)
1695 add_taint(TAINT_PROPRIETARY_MODULE);
1697 /* Set up MODINFO_ATTR fields */
1698 setup_modinfo(mod, sechdrs, infoindex);
1700 /* Fix up syms, so that st_value is a pointer to location. */
1701 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1706 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1707 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1708 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1710 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1711 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1712 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1714 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1715 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1716 sizeof(*mod->gpl_future_syms);
1717 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
1718 sizeof(*mod->unused_syms);
1719 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
1720 sizeof(*mod->unused_gpl_syms);
1721 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1722 if (gplfuturecrcindex)
1723 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
1725 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
1727 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
1728 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
1729 if (unusedgplcrcindex)
1730 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
1732 #ifdef CONFIG_MODVERSIONS
1733 if ((mod->num_syms && !crcindex) ||
1734 (mod->num_gpl_syms && !gplcrcindex) ||
1735 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
1736 (mod->num_unused_syms && !unusedcrcindex) ||
1737 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
1738 printk(KERN_WARNING "%s: No versions for exported symbols."
1739 " Tainting kernel.\n", mod->name);
1740 add_taint(TAINT_FORCED_MODULE);
1744 /* Now do relocations. */
1745 for (i = 1; i < hdr->e_shnum; i++) {
1746 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1747 unsigned int info = sechdrs[i].sh_info;
1749 /* Not a valid relocation section? */
1750 if (info >= hdr->e_shnum)
1753 /* Don't bother with non-allocated sections */
1754 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1757 if (sechdrs[i].sh_type == SHT_REL)
1758 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1759 else if (sechdrs[i].sh_type == SHT_RELA)
1760 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1766 /* Find duplicate symbols */
1767 err = verify_export_symbols(mod);
1772 /* Set up and sort exception table */
1773 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1774 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1775 sort_extable(extable, extable + mod->num_exentries);
1777 /* Finally, copy percpu area over. */
1778 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1779 sechdrs[pcpuindex].sh_size);
1781 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1783 err = module_finalize(hdr, sechdrs, mod);
1787 /* flush the icache in correct context */
1792 * Flush the instruction cache, since we've played with text.
1793 * Do it before processing of module parameters, so the module
1794 * can provide parameter accessor functions of its own.
1796 if (mod->module_init)
1797 flush_icache_range((unsigned long)mod->module_init,
1798 (unsigned long)mod->module_init
1800 flush_icache_range((unsigned long)mod->module_core,
1801 (unsigned long)mod->module_core + mod->core_size);
1807 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
1810 /* Size of section 0 is 0, so this works well if no params */
1811 err = parse_args(mod->name, mod->args,
1812 (struct kernel_param *)
1813 sechdrs[setupindex].sh_addr,
1814 sechdrs[setupindex].sh_size
1815 / sizeof(struct kernel_param),
1820 err = mod_sysfs_setup(mod,
1821 (struct kernel_param *)
1822 sechdrs[setupindex].sh_addr,
1823 sechdrs[setupindex].sh_size
1824 / sizeof(struct kernel_param));
1827 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1829 /* Size of section 0 is 0, so this works well if no unwind info. */
1830 mod->unwind_info = unwind_add_table(mod,
1831 (void *)sechdrs[unwindex].sh_addr,
1832 sechdrs[unwindex].sh_size);
1834 /* Get rid of temporary copy */
1841 module_arch_cleanup(mod);
1843 module_unload_free(mod);
1844 module_free(mod, mod->module_init);
1846 module_free(mod, mod->module_core);
1849 percpu_modfree(percpu);
1854 return ERR_PTR(err);
1857 printk(KERN_ERR "Module len %lu truncated\n", len);
1863 * link the module with the whole machine is stopped with interrupts off
1864 * - this defends against kallsyms not taking locks
1866 static int __link_module(void *_mod)
1868 struct module *mod = _mod;
1869 list_add(&mod->list, &modules);
1873 /* This is where the real work happens */
1875 sys_init_module(void __user *umod,
1877 const char __user *uargs)
1882 /* Must have permission */
1883 if (!capable(CAP_SYS_MODULE))
1886 /* Only one module load at a time, please */
1887 if (mutex_lock_interruptible(&module_mutex) != 0)
1890 /* Do all the hard work */
1891 mod = load_module(umod, len, uargs);
1893 mutex_unlock(&module_mutex);
1894 return PTR_ERR(mod);
1897 /* Now sew it into the lists. They won't access us, since
1898 strong_try_module_get() will fail. */
1899 stop_machine_run(__link_module, mod, NR_CPUS);
1901 /* Drop lock so they can recurse */
1902 mutex_unlock(&module_mutex);
1904 blocking_notifier_call_chain(&module_notify_list,
1905 MODULE_STATE_COMING, mod);
1907 /* Start the module */
1908 if (mod->init != NULL)
1911 /* Init routine failed: abort. Try to protect us from
1912 buggy refcounters. */
1913 mod->state = MODULE_STATE_GOING;
1914 synchronize_sched();
1916 printk(KERN_ERR "%s: module is now stuck!\n",
1920 mutex_lock(&module_mutex);
1922 mutex_unlock(&module_mutex);
1927 /* Now it's a first class citizen! */
1928 mutex_lock(&module_mutex);
1929 mod->state = MODULE_STATE_LIVE;
1930 /* Drop initial reference. */
1932 unwind_remove_table(mod->unwind_info, 1);
1933 module_free(mod, mod->module_init);
1934 mod->module_init = NULL;
1936 mod->init_text_size = 0;
1937 mutex_unlock(&module_mutex);
1942 static inline int within(unsigned long addr, void *start, unsigned long size)
1944 return ((void *)addr >= start && (void *)addr < start + size);
1947 #ifdef CONFIG_KALLSYMS
1949 * This ignores the intensely annoying "mapping symbols" found
1950 * in ARM ELF files: $a, $t and $d.
1952 static inline int is_arm_mapping_symbol(const char *str)
1954 return str[0] == '$' && strchr("atd", str[1])
1955 && (str[2] == '\0' || str[2] == '.');
1958 static const char *get_ksymbol(struct module *mod,
1960 unsigned long *size,
1961 unsigned long *offset)
1963 unsigned int i, best = 0;
1964 unsigned long nextval;
1966 /* At worse, next value is at end of module */
1967 if (within(addr, mod->module_init, mod->init_size))
1968 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1970 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1972 /* Scan for closest preceeding symbol, and next symbol. (ELF
1973 starts real symbols at 1). */
1974 for (i = 1; i < mod->num_symtab; i++) {
1975 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1978 /* We ignore unnamed symbols: they're uninformative
1979 * and inserted at a whim. */
1980 if (mod->symtab[i].st_value <= addr
1981 && mod->symtab[i].st_value > mod->symtab[best].st_value
1982 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1983 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1985 if (mod->symtab[i].st_value > addr
1986 && mod->symtab[i].st_value < nextval
1987 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1988 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1989 nextval = mod->symtab[i].st_value;
1995 *size = nextval - mod->symtab[best].st_value;
1996 *offset = addr - mod->symtab[best].st_value;
1997 return mod->strtab + mod->symtab[best].st_name;
2000 /* For kallsyms to ask for address resolution. NULL means not found.
2001 We don't lock, as this is used for oops resolution and races are a
2003 const char *module_address_lookup(unsigned long addr,
2004 unsigned long *size,
2005 unsigned long *offset,
2010 list_for_each_entry(mod, &modules, list) {
2011 if (within(addr, mod->module_init, mod->init_size)
2012 || within(addr, mod->module_core, mod->core_size)) {
2013 *modname = mod->name;
2014 return get_ksymbol(mod, addr, size, offset);
2020 struct module *module_get_kallsym(unsigned int symnum,
2021 unsigned long *value,
2027 mutex_lock(&module_mutex);
2028 list_for_each_entry(mod, &modules, list) {
2029 if (symnum < mod->num_symtab) {
2030 *value = mod->symtab[symnum].st_value;
2031 *type = mod->symtab[symnum].st_info;
2033 mod->strtab + mod->symtab[symnum].st_name,
2035 mutex_unlock(&module_mutex);
2038 symnum -= mod->num_symtab;
2040 mutex_unlock(&module_mutex);
2044 static unsigned long mod_find_symname(struct module *mod, const char *name)
2048 for (i = 0; i < mod->num_symtab; i++)
2049 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2050 mod->symtab[i].st_info != 'U')
2051 return mod->symtab[i].st_value;
2055 /* Look for this name: can be of form module:name. */
2056 unsigned long module_kallsyms_lookup_name(const char *name)
2060 unsigned long ret = 0;
2062 /* Don't lock: we're in enough trouble already. */
2063 if ((colon = strchr(name, ':')) != NULL) {
2065 if ((mod = find_module(name)) != NULL)
2066 ret = mod_find_symname(mod, colon+1);
2069 list_for_each_entry(mod, &modules, list)
2070 if ((ret = mod_find_symname(mod, name)) != 0)
2075 #endif /* CONFIG_KALLSYMS */
2077 /* Called by the /proc file system to return a list of modules. */
2078 static void *m_start(struct seq_file *m, loff_t *pos)
2080 struct list_head *i;
2083 mutex_lock(&module_mutex);
2084 list_for_each(i, &modules) {
2093 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2095 struct list_head *i = p;
2097 if (i->next == &modules)
2102 static void m_stop(struct seq_file *m, void *p)
2104 mutex_unlock(&module_mutex);
2107 static int m_show(struct seq_file *m, void *p)
2109 struct module *mod = list_entry(p, struct module, list);
2110 seq_printf(m, "%s %lu",
2111 mod->name, mod->init_size + mod->core_size);
2112 print_unload_info(m, mod);
2114 /* Informative for users. */
2115 seq_printf(m, " %s",
2116 mod->state == MODULE_STATE_GOING ? "Unloading":
2117 mod->state == MODULE_STATE_COMING ? "Loading":
2119 /* Used by oprofile and other similar tools. */
2120 seq_printf(m, " 0x%p", mod->module_core);
2122 seq_printf(m, "\n");
2126 /* Format: modulename size refcount deps address
2128 Where refcount is a number or -, and deps is a comma-separated list
2131 struct seq_operations modules_op = {
2138 /* Given an address, look for it in the module exception tables. */
2139 const struct exception_table_entry *search_module_extables(unsigned long addr)
2141 unsigned long flags;
2142 const struct exception_table_entry *e = NULL;
2145 spin_lock_irqsave(&modlist_lock, flags);
2146 list_for_each_entry(mod, &modules, list) {
2147 if (mod->num_exentries == 0)
2150 e = search_extable(mod->extable,
2151 mod->extable + mod->num_exentries - 1,
2156 spin_unlock_irqrestore(&modlist_lock, flags);
2158 /* Now, if we found one, we are running inside it now, hence
2159 we cannot unload the module, hence no refcnt needed. */
2163 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2164 struct module *__module_text_address(unsigned long addr)
2168 list_for_each_entry(mod, &modules, list)
2169 if (within(addr, mod->module_init, mod->init_text_size)
2170 || within(addr, mod->module_core, mod->core_text_size))
2175 struct module *module_text_address(unsigned long addr)
2178 unsigned long flags;
2180 spin_lock_irqsave(&modlist_lock, flags);
2181 mod = __module_text_address(addr);
2182 spin_unlock_irqrestore(&modlist_lock, flags);
2187 /* Don't grab lock, we're oopsing. */
2188 void print_modules(void)
2192 printk("Modules linked in:");
2193 list_for_each_entry(mod, &modules, list)
2194 printk(" %s", mod->name);
2198 void module_add_driver(struct module *mod, struct device_driver *drv)
2203 /* Don't check return code; this call is idempotent */
2204 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2206 EXPORT_SYMBOL(module_add_driver);
2208 void module_remove_driver(struct device_driver *drv)
2212 sysfs_remove_link(&drv->kobj, "module");
2214 EXPORT_SYMBOL(module_remove_driver);
2216 #ifdef CONFIG_MODVERSIONS
2217 /* Generate the signature for struct module here, too, for modversions. */
2218 void struct_module(struct module *mod) { return; }
2219 EXPORT_SYMBOL(struct_module);