1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/elf.h>
26 #include <linux/seq_file.h>
27 #include <linux/syscalls.h>
28 #include <linux/fcntl.h>
29 #include <linux/rcupdate.h>
30 #include <linux/cpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/errno.h>
33 #include <linux/err.h>
34 #include <linux/vermagic.h>
35 #include <linux/notifier.h>
36 #include <linux/stop_machine.h>
37 #include <linux/device.h>
38 #include <linux/string.h>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
41 #include <asm/cacheflush.h>
46 #define DEBUGP(fmt , a...)
49 #ifndef ARCH_SHF_SMALL
50 #define ARCH_SHF_SMALL 0
53 /* If this is set, the section belongs in the init part of the module */
54 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
56 /* Protects module list */
57 static DEFINE_SPINLOCK(modlist_lock);
59 /* List of modules, protected by module_mutex AND modlist_lock */
60 static DECLARE_MUTEX(module_mutex);
61 static LIST_HEAD(modules);
63 static DECLARE_MUTEX(notify_mutex);
64 static struct notifier_block * module_notify_list;
66 int register_module_notifier(struct notifier_block * nb)
70 err = notifier_chain_register(&module_notify_list, nb);
74 EXPORT_SYMBOL(register_module_notifier);
76 int unregister_module_notifier(struct notifier_block * nb)
80 err = notifier_chain_unregister(&module_notify_list, nb);
84 EXPORT_SYMBOL(unregister_module_notifier);
86 /* We require a truly strong try_module_get() */
87 static inline int strong_try_module_get(struct module *mod)
89 if (mod && mod->state == MODULE_STATE_COMING)
91 return try_module_get(mod);
94 /* A thread that wants to hold a reference to a module only while it
95 * is running can call ths to safely exit.
96 * nfsd and lockd use this.
98 void __module_put_and_exit(struct module *mod, long code)
103 EXPORT_SYMBOL(__module_put_and_exit);
105 /* Find a module section: 0 means not found. */
106 static unsigned int find_sec(Elf_Ehdr *hdr,
108 const char *secstrings,
113 for (i = 1; i < hdr->e_shnum; i++)
114 /* Alloc bit cleared means "ignore it." */
115 if ((sechdrs[i].sh_flags & SHF_ALLOC)
116 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
121 /* Provided by the linker */
122 extern const struct kernel_symbol __start___ksymtab[];
123 extern const struct kernel_symbol __stop___ksymtab[];
124 extern const struct kernel_symbol __start___ksymtab_gpl[];
125 extern const struct kernel_symbol __stop___ksymtab_gpl[];
126 extern const unsigned long __start___kcrctab[];
127 extern const unsigned long __start___kcrctab_gpl[];
129 #ifndef CONFIG_MODVERSIONS
130 #define symversion(base, idx) NULL
132 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
135 /* Find a symbol, return value, crc and module which owns it */
136 static unsigned long __find_symbol(const char *name,
137 struct module **owner,
138 const unsigned long **crc,
144 /* Core kernel first. */
146 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
147 if (strcmp(__start___ksymtab[i].name, name) == 0) {
148 *crc = symversion(__start___kcrctab, i);
149 return __start___ksymtab[i].value;
153 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
154 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
155 *crc = symversion(__start___kcrctab_gpl, i);
156 return __start___ksymtab_gpl[i].value;
160 /* Now try modules. */
161 list_for_each_entry(mod, &modules, list) {
163 for (i = 0; i < mod->num_syms; i++)
164 if (strcmp(mod->syms[i].name, name) == 0) {
165 *crc = symversion(mod->crcs, i);
166 return mod->syms[i].value;
170 for (i = 0; i < mod->num_gpl_syms; i++) {
171 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
172 *crc = symversion(mod->gpl_crcs, i);
173 return mod->gpl_syms[i].value;
178 DEBUGP("Failed to find symbol %s\n", name);
182 /* Find a symbol in this elf symbol table */
183 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
184 unsigned int symindex,
189 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
191 /* Search (defined) internal symbols first. */
192 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
193 if (sym[i].st_shndx != SHN_UNDEF
194 && strcmp(name, strtab + sym[i].st_name) == 0)
195 return sym[i].st_value;
200 /* Search for module by name: must hold module_mutex. */
201 static struct module *find_module(const char *name)
205 list_for_each_entry(mod, &modules, list) {
206 if (strcmp(mod->name, name) == 0)
213 /* Number of blocks used and allocated. */
214 static unsigned int pcpu_num_used, pcpu_num_allocated;
215 /* Size of each block. -ve means used. */
216 static int *pcpu_size;
218 static int split_block(unsigned int i, unsigned short size)
220 /* Reallocation required? */
221 if (pcpu_num_used + 1 > pcpu_num_allocated) {
222 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
227 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
228 pcpu_num_allocated *= 2;
233 /* Insert a new subblock */
234 memmove(&pcpu_size[i+1], &pcpu_size[i],
235 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
238 pcpu_size[i+1] -= size;
243 static inline unsigned int block_size(int val)
250 /* Created by linker magic */
251 extern char __per_cpu_start[], __per_cpu_end[];
253 static void *percpu_modalloc(unsigned long size, unsigned long align,
260 if (align > SMP_CACHE_BYTES) {
261 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
262 name, align, SMP_CACHE_BYTES);
263 align = SMP_CACHE_BYTES;
266 ptr = __per_cpu_start;
267 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
268 /* Extra for alignment requirement. */
269 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
270 BUG_ON(i == 0 && extra != 0);
272 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
275 /* Transfer extra to previous block. */
276 if (pcpu_size[i-1] < 0)
277 pcpu_size[i-1] -= extra;
279 pcpu_size[i-1] += extra;
280 pcpu_size[i] -= extra;
283 /* Split block if warranted */
284 if (pcpu_size[i] - size > sizeof(unsigned long))
285 if (!split_block(i, size))
289 pcpu_size[i] = -pcpu_size[i];
293 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
298 static void percpu_modfree(void *freeme)
301 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
303 /* First entry is core kernel percpu data. */
304 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
306 pcpu_size[i] = -pcpu_size[i];
313 /* Merge with previous? */
314 if (pcpu_size[i-1] >= 0) {
315 pcpu_size[i-1] += pcpu_size[i];
317 memmove(&pcpu_size[i], &pcpu_size[i+1],
318 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
321 /* Merge with next? */
322 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
323 pcpu_size[i] += pcpu_size[i+1];
325 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
326 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
330 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
332 const char *secstrings)
334 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
337 static int percpu_modinit(void)
340 pcpu_num_allocated = 2;
341 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
343 /* Static in-kernel percpu data (used). */
344 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
346 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
347 if (pcpu_size[1] < 0) {
348 printk(KERN_ERR "No per-cpu room for modules.\n");
354 __initcall(percpu_modinit);
355 #else /* ... !CONFIG_SMP */
356 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
361 static inline void percpu_modfree(void *pcpuptr)
365 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
367 const char *secstrings)
371 static inline void percpu_modcopy(void *pcpudst, const void *src,
374 /* pcpusec should be 0, and size of that section should be 0. */
377 #endif /* CONFIG_SMP */
379 #ifdef CONFIG_MODULE_UNLOAD
380 #define MODINFO_ATTR(field) \
381 static void setup_modinfo_##field(struct module *mod, const char *s) \
383 mod->field = kstrdup(s, GFP_KERNEL); \
385 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
386 struct module *mod, char *buffer) \
388 return sprintf(buffer, "%s\n", mod->field); \
390 static int modinfo_##field##_exists(struct module *mod) \
392 return mod->field != NULL; \
394 static void free_modinfo_##field(struct module *mod) \
399 static struct module_attribute modinfo_##field = { \
400 .attr = { .name = __stringify(field), .mode = 0444, \
401 .owner = THIS_MODULE }, \
402 .show = show_modinfo_##field, \
403 .setup = setup_modinfo_##field, \
404 .test = modinfo_##field##_exists, \
405 .free = free_modinfo_##field, \
408 MODINFO_ATTR(version);
409 MODINFO_ATTR(srcversion);
411 static struct module_attribute *modinfo_attrs[] = {
417 /* Init the unload section of the module. */
418 static void module_unload_init(struct module *mod)
422 INIT_LIST_HEAD(&mod->modules_which_use_me);
423 for (i = 0; i < NR_CPUS; i++)
424 local_set(&mod->ref[i].count, 0);
425 /* Hold reference count during initialization. */
426 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
427 /* Backwards compatibility macros put refcount during init. */
428 mod->waiter = current;
431 /* modules using other modules */
434 struct list_head list;
435 struct module *module_which_uses;
438 /* Does a already use b? */
439 static int already_uses(struct module *a, struct module *b)
441 struct module_use *use;
443 list_for_each_entry(use, &b->modules_which_use_me, list) {
444 if (use->module_which_uses == a) {
445 DEBUGP("%s uses %s!\n", a->name, b->name);
449 DEBUGP("%s does not use %s!\n", a->name, b->name);
453 /* Module a uses b */
454 static int use_module(struct module *a, struct module *b)
456 struct module_use *use;
457 if (b == NULL || already_uses(a, b)) return 1;
459 if (!strong_try_module_get(b))
462 DEBUGP("Allocating new usage for %s.\n", a->name);
463 use = kmalloc(sizeof(*use), GFP_ATOMIC);
465 printk("%s: out of memory loading\n", a->name);
470 use->module_which_uses = a;
471 list_add(&use->list, &b->modules_which_use_me);
475 /* Clear the unload stuff of the module. */
476 static void module_unload_free(struct module *mod)
480 list_for_each_entry(i, &modules, list) {
481 struct module_use *use;
483 list_for_each_entry(use, &i->modules_which_use_me, list) {
484 if (use->module_which_uses == mod) {
485 DEBUGP("%s unusing %s\n", mod->name, i->name);
487 list_del(&use->list);
489 /* There can be at most one match. */
496 #ifdef CONFIG_MODULE_FORCE_UNLOAD
497 static inline int try_force(unsigned int flags)
499 int ret = (flags & O_TRUNC);
501 tainted |= TAINT_FORCED_MODULE;
505 static inline int try_force(unsigned int flags)
509 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
518 /* Whole machine is stopped with interrupts off when this runs. */
519 static int __try_stop_module(void *_sref)
521 struct stopref *sref = _sref;
523 /* If it's not unused, quit unless we are told to block. */
524 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
525 if (!(*sref->forced = try_force(sref->flags)))
529 /* Mark it as dying. */
530 sref->mod->state = MODULE_STATE_GOING;
534 static int try_stop_module(struct module *mod, int flags, int *forced)
536 struct stopref sref = { mod, flags, forced };
538 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
541 unsigned int module_refcount(struct module *mod)
543 unsigned int i, total = 0;
545 for (i = 0; i < NR_CPUS; i++)
546 total += local_read(&mod->ref[i].count);
549 EXPORT_SYMBOL(module_refcount);
551 /* This exists whether we can unload or not */
552 static void free_module(struct module *mod);
554 static void wait_for_zero_refcount(struct module *mod)
556 /* Since we might sleep for some time, drop the semaphore first */
559 DEBUGP("Looking at refcount...\n");
560 set_current_state(TASK_UNINTERRUPTIBLE);
561 if (module_refcount(mod) == 0)
565 current->state = TASK_RUNNING;
570 sys_delete_module(const char __user *name_user, unsigned int flags)
573 char name[MODULE_NAME_LEN];
576 if (!capable(CAP_SYS_MODULE))
579 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
581 name[MODULE_NAME_LEN-1] = '\0';
583 if (down_interruptible(&module_mutex) != 0)
586 mod = find_module(name);
592 if (!list_empty(&mod->modules_which_use_me)) {
593 /* Other modules depend on us: get rid of them first. */
598 /* Doing init or already dying? */
599 if (mod->state != MODULE_STATE_LIVE) {
600 /* FIXME: if (force), slam module count and wake up
602 DEBUGP("%s already dying\n", mod->name);
607 /* If it has an init func, it must have an exit func to unload */
608 if ((mod->init != NULL && mod->exit == NULL)
610 forced = try_force(flags);
612 /* This module can't be removed */
618 /* Set this up before setting mod->state */
619 mod->waiter = current;
621 /* Stop the machine so refcounts can't move and disable module. */
622 ret = try_stop_module(mod, flags, &forced);
626 /* Never wait if forced. */
627 if (!forced && module_refcount(mod) != 0)
628 wait_for_zero_refcount(mod);
630 /* Final destruction now noone is using it. */
631 if (mod->exit != NULL) {
643 static void print_unload_info(struct seq_file *m, struct module *mod)
645 struct module_use *use;
646 int printed_something = 0;
648 seq_printf(m, " %u ", module_refcount(mod));
650 /* Always include a trailing , so userspace can differentiate
651 between this and the old multi-field proc format. */
652 list_for_each_entry(use, &mod->modules_which_use_me, list) {
653 printed_something = 1;
654 seq_printf(m, "%s,", use->module_which_uses->name);
658 printed_something = 1;
659 seq_printf(m, "[unsafe],");
662 if (mod->init != NULL && mod->exit == NULL) {
663 printed_something = 1;
664 seq_printf(m, "[permanent],");
667 if (!printed_something)
671 void __symbol_put(const char *symbol)
673 struct module *owner;
675 const unsigned long *crc;
677 spin_lock_irqsave(&modlist_lock, flags);
678 if (!__find_symbol(symbol, &owner, &crc, 1))
681 spin_unlock_irqrestore(&modlist_lock, flags);
683 EXPORT_SYMBOL(__symbol_put);
685 void symbol_put_addr(void *addr)
689 spin_lock_irqsave(&modlist_lock, flags);
690 if (!kernel_text_address((unsigned long)addr))
693 module_put(module_text_address((unsigned long)addr));
694 spin_unlock_irqrestore(&modlist_lock, flags);
696 EXPORT_SYMBOL_GPL(symbol_put_addr);
698 static ssize_t show_refcnt(struct module_attribute *mattr,
699 struct module *mod, char *buffer)
701 /* sysfs holds a reference */
702 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
705 static struct module_attribute refcnt = {
706 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
710 #else /* !CONFIG_MODULE_UNLOAD */
711 static void print_unload_info(struct seq_file *m, struct module *mod)
713 /* We don't know the usage count, or what modules are using. */
714 seq_printf(m, " - -");
717 static inline void module_unload_free(struct module *mod)
721 static inline int use_module(struct module *a, struct module *b)
723 return strong_try_module_get(b);
726 static inline void module_unload_init(struct module *mod)
729 #endif /* CONFIG_MODULE_UNLOAD */
731 #ifdef CONFIG_OBSOLETE_MODPARM
732 /* Bounds checking done below */
733 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
735 strcpy(kp->arg, val);
739 static int set_obsolete(const char *val, struct kernel_param *kp)
741 unsigned int min, max;
742 unsigned int size, maxsize;
746 struct obsolete_modparm *obsparm = kp->arg;
749 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
753 /* type is: [min[-max]]{b,h,i,l,s} */
755 min = simple_strtol(p, &endp, 10);
756 if (endp == obsparm->type)
758 else if (*endp == '-') {
760 max = simple_strtol(p, &endp, 10);
765 return param_array(kp->name, val, min, max, obsparm->addr,
766 1, param_set_byte, &dummy);
768 return param_array(kp->name, val, min, max, obsparm->addr,
769 sizeof(short), param_set_short, &dummy);
771 return param_array(kp->name, val, min, max, obsparm->addr,
772 sizeof(int), param_set_int, &dummy);
774 return param_array(kp->name, val, min, max, obsparm->addr,
775 sizeof(long), param_set_long, &dummy);
777 return param_array(kp->name, val, min, max, obsparm->addr,
778 sizeof(char *), param_set_charp, &dummy);
781 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
782 and the decl is "char xxx[5][50];" */
784 maxsize = simple_strtol(p, &endp, 10);
785 /* We check lengths here (yes, this is a hack). */
787 while (p[size = strcspn(p, ",")]) {
794 return param_array(kp->name, val, min, max, obsparm->addr,
795 maxsize, obsparm_copy_string, &dummy);
797 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
801 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
805 static int obsolete_params(const char *name,
807 struct obsolete_modparm obsparm[],
810 unsigned int symindex,
813 struct kernel_param *kp;
817 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
821 for (i = 0; i < num; i++) {
822 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
824 snprintf(sym_name, sizeof(sym_name), "%s%s",
825 MODULE_SYMBOL_PREFIX, obsparm[i].name);
827 kp[i].name = obsparm[i].name;
829 kp[i].set = set_obsolete;
832 = (void *)find_local_symbol(sechdrs, symindex, strtab,
834 if (!obsparm[i].addr) {
835 printk("%s: falsely claims to have parameter %s\n",
836 name, obsparm[i].name);
840 kp[i].arg = &obsparm[i];
843 ret = parse_args(name, args, kp, num, NULL);
849 static int obsolete_params(const char *name,
851 struct obsolete_modparm obsparm[],
854 unsigned int symindex,
858 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
862 #endif /* CONFIG_OBSOLETE_MODPARM */
864 static const char vermagic[] = VERMAGIC_STRING;
866 #ifdef CONFIG_MODVERSIONS
867 static int check_version(Elf_Shdr *sechdrs,
868 unsigned int versindex,
871 const unsigned long *crc)
873 unsigned int i, num_versions;
874 struct modversion_info *versions;
876 /* Exporting module didn't supply crcs? OK, we're already tainted. */
880 versions = (void *) sechdrs[versindex].sh_addr;
881 num_versions = sechdrs[versindex].sh_size
882 / sizeof(struct modversion_info);
884 for (i = 0; i < num_versions; i++) {
885 if (strcmp(versions[i].name, symname) != 0)
888 if (versions[i].crc == *crc)
890 printk("%s: disagrees about version of symbol %s\n",
892 DEBUGP("Found checksum %lX vs module %lX\n",
893 *crc, versions[i].crc);
896 /* Not in module's version table. OK, but that taints the kernel. */
897 if (!(tainted & TAINT_FORCED_MODULE)) {
898 printk("%s: no version for \"%s\" found: kernel tainted.\n",
900 tainted |= TAINT_FORCED_MODULE;
905 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
906 unsigned int versindex,
909 const unsigned long *crc;
910 struct module *owner;
912 if (!__find_symbol("struct_module", &owner, &crc, 1))
914 return check_version(sechdrs, versindex, "struct_module", mod,
918 /* First part is kernel version, which we ignore. */
919 static inline int same_magic(const char *amagic, const char *bmagic)
921 amagic += strcspn(amagic, " ");
922 bmagic += strcspn(bmagic, " ");
923 return strcmp(amagic, bmagic) == 0;
926 static inline int check_version(Elf_Shdr *sechdrs,
927 unsigned int versindex,
930 const unsigned long *crc)
935 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
936 unsigned int versindex,
942 static inline int same_magic(const char *amagic, const char *bmagic)
944 return strcmp(amagic, bmagic) == 0;
946 #endif /* CONFIG_MODVERSIONS */
948 /* Resolve a symbol for this module. I.e. if we find one, record usage.
949 Must be holding module_mutex. */
950 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
951 unsigned int versindex,
955 struct module *owner;
957 const unsigned long *crc;
959 spin_lock_irq(&modlist_lock);
960 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
962 /* use_module can fail due to OOM, or module unloading */
963 if (!check_version(sechdrs, versindex, name, mod, crc) ||
964 !use_module(mod, owner))
967 spin_unlock_irq(&modlist_lock);
973 * /sys/module/foo/sections stuff
974 * J. Corbet <corbet@lwn.net>
976 #ifdef CONFIG_KALLSYMS
977 static ssize_t module_sect_show(struct module_attribute *mattr,
978 struct module *mod, char *buf)
980 struct module_sect_attr *sattr =
981 container_of(mattr, struct module_sect_attr, mattr);
982 return sprintf(buf, "0x%lx\n", sattr->address);
985 static void add_sect_attrs(struct module *mod, unsigned int nsect,
986 char *secstrings, Elf_Shdr *sechdrs)
988 unsigned int nloaded = 0, i, size[2];
989 struct module_sect_attrs *sect_attrs;
990 struct module_sect_attr *sattr;
991 struct attribute **gattr;
993 /* Count loaded sections and allocate structures */
994 for (i = 0; i < nsect; i++)
995 if (sechdrs[i].sh_flags & SHF_ALLOC)
997 size[0] = ALIGN(sizeof(*sect_attrs)
998 + nloaded * sizeof(sect_attrs->attrs[0]),
999 sizeof(sect_attrs->grp.attrs[0]));
1000 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1001 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
1004 /* Setup section attributes. */
1005 sect_attrs->grp.name = "sections";
1006 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1008 sattr = §_attrs->attrs[0];
1009 gattr = §_attrs->grp.attrs[0];
1010 for (i = 0; i < nsect; i++) {
1011 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1013 sattr->address = sechdrs[i].sh_addr;
1014 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1015 MODULE_SECT_NAME_LEN);
1016 sattr->mattr.show = module_sect_show;
1017 sattr->mattr.store = NULL;
1018 sattr->mattr.attr.name = sattr->name;
1019 sattr->mattr.attr.owner = mod;
1020 sattr->mattr.attr.mode = S_IRUGO;
1021 *(gattr++) = &(sattr++)->mattr.attr;
1025 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1028 mod->sect_attrs = sect_attrs;
1034 static void remove_sect_attrs(struct module *mod)
1036 if (mod->sect_attrs) {
1037 sysfs_remove_group(&mod->mkobj.kobj,
1038 &mod->sect_attrs->grp);
1039 /* We are positive that no one is using any sect attrs
1040 * at this point. Deallocate immediately. */
1041 kfree(mod->sect_attrs);
1042 mod->sect_attrs = NULL;
1048 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1049 char *sectstrings, Elf_Shdr *sechdrs)
1053 static inline void remove_sect_attrs(struct module *mod)
1056 #endif /* CONFIG_KALLSYMS */
1059 #ifdef CONFIG_MODULE_UNLOAD
1060 static inline int module_add_refcnt_attr(struct module *mod)
1062 return sysfs_create_file(&mod->mkobj.kobj, &refcnt.attr);
1064 static void module_remove_refcnt_attr(struct module *mod)
1066 return sysfs_remove_file(&mod->mkobj.kobj, &refcnt.attr);
1069 static inline int module_add_refcnt_attr(struct module *mod)
1073 static void module_remove_refcnt_attr(struct module *mod)
1078 #ifdef CONFIG_MODULE_UNLOAD
1079 static int module_add_modinfo_attrs(struct module *mod)
1081 struct module_attribute *attr;
1085 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1087 (attr->test && attr->test(mod)))
1088 error = sysfs_create_file(&mod->mkobj.kobj,&attr->attr);
1093 static void module_remove_modinfo_attrs(struct module *mod)
1095 struct module_attribute *attr;
1098 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1099 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1105 static int mod_sysfs_setup(struct module *mod,
1106 struct kernel_param *kparam,
1107 unsigned int num_params)
1111 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1112 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1115 kobj_set_kset_s(&mod->mkobj, module_subsys);
1116 mod->mkobj.mod = mod;
1117 err = kobject_register(&mod->mkobj.kobj);
1121 err = module_add_refcnt_attr(mod);
1125 err = module_param_sysfs_setup(mod, kparam, num_params);
1129 #ifdef CONFIG_MODULE_UNLOAD
1130 err = module_add_modinfo_attrs(mod);
1138 kobject_unregister(&mod->mkobj.kobj);
1143 static void mod_kobject_remove(struct module *mod)
1145 #ifdef CONFIG_MODULE_UNLOAD
1146 module_remove_modinfo_attrs(mod);
1148 module_remove_refcnt_attr(mod);
1149 module_param_sysfs_remove(mod);
1151 kobject_unregister(&mod->mkobj.kobj);
1155 * unlink the module with the whole machine is stopped with interrupts off
1156 * - this defends against kallsyms not taking locks
1158 static int __unlink_module(void *_mod)
1160 struct module *mod = _mod;
1161 list_del(&mod->list);
1165 /* Free a module, remove from lists, etc (must hold module mutex). */
1166 static void free_module(struct module *mod)
1168 /* Delete from various lists */
1169 stop_machine_run(__unlink_module, mod, NR_CPUS);
1170 remove_sect_attrs(mod);
1171 mod_kobject_remove(mod);
1173 /* Arch-specific cleanup. */
1174 module_arch_cleanup(mod);
1176 /* Module unload stuff */
1177 module_unload_free(mod);
1179 /* This may be NULL, but that's OK */
1180 module_free(mod, mod->module_init);
1183 percpu_modfree(mod->percpu);
1185 /* Finally, free the core (containing the module structure) */
1186 module_free(mod, mod->module_core);
1189 void *__symbol_get(const char *symbol)
1191 struct module *owner;
1192 unsigned long value, flags;
1193 const unsigned long *crc;
1195 spin_lock_irqsave(&modlist_lock, flags);
1196 value = __find_symbol(symbol, &owner, &crc, 1);
1197 if (value && !strong_try_module_get(owner))
1199 spin_unlock_irqrestore(&modlist_lock, flags);
1201 return (void *)value;
1203 EXPORT_SYMBOL_GPL(__symbol_get);
1205 /* Change all symbols so that sh_value encodes the pointer directly. */
1206 static int simplify_symbols(Elf_Shdr *sechdrs,
1207 unsigned int symindex,
1209 unsigned int versindex,
1210 unsigned int pcpuindex,
1213 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1214 unsigned long secbase;
1215 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1218 for (i = 1; i < n; i++) {
1219 switch (sym[i].st_shndx) {
1221 /* We compiled with -fno-common. These are not
1222 supposed to happen. */
1223 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1224 printk("%s: please compile with -fno-common\n",
1230 /* Don't need to do anything */
1231 DEBUGP("Absolute symbol: 0x%08lx\n",
1232 (long)sym[i].st_value);
1237 = resolve_symbol(sechdrs, versindex,
1238 strtab + sym[i].st_name, mod);
1240 /* Ok if resolved. */
1241 if (sym[i].st_value != 0)
1244 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1247 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1248 mod->name, strtab + sym[i].st_name);
1253 /* Divert to percpu allocation if a percpu var. */
1254 if (sym[i].st_shndx == pcpuindex)
1255 secbase = (unsigned long)mod->percpu;
1257 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1258 sym[i].st_value += secbase;
1266 /* Update size with this section: return offset. */
1267 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1271 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1272 *size = ret + sechdr->sh_size;
1276 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1277 might -- code, read-only data, read-write data, small data. Tally
1278 sizes, and place the offsets into sh_entsize fields: high bit means it
1280 static void layout_sections(struct module *mod,
1281 const Elf_Ehdr *hdr,
1283 const char *secstrings)
1285 static unsigned long const masks[][2] = {
1286 /* NOTE: all executable code must be the first section
1287 * in this array; otherwise modify the text_size
1288 * finder in the two loops below */
1289 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1290 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1291 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1292 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1296 for (i = 0; i < hdr->e_shnum; i++)
1297 sechdrs[i].sh_entsize = ~0UL;
1299 DEBUGP("Core section allocation order:\n");
1300 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1301 for (i = 0; i < hdr->e_shnum; ++i) {
1302 Elf_Shdr *s = &sechdrs[i];
1304 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1305 || (s->sh_flags & masks[m][1])
1306 || s->sh_entsize != ~0UL
1307 || strncmp(secstrings + s->sh_name,
1310 s->sh_entsize = get_offset(&mod->core_size, s);
1311 DEBUGP("\t%s\n", secstrings + s->sh_name);
1314 mod->core_text_size = mod->core_size;
1317 DEBUGP("Init section allocation order:\n");
1318 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1319 for (i = 0; i < hdr->e_shnum; ++i) {
1320 Elf_Shdr *s = &sechdrs[i];
1322 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1323 || (s->sh_flags & masks[m][1])
1324 || s->sh_entsize != ~0UL
1325 || strncmp(secstrings + s->sh_name,
1328 s->sh_entsize = (get_offset(&mod->init_size, s)
1329 | INIT_OFFSET_MASK);
1330 DEBUGP("\t%s\n", secstrings + s->sh_name);
1333 mod->init_text_size = mod->init_size;
1337 static inline int license_is_gpl_compatible(const char *license)
1339 return (strcmp(license, "GPL") == 0
1340 || strcmp(license, "GPL v2") == 0
1341 || strcmp(license, "GPL and additional rights") == 0
1342 || strcmp(license, "Dual BSD/GPL") == 0
1343 || strcmp(license, "Dual MPL/GPL") == 0);
1346 static void set_license(struct module *mod, const char *license)
1349 license = "unspecified";
1351 mod->license_gplok = license_is_gpl_compatible(license);
1352 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1353 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1354 mod->name, license);
1355 tainted |= TAINT_PROPRIETARY_MODULE;
1359 /* Parse tag=value strings from .modinfo section */
1360 static char *next_string(char *string, unsigned long *secsize)
1362 /* Skip non-zero chars */
1365 if ((*secsize)-- <= 1)
1369 /* Skip any zero padding. */
1370 while (!string[0]) {
1372 if ((*secsize)-- <= 1)
1378 static char *get_modinfo(Elf_Shdr *sechdrs,
1383 unsigned int taglen = strlen(tag);
1384 unsigned long size = sechdrs[info].sh_size;
1386 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1387 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1388 return p + taglen + 1;
1393 #ifdef CONFIG_MODULE_UNLOAD
1394 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1395 unsigned int infoindex)
1397 struct module_attribute *attr;
1400 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1403 get_modinfo(sechdrs,
1410 #ifdef CONFIG_KALLSYMS
1411 int is_exported(const char *name, const struct module *mod)
1416 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1417 if (strcmp(__start___ksymtab[i].name, name) == 0)
1421 for (i = 0; i < mod->num_syms; i++)
1422 if (strcmp(mod->syms[i].name, name) == 0)
1428 static char elf_type(const Elf_Sym *sym,
1430 const char *secstrings,
1433 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1434 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1439 if (sym->st_shndx == SHN_UNDEF)
1441 if (sym->st_shndx == SHN_ABS)
1443 if (sym->st_shndx >= SHN_LORESERVE)
1445 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1447 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1448 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1449 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1451 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1456 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1457 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1462 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1463 ".debug", strlen(".debug")) == 0)
1468 static void add_kallsyms(struct module *mod,
1470 unsigned int symindex,
1471 unsigned int strindex,
1472 const char *secstrings)
1476 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1477 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1478 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1480 /* Set types up while we still have access to sections. */
1481 for (i = 0; i < mod->num_symtab; i++)
1482 mod->symtab[i].st_info
1483 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1486 static inline void add_kallsyms(struct module *mod,
1488 unsigned int symindex,
1489 unsigned int strindex,
1490 const char *secstrings)
1493 #endif /* CONFIG_KALLSYMS */
1495 /* Allocate and load the module: note that size of section 0 is always
1496 zero, and we rely on this for optional sections. */
1497 static struct module *load_module(void __user *umod,
1499 const char __user *uargs)
1503 char *secstrings, *args, *modmagic, *strtab = NULL;
1504 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1505 exportindex, modindex, obsparmindex, infoindex, gplindex,
1506 crcindex, gplcrcindex, versindex, pcpuindex;
1510 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1511 struct exception_table_entry *extable;
1512 mm_segment_t old_fs;
1514 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1516 if (len < sizeof(*hdr))
1517 return ERR_PTR(-ENOEXEC);
1519 /* Suck in entire file: we'll want most of it. */
1520 /* vmalloc barfs on "unusual" numbers. Check here */
1521 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1522 return ERR_PTR(-ENOMEM);
1523 if (copy_from_user(hdr, umod, len) != 0) {
1528 /* Sanity checks against insmoding binaries or wrong arch,
1529 weird elf version */
1530 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1531 || hdr->e_type != ET_REL
1532 || !elf_check_arch(hdr)
1533 || hdr->e_shentsize != sizeof(*sechdrs)) {
1538 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1541 /* Convenience variables */
1542 sechdrs = (void *)hdr + hdr->e_shoff;
1543 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1544 sechdrs[0].sh_addr = 0;
1546 for (i = 1; i < hdr->e_shnum; i++) {
1547 if (sechdrs[i].sh_type != SHT_NOBITS
1548 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1551 /* Mark all sections sh_addr with their address in the
1553 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1555 /* Internal symbols and strings. */
1556 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1558 strindex = sechdrs[i].sh_link;
1559 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1561 #ifndef CONFIG_MODULE_UNLOAD
1562 /* Don't load .exit sections */
1563 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1564 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1568 modindex = find_sec(hdr, sechdrs, secstrings,
1569 ".gnu.linkonce.this_module");
1571 printk(KERN_WARNING "No module found in object\n");
1575 mod = (void *)sechdrs[modindex].sh_addr;
1577 if (symindex == 0) {
1578 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1584 /* Optional sections */
1585 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1586 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1587 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1588 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1589 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1590 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1591 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1592 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1593 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1594 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1596 /* Don't keep modinfo section */
1597 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1598 #ifdef CONFIG_KALLSYMS
1599 /* Keep symbol and string tables for decoding later. */
1600 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1601 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1604 /* Check module struct version now, before we try to use module. */
1605 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1610 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1611 /* This is allowed: modprobe --force will invalidate it. */
1613 tainted |= TAINT_FORCED_MODULE;
1614 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1616 } else if (!same_magic(modmagic, vermagic)) {
1617 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1618 mod->name, modmagic, vermagic);
1623 /* Now copy in args */
1624 arglen = strlen_user(uargs);
1629 args = kmalloc(arglen, GFP_KERNEL);
1634 if (copy_from_user(args, uargs, arglen) != 0) {
1639 if (find_module(mod->name)) {
1644 mod->state = MODULE_STATE_COMING;
1646 /* Allow arches to frob section contents and sizes. */
1647 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1652 /* We have a special allocation for this section. */
1653 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1654 sechdrs[pcpuindex].sh_addralign,
1660 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1661 mod->percpu = percpu;
1664 /* Determine total sizes, and put offsets in sh_entsize. For now
1665 this is done generically; there doesn't appear to be any
1666 special cases for the architectures. */
1667 layout_sections(mod, hdr, sechdrs, secstrings);
1669 /* Do the allocs. */
1670 ptr = module_alloc(mod->core_size);
1675 memset(ptr, 0, mod->core_size);
1676 mod->module_core = ptr;
1678 ptr = module_alloc(mod->init_size);
1679 if (!ptr && mod->init_size) {
1683 memset(ptr, 0, mod->init_size);
1684 mod->module_init = ptr;
1686 /* Transfer each section which specifies SHF_ALLOC */
1687 DEBUGP("final section addresses:\n");
1688 for (i = 0; i < hdr->e_shnum; i++) {
1691 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1694 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1695 dest = mod->module_init
1696 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1698 dest = mod->module_core + sechdrs[i].sh_entsize;
1700 if (sechdrs[i].sh_type != SHT_NOBITS)
1701 memcpy(dest, (void *)sechdrs[i].sh_addr,
1702 sechdrs[i].sh_size);
1703 /* Update sh_addr to point to copy in image. */
1704 sechdrs[i].sh_addr = (unsigned long)dest;
1705 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1707 /* Module has been moved. */
1708 mod = (void *)sechdrs[modindex].sh_addr;
1710 /* Now we've moved module, initialize linked lists, etc. */
1711 module_unload_init(mod);
1713 /* Set up license info based on the info section */
1714 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1716 #ifdef CONFIG_MODULE_UNLOAD
1717 /* Set up MODINFO_ATTR fields */
1718 setup_modinfo(mod, sechdrs, infoindex);
1721 /* Fix up syms, so that st_value is a pointer to location. */
1722 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1727 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1728 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1729 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1731 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1732 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1733 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1735 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1737 #ifdef CONFIG_MODVERSIONS
1738 if ((mod->num_syms && !crcindex) ||
1739 (mod->num_gpl_syms && !gplcrcindex)) {
1740 printk(KERN_WARNING "%s: No versions for exported symbols."
1741 " Tainting kernel.\n", mod->name);
1742 tainted |= TAINT_FORCED_MODULE;
1746 /* Now do relocations. */
1747 for (i = 1; i < hdr->e_shnum; i++) {
1748 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1749 unsigned int info = sechdrs[i].sh_info;
1751 /* Not a valid relocation section? */
1752 if (info >= hdr->e_shnum)
1755 /* Don't bother with non-allocated sections */
1756 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1759 if (sechdrs[i].sh_type == SHT_REL)
1760 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1761 else if (sechdrs[i].sh_type == SHT_RELA)
1762 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1768 /* Set up and sort exception table */
1769 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1770 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1771 sort_extable(extable, extable + mod->num_exentries);
1773 /* Finally, copy percpu area over. */
1774 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1775 sechdrs[pcpuindex].sh_size);
1777 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1779 err = module_finalize(hdr, sechdrs, mod);
1783 /* flush the icache in correct context */
1788 * Flush the instruction cache, since we've played with text.
1789 * Do it before processing of module parameters, so the module
1790 * can provide parameter accessor functions of its own.
1792 if (mod->module_init)
1793 flush_icache_range((unsigned long)mod->module_init,
1794 (unsigned long)mod->module_init
1796 flush_icache_range((unsigned long)mod->module_core,
1797 (unsigned long)mod->module_core + mod->core_size);
1803 err = obsolete_params(mod->name, mod->args,
1804 (struct obsolete_modparm *)
1805 sechdrs[obsparmindex].sh_addr,
1806 sechdrs[obsparmindex].sh_size
1807 / sizeof(struct obsolete_modparm),
1809 (char *)sechdrs[strindex].sh_addr);
1811 printk(KERN_WARNING "%s: Ignoring new-style "
1812 "parameters in presence of obsolete ones\n",
1815 /* Size of section 0 is 0, so this works well if no params */
1816 err = parse_args(mod->name, mod->args,
1817 (struct kernel_param *)
1818 sechdrs[setupindex].sh_addr,
1819 sechdrs[setupindex].sh_size
1820 / sizeof(struct kernel_param),
1826 err = mod_sysfs_setup(mod,
1827 (struct kernel_param *)
1828 sechdrs[setupindex].sh_addr,
1829 sechdrs[setupindex].sh_size
1830 / sizeof(struct kernel_param));
1833 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1835 /* Get rid of temporary copy */
1842 module_arch_cleanup(mod);
1844 module_unload_free(mod);
1845 module_free(mod, mod->module_init);
1847 module_free(mod, mod->module_core);
1850 percpu_modfree(percpu);
1855 if (err < 0) return ERR_PTR(err);
1859 printk(KERN_ERR "Module len %lu truncated\n", len);
1865 * link the module with the whole machine is stopped with interrupts off
1866 * - this defends against kallsyms not taking locks
1868 static int __link_module(void *_mod)
1870 struct module *mod = _mod;
1871 list_add(&mod->list, &modules);
1875 /* This is where the real work happens */
1877 sys_init_module(void __user *umod,
1879 const char __user *uargs)
1884 /* Must have permission */
1885 if (!capable(CAP_SYS_MODULE))
1888 /* Only one module load at a time, please */
1889 if (down_interruptible(&module_mutex) != 0)
1892 /* Do all the hard work */
1893 mod = load_module(umod, len, uargs);
1896 return PTR_ERR(mod);
1899 /* Now sew it into the lists. They won't access us, since
1900 strong_try_module_get() will fail. */
1901 stop_machine_run(__link_module, mod, NR_CPUS);
1903 /* Drop lock so they can recurse */
1906 down(¬ify_mutex);
1907 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1910 /* Start the module */
1911 if (mod->init != NULL)
1914 /* Init routine failed: abort. Try to protect us from
1915 buggy refcounters. */
1916 mod->state = MODULE_STATE_GOING;
1917 synchronize_sched();
1919 printk(KERN_ERR "%s: module is now stuck!\n",
1923 down(&module_mutex);
1930 /* Now it's a first class citizen! */
1931 down(&module_mutex);
1932 mod->state = MODULE_STATE_LIVE;
1933 /* Drop initial reference. */
1935 module_free(mod, mod->module_init);
1936 mod->module_init = NULL;
1938 mod->init_text_size = 0;
1944 static inline int within(unsigned long addr, void *start, unsigned long size)
1946 return ((void *)addr >= start && (void *)addr < start + size);
1949 #ifdef CONFIG_KALLSYMS
1951 * This ignores the intensely annoying "mapping symbols" found
1952 * in ARM ELF files: $a, $t and $d.
1954 static inline int is_arm_mapping_symbol(const char *str)
1956 return str[0] == '$' && strchr("atd", str[1])
1957 && (str[2] == '\0' || str[2] == '.');
1960 static const char *get_ksymbol(struct module *mod,
1962 unsigned long *size,
1963 unsigned long *offset)
1965 unsigned int i, best = 0;
1966 unsigned long nextval;
1968 /* At worse, next value is at end of module */
1969 if (within(addr, mod->module_init, mod->init_size))
1970 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1972 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1974 /* Scan for closest preceeding symbol, and next symbol. (ELF
1975 starts real symbols at 1). */
1976 for (i = 1; i < mod->num_symtab; i++) {
1977 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1980 /* We ignore unnamed symbols: they're uninformative
1981 * and inserted at a whim. */
1982 if (mod->symtab[i].st_value <= addr
1983 && mod->symtab[i].st_value > mod->symtab[best].st_value
1984 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1985 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1987 if (mod->symtab[i].st_value > addr
1988 && mod->symtab[i].st_value < nextval
1989 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1990 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1991 nextval = mod->symtab[i].st_value;
1997 *size = nextval - mod->symtab[best].st_value;
1998 *offset = addr - mod->symtab[best].st_value;
1999 return mod->strtab + mod->symtab[best].st_name;
2002 /* For kallsyms to ask for address resolution. NULL means not found.
2003 We don't lock, as this is used for oops resolution and races are a
2005 const char *module_address_lookup(unsigned long addr,
2006 unsigned long *size,
2007 unsigned long *offset,
2012 list_for_each_entry(mod, &modules, list) {
2013 if (within(addr, mod->module_init, mod->init_size)
2014 || within(addr, mod->module_core, mod->core_size)) {
2015 *modname = mod->name;
2016 return get_ksymbol(mod, addr, size, offset);
2022 struct module *module_get_kallsym(unsigned int symnum,
2023 unsigned long *value,
2029 down(&module_mutex);
2030 list_for_each_entry(mod, &modules, list) {
2031 if (symnum < mod->num_symtab) {
2032 *value = mod->symtab[symnum].st_value;
2033 *type = mod->symtab[symnum].st_info;
2035 mod->strtab + mod->symtab[symnum].st_name,
2040 symnum -= mod->num_symtab;
2046 static unsigned long mod_find_symname(struct module *mod, const char *name)
2050 for (i = 0; i < mod->num_symtab; i++)
2051 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
2052 return mod->symtab[i].st_value;
2056 /* Look for this name: can be of form module:name. */
2057 unsigned long module_kallsyms_lookup_name(const char *name)
2061 unsigned long ret = 0;
2063 /* Don't lock: we're in enough trouble already. */
2064 if ((colon = strchr(name, ':')) != NULL) {
2066 if ((mod = find_module(name)) != NULL)
2067 ret = mod_find_symname(mod, colon+1);
2070 list_for_each_entry(mod, &modules, list)
2071 if ((ret = mod_find_symname(mod, name)) != 0)
2076 #endif /* CONFIG_KALLSYMS */
2078 /* Called by the /proc file system to return a list of modules. */
2079 static void *m_start(struct seq_file *m, loff_t *pos)
2081 struct list_head *i;
2084 down(&module_mutex);
2085 list_for_each(i, &modules) {
2094 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2096 struct list_head *i = p;
2098 if (i->next == &modules)
2103 static void m_stop(struct seq_file *m, void *p)
2108 static int m_show(struct seq_file *m, void *p)
2110 struct module *mod = list_entry(p, struct module, list);
2111 seq_printf(m, "%s %lu",
2112 mod->name, mod->init_size + mod->core_size);
2113 print_unload_info(m, mod);
2115 /* Informative for users. */
2116 seq_printf(m, " %s",
2117 mod->state == MODULE_STATE_GOING ? "Unloading":
2118 mod->state == MODULE_STATE_COMING ? "Loading":
2120 /* Used by oprofile and other similar tools. */
2121 seq_printf(m, " 0x%p", mod->module_core);
2123 seq_printf(m, "\n");
2127 /* Format: modulename size refcount deps address
2129 Where refcount is a number or -, and deps is a comma-separated list
2132 struct seq_operations modules_op = {
2139 /* Given an address, look for it in the module exception tables. */
2140 const struct exception_table_entry *search_module_extables(unsigned long addr)
2142 unsigned long flags;
2143 const struct exception_table_entry *e = NULL;
2146 spin_lock_irqsave(&modlist_lock, flags);
2147 list_for_each_entry(mod, &modules, list) {
2148 if (mod->num_exentries == 0)
2151 e = search_extable(mod->extable,
2152 mod->extable + mod->num_exentries - 1,
2157 spin_unlock_irqrestore(&modlist_lock, flags);
2159 /* Now, if we found one, we are running inside it now, hence
2160 we cannot unload the module, hence no refcnt needed. */
2164 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2165 struct module *__module_text_address(unsigned long addr)
2169 list_for_each_entry(mod, &modules, list)
2170 if (within(addr, mod->module_init, mod->init_text_size)
2171 || within(addr, mod->module_core, mod->core_text_size))
2176 struct module *module_text_address(unsigned long addr)
2179 unsigned long flags;
2181 spin_lock_irqsave(&modlist_lock, flags);
2182 mod = __module_text_address(addr);
2183 spin_unlock_irqrestore(&modlist_lock, flags);
2188 /* Don't grab lock, we're oopsing. */
2189 void print_modules(void)
2193 printk("Modules linked in:");
2194 list_for_each_entry(mod, &modules, list)
2195 printk(" %s", mod->name);
2199 void module_add_driver(struct module *mod, struct device_driver *drv)
2204 /* Don't check return code; this call is idempotent */
2205 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2207 EXPORT_SYMBOL(module_add_driver);
2209 void module_remove_driver(struct device_driver *drv)
2213 sysfs_remove_link(&drv->kobj, "module");
2215 EXPORT_SYMBOL(module_remove_driver);
2217 #ifdef CONFIG_MODVERSIONS
2218 /* Generate the signature for struct module here, too, for modversions. */
2219 void struct_module(struct module *mod) { return; }
2220 EXPORT_SYMBOL(struct_module);