1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/elf.h>
27 #include <linux/seq_file.h>
28 #include <linux/syscalls.h>
29 #include <linux/fcntl.h>
30 #include <linux/rcupdate.h>
31 #include <linux/cpu.h>
32 #include <linux/moduleparam.h>
33 #include <linux/errno.h>
34 #include <linux/err.h>
35 #include <linux/vermagic.h>
36 #include <linux/notifier.h>
37 #include <linux/stop_machine.h>
38 #include <linux/device.h>
39 #include <linux/string.h>
40 #include <asm/uaccess.h>
41 #include <asm/semaphore.h>
42 #include <asm/cacheflush.h>
47 #define DEBUGP(fmt , a...)
50 #ifndef ARCH_SHF_SMALL
51 #define ARCH_SHF_SMALL 0
54 /* If this is set, the section belongs in the init part of the module */
55 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
57 /* Protects module list */
58 static DEFINE_SPINLOCK(modlist_lock
);
60 /* List of modules, protected by module_mutex AND modlist_lock */
61 static DECLARE_MUTEX(module_mutex
);
62 static LIST_HEAD(modules
);
64 static DECLARE_MUTEX(notify_mutex
);
65 static struct notifier_block
* module_notify_list
;
67 int register_module_notifier(struct notifier_block
* nb
)
71 err
= notifier_chain_register(&module_notify_list
, nb
);
75 EXPORT_SYMBOL(register_module_notifier
);
77 int unregister_module_notifier(struct notifier_block
* nb
)
81 err
= notifier_chain_unregister(&module_notify_list
, nb
);
85 EXPORT_SYMBOL(unregister_module_notifier
);
87 /* We require a truly strong try_module_get() */
88 static inline int strong_try_module_get(struct module
*mod
)
90 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
92 return try_module_get(mod
);
95 /* A thread that wants to hold a reference to a module only while it
96 * is running can call ths to safely exit.
97 * nfsd and lockd use this.
99 void __module_put_and_exit(struct module
*mod
, long code
)
104 EXPORT_SYMBOL(__module_put_and_exit
);
106 /* Find a module section: 0 means not found. */
107 static unsigned int find_sec(Elf_Ehdr
*hdr
,
109 const char *secstrings
,
114 for (i
= 1; i
< hdr
->e_shnum
; i
++)
115 /* Alloc bit cleared means "ignore it." */
116 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
117 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
122 /* Provided by the linker */
123 extern const struct kernel_symbol __start___ksymtab
[];
124 extern const struct kernel_symbol __stop___ksymtab
[];
125 extern const struct kernel_symbol __start___ksymtab_gpl
[];
126 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
127 extern const unsigned long __start___kcrctab
[];
128 extern const unsigned long __start___kcrctab_gpl
[];
130 #ifndef CONFIG_MODVERSIONS
131 #define symversion(base, idx) NULL
133 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
136 /* Find a symbol, return value, crc and module which owns it */
137 static unsigned long __find_symbol(const char *name
,
138 struct module
**owner
,
139 const unsigned long **crc
,
145 /* Core kernel first. */
147 for (i
= 0; __start___ksymtab
+i
< __stop___ksymtab
; i
++) {
148 if (strcmp(__start___ksymtab
[i
].name
, name
) == 0) {
149 *crc
= symversion(__start___kcrctab
, i
);
150 return __start___ksymtab
[i
].value
;
154 for (i
= 0; __start___ksymtab_gpl
+i
<__stop___ksymtab_gpl
; i
++)
155 if (strcmp(__start___ksymtab_gpl
[i
].name
, name
) == 0) {
156 *crc
= symversion(__start___kcrctab_gpl
, i
);
157 return __start___ksymtab_gpl
[i
].value
;
161 /* Now try modules. */
162 list_for_each_entry(mod
, &modules
, list
) {
164 for (i
= 0; i
< mod
->num_syms
; i
++)
165 if (strcmp(mod
->syms
[i
].name
, name
) == 0) {
166 *crc
= symversion(mod
->crcs
, i
);
167 return mod
->syms
[i
].value
;
171 for (i
= 0; i
< mod
->num_gpl_syms
; i
++) {
172 if (strcmp(mod
->gpl_syms
[i
].name
, name
) == 0) {
173 *crc
= symversion(mod
->gpl_crcs
, i
);
174 return mod
->gpl_syms
[i
].value
;
179 DEBUGP("Failed to find symbol %s\n", name
);
183 /* Find a symbol in this elf symbol table */
184 static unsigned long find_local_symbol(Elf_Shdr
*sechdrs
,
185 unsigned int symindex
,
190 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
192 /* Search (defined) internal symbols first. */
193 for (i
= 1; i
< sechdrs
[symindex
].sh_size
/sizeof(*sym
); i
++) {
194 if (sym
[i
].st_shndx
!= SHN_UNDEF
195 && strcmp(name
, strtab
+ sym
[i
].st_name
) == 0)
196 return sym
[i
].st_value
;
201 /* Search for module by name: must hold module_mutex. */
202 static struct module
*find_module(const char *name
)
206 list_for_each_entry(mod
, &modules
, list
) {
207 if (strcmp(mod
->name
, name
) == 0)
214 /* Number of blocks used and allocated. */
215 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
216 /* Size of each block. -ve means used. */
217 static int *pcpu_size
;
219 static int split_block(unsigned int i
, unsigned short size
)
221 /* Reallocation required? */
222 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
223 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated
*2,
228 memcpy(new, pcpu_size
, sizeof(new[0])*pcpu_num_allocated
);
229 pcpu_num_allocated
*= 2;
234 /* Insert a new subblock */
235 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
236 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
239 pcpu_size
[i
+1] -= size
;
244 static inline unsigned int block_size(int val
)
251 /* Created by linker magic */
252 extern char __per_cpu_start
[], __per_cpu_end
[];
254 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
261 if (align
> SMP_CACHE_BYTES
) {
262 printk(KERN_WARNING
"%s: per-cpu alignment %li > %i\n",
263 name
, align
, SMP_CACHE_BYTES
);
264 align
= SMP_CACHE_BYTES
;
267 ptr
= __per_cpu_start
;
268 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
269 /* Extra for alignment requirement. */
270 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
271 BUG_ON(i
== 0 && extra
!= 0);
273 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
276 /* Transfer extra to previous block. */
277 if (pcpu_size
[i
-1] < 0)
278 pcpu_size
[i
-1] -= extra
;
280 pcpu_size
[i
-1] += extra
;
281 pcpu_size
[i
] -= extra
;
284 /* Split block if warranted */
285 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
286 if (!split_block(i
, size
))
290 pcpu_size
[i
] = -pcpu_size
[i
];
294 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
299 static void percpu_modfree(void *freeme
)
302 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
304 /* First entry is core kernel percpu data. */
305 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
307 pcpu_size
[i
] = -pcpu_size
[i
];
314 /* Merge with previous? */
315 if (pcpu_size
[i
-1] >= 0) {
316 pcpu_size
[i
-1] += pcpu_size
[i
];
318 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
319 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
322 /* Merge with next? */
323 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
324 pcpu_size
[i
] += pcpu_size
[i
+1];
326 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
327 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
331 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
333 const char *secstrings
)
335 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
338 static int percpu_modinit(void)
341 pcpu_num_allocated
= 2;
342 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
344 /* Static in-kernel percpu data (used). */
345 pcpu_size
[0] = -ALIGN(__per_cpu_end
-__per_cpu_start
, SMP_CACHE_BYTES
);
347 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
348 if (pcpu_size
[1] < 0) {
349 printk(KERN_ERR
"No per-cpu room for modules.\n");
355 __initcall(percpu_modinit
);
356 #else /* ... !CONFIG_SMP */
357 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
362 static inline void percpu_modfree(void *pcpuptr
)
366 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
368 const char *secstrings
)
372 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
375 /* pcpusec should be 0, and size of that section should be 0. */
378 #endif /* CONFIG_SMP */
380 #ifdef CONFIG_MODULE_UNLOAD
381 #define MODINFO_ATTR(field) \
382 static void setup_modinfo_##field(struct module *mod, const char *s) \
384 mod->field = kstrdup(s, GFP_KERNEL); \
386 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
387 struct module *mod, char *buffer) \
389 return sprintf(buffer, "%s\n", mod->field); \
391 static int modinfo_##field##_exists(struct module *mod) \
393 return mod->field != NULL; \
395 static void free_modinfo_##field(struct module *mod) \
400 static struct module_attribute modinfo_##field = { \
401 .attr = { .name = __stringify(field), .mode = 0444, \
402 .owner = THIS_MODULE }, \
403 .show = show_modinfo_##field, \
404 .setup = setup_modinfo_##field, \
405 .test = modinfo_##field##_exists, \
406 .free = free_modinfo_##field, \
409 MODINFO_ATTR(version
);
410 MODINFO_ATTR(srcversion
);
412 static struct module_attribute
*modinfo_attrs
[] = {
418 /* Init the unload section of the module. */
419 static void module_unload_init(struct module
*mod
)
423 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
424 for (i
= 0; i
< NR_CPUS
; i
++)
425 local_set(&mod
->ref
[i
].count
, 0);
426 /* Hold reference count during initialization. */
427 local_set(&mod
->ref
[raw_smp_processor_id()].count
, 1);
428 /* Backwards compatibility macros put refcount during init. */
429 mod
->waiter
= current
;
432 /* modules using other modules */
435 struct list_head list
;
436 struct module
*module_which_uses
;
439 /* Does a already use b? */
440 static int already_uses(struct module
*a
, struct module
*b
)
442 struct module_use
*use
;
444 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
445 if (use
->module_which_uses
== a
) {
446 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
450 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
454 /* Module a uses b */
455 static int use_module(struct module
*a
, struct module
*b
)
457 struct module_use
*use
;
458 if (b
== NULL
|| already_uses(a
, b
)) return 1;
460 if (!strong_try_module_get(b
))
463 DEBUGP("Allocating new usage for %s.\n", a
->name
);
464 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
466 printk("%s: out of memory loading\n", a
->name
);
471 use
->module_which_uses
= a
;
472 list_add(&use
->list
, &b
->modules_which_use_me
);
476 /* Clear the unload stuff of the module. */
477 static void module_unload_free(struct module
*mod
)
481 list_for_each_entry(i
, &modules
, list
) {
482 struct module_use
*use
;
484 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
485 if (use
->module_which_uses
== mod
) {
486 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
488 list_del(&use
->list
);
490 /* There can be at most one match. */
497 #ifdef CONFIG_MODULE_FORCE_UNLOAD
498 static inline int try_force(unsigned int flags
)
500 int ret
= (flags
& O_TRUNC
);
502 add_taint(TAINT_FORCED_MODULE
);
506 static inline int try_force(unsigned int flags
)
510 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
519 /* Whole machine is stopped with interrupts off when this runs. */
520 static int __try_stop_module(void *_sref
)
522 struct stopref
*sref
= _sref
;
524 /* If it's not unused, quit unless we are told to block. */
525 if ((sref
->flags
& O_NONBLOCK
) && module_refcount(sref
->mod
) != 0) {
526 if (!(*sref
->forced
= try_force(sref
->flags
)))
530 /* Mark it as dying. */
531 sref
->mod
->state
= MODULE_STATE_GOING
;
535 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
537 struct stopref sref
= { mod
, flags
, forced
};
539 return stop_machine_run(__try_stop_module
, &sref
, NR_CPUS
);
542 unsigned int module_refcount(struct module
*mod
)
544 unsigned int i
, total
= 0;
546 for (i
= 0; i
< NR_CPUS
; i
++)
547 total
+= local_read(&mod
->ref
[i
].count
);
550 EXPORT_SYMBOL(module_refcount
);
552 /* This exists whether we can unload or not */
553 static void free_module(struct module
*mod
);
555 static void wait_for_zero_refcount(struct module
*mod
)
557 /* Since we might sleep for some time, drop the semaphore first */
560 DEBUGP("Looking at refcount...\n");
561 set_current_state(TASK_UNINTERRUPTIBLE
);
562 if (module_refcount(mod
) == 0)
566 current
->state
= TASK_RUNNING
;
571 sys_delete_module(const char __user
*name_user
, unsigned int flags
)
574 char name
[MODULE_NAME_LEN
];
577 if (!capable(CAP_SYS_MODULE
))
580 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
582 name
[MODULE_NAME_LEN
-1] = '\0';
584 if (down_interruptible(&module_mutex
) != 0)
587 mod
= find_module(name
);
593 if (!list_empty(&mod
->modules_which_use_me
)) {
594 /* Other modules depend on us: get rid of them first. */
599 /* Doing init or already dying? */
600 if (mod
->state
!= MODULE_STATE_LIVE
) {
601 /* FIXME: if (force), slam module count and wake up
603 DEBUGP("%s already dying\n", mod
->name
);
608 /* If it has an init func, it must have an exit func to unload */
609 if ((mod
->init
!= NULL
&& mod
->exit
== NULL
)
611 forced
= try_force(flags
);
613 /* This module can't be removed */
619 /* Set this up before setting mod->state */
620 mod
->waiter
= current
;
622 /* Stop the machine so refcounts can't move and disable module. */
623 ret
= try_stop_module(mod
, flags
, &forced
);
627 /* Never wait if forced. */
628 if (!forced
&& module_refcount(mod
) != 0)
629 wait_for_zero_refcount(mod
);
631 /* Final destruction now noone is using it. */
632 if (mod
->exit
!= NULL
) {
644 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
646 struct module_use
*use
;
647 int printed_something
= 0;
649 seq_printf(m
, " %u ", module_refcount(mod
));
651 /* Always include a trailing , so userspace can differentiate
652 between this and the old multi-field proc format. */
653 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
654 printed_something
= 1;
655 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
659 printed_something
= 1;
660 seq_printf(m
, "[unsafe],");
663 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
664 printed_something
= 1;
665 seq_printf(m
, "[permanent],");
668 if (!printed_something
)
672 void __symbol_put(const char *symbol
)
674 struct module
*owner
;
676 const unsigned long *crc
;
678 spin_lock_irqsave(&modlist_lock
, flags
);
679 if (!__find_symbol(symbol
, &owner
, &crc
, 1))
682 spin_unlock_irqrestore(&modlist_lock
, flags
);
684 EXPORT_SYMBOL(__symbol_put
);
686 void symbol_put_addr(void *addr
)
690 spin_lock_irqsave(&modlist_lock
, flags
);
691 if (!kernel_text_address((unsigned long)addr
))
694 module_put(module_text_address((unsigned long)addr
));
695 spin_unlock_irqrestore(&modlist_lock
, flags
);
697 EXPORT_SYMBOL_GPL(symbol_put_addr
);
699 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
700 struct module
*mod
, char *buffer
)
702 /* sysfs holds a reference */
703 return sprintf(buffer
, "%u\n", module_refcount(mod
)-1);
706 static struct module_attribute refcnt
= {
707 .attr
= { .name
= "refcnt", .mode
= 0444, .owner
= THIS_MODULE
},
711 #else /* !CONFIG_MODULE_UNLOAD */
712 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
714 /* We don't know the usage count, or what modules are using. */
715 seq_printf(m
, " - -");
718 static inline void module_unload_free(struct module
*mod
)
722 static inline int use_module(struct module
*a
, struct module
*b
)
724 return strong_try_module_get(b
);
727 static inline void module_unload_init(struct module
*mod
)
730 #endif /* CONFIG_MODULE_UNLOAD */
732 #ifdef CONFIG_OBSOLETE_MODPARM
733 /* Bounds checking done below */
734 static int obsparm_copy_string(const char *val
, struct kernel_param
*kp
)
736 strcpy(kp
->arg
, val
);
740 static int set_obsolete(const char *val
, struct kernel_param
*kp
)
742 unsigned int min
, max
;
743 unsigned int size
, maxsize
;
747 struct obsolete_modparm
*obsparm
= kp
->arg
;
750 printk(KERN_ERR
"Parameter %s needs an argument\n", kp
->name
);
754 /* type is: [min[-max]]{b,h,i,l,s} */
756 min
= simple_strtol(p
, &endp
, 10);
757 if (endp
== obsparm
->type
)
759 else if (*endp
== '-') {
761 max
= simple_strtol(p
, &endp
, 10);
766 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
767 1, param_set_byte
, &dummy
);
769 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
770 sizeof(short), param_set_short
, &dummy
);
772 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
773 sizeof(int), param_set_int
, &dummy
);
775 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
776 sizeof(long), param_set_long
, &dummy
);
778 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
779 sizeof(char *), param_set_charp
, &dummy
);
782 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
783 and the decl is "char xxx[5][50];" */
785 maxsize
= simple_strtol(p
, &endp
, 10);
786 /* We check lengths here (yes, this is a hack). */
788 while (p
[size
= strcspn(p
, ",")]) {
795 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
796 maxsize
, obsparm_copy_string
, &dummy
);
798 printk(KERN_ERR
"Unknown obsolete parameter type %s\n", obsparm
->type
);
802 "Parameter %s doesn't fit in %u chars.\n", kp
->name
, maxsize
);
806 static int obsolete_params(const char *name
,
808 struct obsolete_modparm obsparm
[],
811 unsigned int symindex
,
814 struct kernel_param
*kp
;
818 kp
= kmalloc(sizeof(kp
[0]) * num
, GFP_KERNEL
);
822 for (i
= 0; i
< num
; i
++) {
823 char sym_name
[128 + sizeof(MODULE_SYMBOL_PREFIX
)];
825 snprintf(sym_name
, sizeof(sym_name
), "%s%s",
826 MODULE_SYMBOL_PREFIX
, obsparm
[i
].name
);
828 kp
[i
].name
= obsparm
[i
].name
;
830 kp
[i
].set
= set_obsolete
;
833 = (void *)find_local_symbol(sechdrs
, symindex
, strtab
,
835 if (!obsparm
[i
].addr
) {
836 printk("%s: falsely claims to have parameter %s\n",
837 name
, obsparm
[i
].name
);
841 kp
[i
].arg
= &obsparm
[i
];
844 ret
= parse_args(name
, args
, kp
, num
, NULL
);
850 static int obsolete_params(const char *name
,
852 struct obsolete_modparm obsparm
[],
855 unsigned int symindex
,
859 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
863 #endif /* CONFIG_OBSOLETE_MODPARM */
865 static const char vermagic
[] = VERMAGIC_STRING
;
867 #ifdef CONFIG_MODVERSIONS
868 static int check_version(Elf_Shdr
*sechdrs
,
869 unsigned int versindex
,
872 const unsigned long *crc
)
874 unsigned int i
, num_versions
;
875 struct modversion_info
*versions
;
877 /* Exporting module didn't supply crcs? OK, we're already tainted. */
881 versions
= (void *) sechdrs
[versindex
].sh_addr
;
882 num_versions
= sechdrs
[versindex
].sh_size
883 / sizeof(struct modversion_info
);
885 for (i
= 0; i
< num_versions
; i
++) {
886 if (strcmp(versions
[i
].name
, symname
) != 0)
889 if (versions
[i
].crc
== *crc
)
891 printk("%s: disagrees about version of symbol %s\n",
893 DEBUGP("Found checksum %lX vs module %lX\n",
894 *crc
, versions
[i
].crc
);
897 /* Not in module's version table. OK, but that taints the kernel. */
898 if (!(tainted
& TAINT_FORCED_MODULE
)) {
899 printk("%s: no version for \"%s\" found: kernel tainted.\n",
901 add_taint(TAINT_FORCED_MODULE
);
906 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
907 unsigned int versindex
,
910 const unsigned long *crc
;
911 struct module
*owner
;
913 if (!__find_symbol("struct_module", &owner
, &crc
, 1))
915 return check_version(sechdrs
, versindex
, "struct_module", mod
,
919 /* First part is kernel version, which we ignore. */
920 static inline int same_magic(const char *amagic
, const char *bmagic
)
922 amagic
+= strcspn(amagic
, " ");
923 bmagic
+= strcspn(bmagic
, " ");
924 return strcmp(amagic
, bmagic
) == 0;
927 static inline int check_version(Elf_Shdr
*sechdrs
,
928 unsigned int versindex
,
931 const unsigned long *crc
)
936 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
937 unsigned int versindex
,
943 static inline int same_magic(const char *amagic
, const char *bmagic
)
945 return strcmp(amagic
, bmagic
) == 0;
947 #endif /* CONFIG_MODVERSIONS */
949 /* Resolve a symbol for this module. I.e. if we find one, record usage.
950 Must be holding module_mutex. */
951 static unsigned long resolve_symbol(Elf_Shdr
*sechdrs
,
952 unsigned int versindex
,
956 struct module
*owner
;
958 const unsigned long *crc
;
960 spin_lock_irq(&modlist_lock
);
961 ret
= __find_symbol(name
, &owner
, &crc
, mod
->license_gplok
);
963 /* use_module can fail due to OOM, or module unloading */
964 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
965 !use_module(mod
, owner
))
968 spin_unlock_irq(&modlist_lock
);
974 * /sys/module/foo/sections stuff
975 * J. Corbet <corbet@lwn.net>
977 #ifdef CONFIG_KALLSYMS
978 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
979 struct module
*mod
, char *buf
)
981 struct module_sect_attr
*sattr
=
982 container_of(mattr
, struct module_sect_attr
, mattr
);
983 return sprintf(buf
, "0x%lx\n", sattr
->address
);
986 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
987 char *secstrings
, Elf_Shdr
*sechdrs
)
989 unsigned int nloaded
= 0, i
, size
[2];
990 struct module_sect_attrs
*sect_attrs
;
991 struct module_sect_attr
*sattr
;
992 struct attribute
**gattr
;
994 /* Count loaded sections and allocate structures */
995 for (i
= 0; i
< nsect
; i
++)
996 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
998 size
[0] = ALIGN(sizeof(*sect_attrs
)
999 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1000 sizeof(sect_attrs
->grp
.attrs
[0]));
1001 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1002 if (! (sect_attrs
= kmalloc(size
[0] + size
[1], GFP_KERNEL
)))
1005 /* Setup section attributes. */
1006 sect_attrs
->grp
.name
= "sections";
1007 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1009 sattr
= §_attrs
->attrs
[0];
1010 gattr
= §_attrs
->grp
.attrs
[0];
1011 for (i
= 0; i
< nsect
; i
++) {
1012 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1014 sattr
->address
= sechdrs
[i
].sh_addr
;
1015 strlcpy(sattr
->name
, secstrings
+ sechdrs
[i
].sh_name
,
1016 MODULE_SECT_NAME_LEN
);
1017 sattr
->mattr
.show
= module_sect_show
;
1018 sattr
->mattr
.store
= NULL
;
1019 sattr
->mattr
.attr
.name
= sattr
->name
;
1020 sattr
->mattr
.attr
.owner
= mod
;
1021 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1022 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1026 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1029 mod
->sect_attrs
= sect_attrs
;
1035 static void remove_sect_attrs(struct module
*mod
)
1037 if (mod
->sect_attrs
) {
1038 sysfs_remove_group(&mod
->mkobj
.kobj
,
1039 &mod
->sect_attrs
->grp
);
1040 /* We are positive that no one is using any sect attrs
1041 * at this point. Deallocate immediately. */
1042 kfree(mod
->sect_attrs
);
1043 mod
->sect_attrs
= NULL
;
1049 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1050 char *sectstrings
, Elf_Shdr
*sechdrs
)
1054 static inline void remove_sect_attrs(struct module
*mod
)
1057 #endif /* CONFIG_KALLSYMS */
1060 #ifdef CONFIG_MODULE_UNLOAD
1061 static inline int module_add_refcnt_attr(struct module
*mod
)
1063 return sysfs_create_file(&mod
->mkobj
.kobj
, &refcnt
.attr
);
1065 static void module_remove_refcnt_attr(struct module
*mod
)
1067 return sysfs_remove_file(&mod
->mkobj
.kobj
, &refcnt
.attr
);
1070 static inline int module_add_refcnt_attr(struct module
*mod
)
1074 static void module_remove_refcnt_attr(struct module
*mod
)
1079 #ifdef CONFIG_MODULE_UNLOAD
1080 static int module_add_modinfo_attrs(struct module
*mod
)
1082 struct module_attribute
*attr
;
1086 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1088 (attr
->test
&& attr
->test(mod
)))
1089 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1094 static void module_remove_modinfo_attrs(struct module
*mod
)
1096 struct module_attribute
*attr
;
1099 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1100 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1106 static int mod_sysfs_setup(struct module
*mod
,
1107 struct kernel_param
*kparam
,
1108 unsigned int num_params
)
1112 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1113 err
= kobject_set_name(&mod
->mkobj
.kobj
, "%s", mod
->name
);
1116 kobj_set_kset_s(&mod
->mkobj
, module_subsys
);
1117 mod
->mkobj
.mod
= mod
;
1118 err
= kobject_register(&mod
->mkobj
.kobj
);
1122 err
= module_add_refcnt_attr(mod
);
1126 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1130 #ifdef CONFIG_MODULE_UNLOAD
1131 err
= module_add_modinfo_attrs(mod
);
1139 kobject_unregister(&mod
->mkobj
.kobj
);
1144 static void mod_kobject_remove(struct module
*mod
)
1146 #ifdef CONFIG_MODULE_UNLOAD
1147 module_remove_modinfo_attrs(mod
);
1149 module_remove_refcnt_attr(mod
);
1150 module_param_sysfs_remove(mod
);
1152 kobject_unregister(&mod
->mkobj
.kobj
);
1156 * unlink the module with the whole machine is stopped with interrupts off
1157 * - this defends against kallsyms not taking locks
1159 static int __unlink_module(void *_mod
)
1161 struct module
*mod
= _mod
;
1162 list_del(&mod
->list
);
1166 /* Free a module, remove from lists, etc (must hold module mutex). */
1167 static void free_module(struct module
*mod
)
1169 /* Delete from various lists */
1170 stop_machine_run(__unlink_module
, mod
, NR_CPUS
);
1171 remove_sect_attrs(mod
);
1172 mod_kobject_remove(mod
);
1174 /* Arch-specific cleanup. */
1175 module_arch_cleanup(mod
);
1177 /* Module unload stuff */
1178 module_unload_free(mod
);
1180 /* This may be NULL, but that's OK */
1181 module_free(mod
, mod
->module_init
);
1184 percpu_modfree(mod
->percpu
);
1186 /* Finally, free the core (containing the module structure) */
1187 module_free(mod
, mod
->module_core
);
1190 void *__symbol_get(const char *symbol
)
1192 struct module
*owner
;
1193 unsigned long value
, flags
;
1194 const unsigned long *crc
;
1196 spin_lock_irqsave(&modlist_lock
, flags
);
1197 value
= __find_symbol(symbol
, &owner
, &crc
, 1);
1198 if (value
&& !strong_try_module_get(owner
))
1200 spin_unlock_irqrestore(&modlist_lock
, flags
);
1202 return (void *)value
;
1204 EXPORT_SYMBOL_GPL(__symbol_get
);
1206 /* Change all symbols so that sh_value encodes the pointer directly. */
1207 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1208 unsigned int symindex
,
1210 unsigned int versindex
,
1211 unsigned int pcpuindex
,
1214 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1215 unsigned long secbase
;
1216 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1219 for (i
= 1; i
< n
; i
++) {
1220 switch (sym
[i
].st_shndx
) {
1222 /* We compiled with -fno-common. These are not
1223 supposed to happen. */
1224 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1225 printk("%s: please compile with -fno-common\n",
1231 /* Don't need to do anything */
1232 DEBUGP("Absolute symbol: 0x%08lx\n",
1233 (long)sym
[i
].st_value
);
1238 = resolve_symbol(sechdrs
, versindex
,
1239 strtab
+ sym
[i
].st_name
, mod
);
1241 /* Ok if resolved. */
1242 if (sym
[i
].st_value
!= 0)
1245 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1248 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1249 mod
->name
, strtab
+ sym
[i
].st_name
);
1254 /* Divert to percpu allocation if a percpu var. */
1255 if (sym
[i
].st_shndx
== pcpuindex
)
1256 secbase
= (unsigned long)mod
->percpu
;
1258 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1259 sym
[i
].st_value
+= secbase
;
1267 /* Update size with this section: return offset. */
1268 static long get_offset(unsigned long *size
, Elf_Shdr
*sechdr
)
1272 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1273 *size
= ret
+ sechdr
->sh_size
;
1277 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1278 might -- code, read-only data, read-write data, small data. Tally
1279 sizes, and place the offsets into sh_entsize fields: high bit means it
1281 static void layout_sections(struct module
*mod
,
1282 const Elf_Ehdr
*hdr
,
1284 const char *secstrings
)
1286 static unsigned long const masks
[][2] = {
1287 /* NOTE: all executable code must be the first section
1288 * in this array; otherwise modify the text_size
1289 * finder in the two loops below */
1290 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1291 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1292 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1293 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1297 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1298 sechdrs
[i
].sh_entsize
= ~0UL;
1300 DEBUGP("Core section allocation order:\n");
1301 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1302 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1303 Elf_Shdr
*s
= &sechdrs
[i
];
1305 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1306 || (s
->sh_flags
& masks
[m
][1])
1307 || s
->sh_entsize
!= ~0UL
1308 || strncmp(secstrings
+ s
->sh_name
,
1311 s
->sh_entsize
= get_offset(&mod
->core_size
, s
);
1312 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1315 mod
->core_text_size
= mod
->core_size
;
1318 DEBUGP("Init section allocation order:\n");
1319 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1320 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1321 Elf_Shdr
*s
= &sechdrs
[i
];
1323 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1324 || (s
->sh_flags
& masks
[m
][1])
1325 || s
->sh_entsize
!= ~0UL
1326 || strncmp(secstrings
+ s
->sh_name
,
1329 s
->sh_entsize
= (get_offset(&mod
->init_size
, s
)
1330 | INIT_OFFSET_MASK
);
1331 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1334 mod
->init_text_size
= mod
->init_size
;
1338 static inline int license_is_gpl_compatible(const char *license
)
1340 return (strcmp(license
, "GPL") == 0
1341 || strcmp(license
, "GPL v2") == 0
1342 || strcmp(license
, "GPL and additional rights") == 0
1343 || strcmp(license
, "Dual BSD/GPL") == 0
1344 || strcmp(license
, "Dual MPL/GPL") == 0);
1347 static void set_license(struct module
*mod
, const char *license
)
1350 license
= "unspecified";
1352 mod
->license_gplok
= license_is_gpl_compatible(license
);
1353 if (!mod
->license_gplok
&& !(tainted
& TAINT_PROPRIETARY_MODULE
)) {
1354 printk(KERN_WARNING
"%s: module license '%s' taints kernel.\n",
1355 mod
->name
, license
);
1356 add_taint(TAINT_PROPRIETARY_MODULE
);
1360 /* Parse tag=value strings from .modinfo section */
1361 static char *next_string(char *string
, unsigned long *secsize
)
1363 /* Skip non-zero chars */
1366 if ((*secsize
)-- <= 1)
1370 /* Skip any zero padding. */
1371 while (!string
[0]) {
1373 if ((*secsize
)-- <= 1)
1379 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1384 unsigned int taglen
= strlen(tag
);
1385 unsigned long size
= sechdrs
[info
].sh_size
;
1387 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1388 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1389 return p
+ taglen
+ 1;
1394 #ifdef CONFIG_MODULE_UNLOAD
1395 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1396 unsigned int infoindex
)
1398 struct module_attribute
*attr
;
1401 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1404 get_modinfo(sechdrs
,
1411 #ifdef CONFIG_KALLSYMS
1412 int is_exported(const char *name
, const struct module
*mod
)
1417 for (i
= 0; __start___ksymtab
+i
< __stop___ksymtab
; i
++)
1418 if (strcmp(__start___ksymtab
[i
].name
, name
) == 0)
1422 for (i
= 0; i
< mod
->num_syms
; i
++)
1423 if (strcmp(mod
->syms
[i
].name
, name
) == 0)
1429 static char elf_type(const Elf_Sym
*sym
,
1431 const char *secstrings
,
1434 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1435 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1440 if (sym
->st_shndx
== SHN_UNDEF
)
1442 if (sym
->st_shndx
== SHN_ABS
)
1444 if (sym
->st_shndx
>= SHN_LORESERVE
)
1446 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1448 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1449 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1450 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1452 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1457 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1458 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1463 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1464 ".debug", strlen(".debug")) == 0)
1469 static void add_kallsyms(struct module
*mod
,
1471 unsigned int symindex
,
1472 unsigned int strindex
,
1473 const char *secstrings
)
1477 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1478 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1479 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1481 /* Set types up while we still have access to sections. */
1482 for (i
= 0; i
< mod
->num_symtab
; i
++)
1483 mod
->symtab
[i
].st_info
1484 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1487 static inline void add_kallsyms(struct module
*mod
,
1489 unsigned int symindex
,
1490 unsigned int strindex
,
1491 const char *secstrings
)
1494 #endif /* CONFIG_KALLSYMS */
1496 /* Allocate and load the module: note that size of section 0 is always
1497 zero, and we rely on this for optional sections. */
1498 static struct module
*load_module(void __user
*umod
,
1500 const char __user
*uargs
)
1504 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1505 unsigned int i
, symindex
= 0, strindex
= 0, setupindex
, exindex
,
1506 exportindex
, modindex
, obsparmindex
, infoindex
, gplindex
,
1507 crcindex
, gplcrcindex
, versindex
, pcpuindex
;
1511 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1512 struct exception_table_entry
*extable
;
1513 mm_segment_t old_fs
;
1515 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1517 if (len
< sizeof(*hdr
))
1518 return ERR_PTR(-ENOEXEC
);
1520 /* Suck in entire file: we'll want most of it. */
1521 /* vmalloc barfs on "unusual" numbers. Check here */
1522 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1523 return ERR_PTR(-ENOMEM
);
1524 if (copy_from_user(hdr
, umod
, len
) != 0) {
1529 /* Sanity checks against insmoding binaries or wrong arch,
1530 weird elf version */
1531 if (memcmp(hdr
->e_ident
, ELFMAG
, 4) != 0
1532 || hdr
->e_type
!= ET_REL
1533 || !elf_check_arch(hdr
)
1534 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1539 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1542 /* Convenience variables */
1543 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1544 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1545 sechdrs
[0].sh_addr
= 0;
1547 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1548 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1549 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1552 /* Mark all sections sh_addr with their address in the
1554 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1556 /* Internal symbols and strings. */
1557 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1559 strindex
= sechdrs
[i
].sh_link
;
1560 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1562 #ifndef CONFIG_MODULE_UNLOAD
1563 /* Don't load .exit sections */
1564 if (strncmp(secstrings
+sechdrs
[i
].sh_name
, ".exit", 5) == 0)
1565 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1569 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1570 ".gnu.linkonce.this_module");
1572 printk(KERN_WARNING
"No module found in object\n");
1576 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1578 if (symindex
== 0) {
1579 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1585 /* Optional sections */
1586 exportindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab");
1587 gplindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl");
1588 crcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab");
1589 gplcrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
1590 setupindex
= find_sec(hdr
, sechdrs
, secstrings
, "__param");
1591 exindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ex_table");
1592 obsparmindex
= find_sec(hdr
, sechdrs
, secstrings
, "__obsparm");
1593 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1594 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1595 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1597 /* Don't keep modinfo section */
1598 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1599 #ifdef CONFIG_KALLSYMS
1600 /* Keep symbol and string tables for decoding later. */
1601 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1602 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1605 /* Check module struct version now, before we try to use module. */
1606 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1611 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1612 /* This is allowed: modprobe --force will invalidate it. */
1614 add_taint(TAINT_FORCED_MODULE
);
1615 printk(KERN_WARNING
"%s: no version magic, tainting kernel.\n",
1617 } else if (!same_magic(modmagic
, vermagic
)) {
1618 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
1619 mod
->name
, modmagic
, vermagic
);
1624 /* Now copy in args */
1625 arglen
= strlen_user(uargs
);
1630 args
= kmalloc(arglen
, GFP_KERNEL
);
1635 if (copy_from_user(args
, uargs
, arglen
) != 0) {
1640 if (find_module(mod
->name
)) {
1645 mod
->state
= MODULE_STATE_COMING
;
1647 /* Allow arches to frob section contents and sizes. */
1648 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
1653 /* We have a special allocation for this section. */
1654 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
1655 sechdrs
[pcpuindex
].sh_addralign
,
1661 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1662 mod
->percpu
= percpu
;
1665 /* Determine total sizes, and put offsets in sh_entsize. For now
1666 this is done generically; there doesn't appear to be any
1667 special cases for the architectures. */
1668 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
1670 /* Do the allocs. */
1671 ptr
= module_alloc(mod
->core_size
);
1676 memset(ptr
, 0, mod
->core_size
);
1677 mod
->module_core
= ptr
;
1679 ptr
= module_alloc(mod
->init_size
);
1680 if (!ptr
&& mod
->init_size
) {
1684 memset(ptr
, 0, mod
->init_size
);
1685 mod
->module_init
= ptr
;
1687 /* Transfer each section which specifies SHF_ALLOC */
1688 DEBUGP("final section addresses:\n");
1689 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
1692 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1695 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
1696 dest
= mod
->module_init
1697 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
1699 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
1701 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
1702 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
1703 sechdrs
[i
].sh_size
);
1704 /* Update sh_addr to point to copy in image. */
1705 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
1706 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
1708 /* Module has been moved. */
1709 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1711 /* Now we've moved module, initialize linked lists, etc. */
1712 module_unload_init(mod
);
1714 /* Set up license info based on the info section */
1715 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
1717 #ifdef CONFIG_MODULE_UNLOAD
1718 /* Set up MODINFO_ATTR fields */
1719 setup_modinfo(mod
, sechdrs
, infoindex
);
1722 /* Fix up syms, so that st_value is a pointer to location. */
1723 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
1728 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1729 mod
->num_syms
= sechdrs
[exportindex
].sh_size
/ sizeof(*mod
->syms
);
1730 mod
->syms
= (void *)sechdrs
[exportindex
].sh_addr
;
1732 mod
->crcs
= (void *)sechdrs
[crcindex
].sh_addr
;
1733 mod
->num_gpl_syms
= sechdrs
[gplindex
].sh_size
/ sizeof(*mod
->gpl_syms
);
1734 mod
->gpl_syms
= (void *)sechdrs
[gplindex
].sh_addr
;
1736 mod
->gpl_crcs
= (void *)sechdrs
[gplcrcindex
].sh_addr
;
1738 #ifdef CONFIG_MODVERSIONS
1739 if ((mod
->num_syms
&& !crcindex
) ||
1740 (mod
->num_gpl_syms
&& !gplcrcindex
)) {
1741 printk(KERN_WARNING
"%s: No versions for exported symbols."
1742 " Tainting kernel.\n", mod
->name
);
1743 add_taint(TAINT_FORCED_MODULE
);
1747 /* Now do relocations. */
1748 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1749 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
1750 unsigned int info
= sechdrs
[i
].sh_info
;
1752 /* Not a valid relocation section? */
1753 if (info
>= hdr
->e_shnum
)
1756 /* Don't bother with non-allocated sections */
1757 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
1760 if (sechdrs
[i
].sh_type
== SHT_REL
)
1761 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
1762 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
1763 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
1769 /* Set up and sort exception table */
1770 mod
->num_exentries
= sechdrs
[exindex
].sh_size
/ sizeof(*mod
->extable
);
1771 mod
->extable
= extable
= (void *)sechdrs
[exindex
].sh_addr
;
1772 sort_extable(extable
, extable
+ mod
->num_exentries
);
1774 /* Finally, copy percpu area over. */
1775 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
1776 sechdrs
[pcpuindex
].sh_size
);
1778 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
1780 err
= module_finalize(hdr
, sechdrs
, mod
);
1784 /* flush the icache in correct context */
1789 * Flush the instruction cache, since we've played with text.
1790 * Do it before processing of module parameters, so the module
1791 * can provide parameter accessor functions of its own.
1793 if (mod
->module_init
)
1794 flush_icache_range((unsigned long)mod
->module_init
,
1795 (unsigned long)mod
->module_init
1797 flush_icache_range((unsigned long)mod
->module_core
,
1798 (unsigned long)mod
->module_core
+ mod
->core_size
);
1804 err
= obsolete_params(mod
->name
, mod
->args
,
1805 (struct obsolete_modparm
*)
1806 sechdrs
[obsparmindex
].sh_addr
,
1807 sechdrs
[obsparmindex
].sh_size
1808 / sizeof(struct obsolete_modparm
),
1810 (char *)sechdrs
[strindex
].sh_addr
);
1812 printk(KERN_WARNING
"%s: Ignoring new-style "
1813 "parameters in presence of obsolete ones\n",
1816 /* Size of section 0 is 0, so this works well if no params */
1817 err
= parse_args(mod
->name
, mod
->args
,
1818 (struct kernel_param
*)
1819 sechdrs
[setupindex
].sh_addr
,
1820 sechdrs
[setupindex
].sh_size
1821 / sizeof(struct kernel_param
),
1827 err
= mod_sysfs_setup(mod
,
1828 (struct kernel_param
*)
1829 sechdrs
[setupindex
].sh_addr
,
1830 sechdrs
[setupindex
].sh_size
1831 / sizeof(struct kernel_param
));
1834 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
1836 /* Get rid of temporary copy */
1843 module_arch_cleanup(mod
);
1845 module_unload_free(mod
);
1846 module_free(mod
, mod
->module_init
);
1848 module_free(mod
, mod
->module_core
);
1851 percpu_modfree(percpu
);
1856 if (err
< 0) return ERR_PTR(err
);
1860 printk(KERN_ERR
"Module len %lu truncated\n", len
);
1866 * link the module with the whole machine is stopped with interrupts off
1867 * - this defends against kallsyms not taking locks
1869 static int __link_module(void *_mod
)
1871 struct module
*mod
= _mod
;
1872 list_add(&mod
->list
, &modules
);
1876 /* This is where the real work happens */
1878 sys_init_module(void __user
*umod
,
1880 const char __user
*uargs
)
1885 /* Must have permission */
1886 if (!capable(CAP_SYS_MODULE
))
1889 /* Only one module load at a time, please */
1890 if (down_interruptible(&module_mutex
) != 0)
1893 /* Do all the hard work */
1894 mod
= load_module(umod
, len
, uargs
);
1897 return PTR_ERR(mod
);
1900 /* Now sew it into the lists. They won't access us, since
1901 strong_try_module_get() will fail. */
1902 stop_machine_run(__link_module
, mod
, NR_CPUS
);
1904 /* Drop lock so they can recurse */
1907 down(¬ify_mutex
);
1908 notifier_call_chain(&module_notify_list
, MODULE_STATE_COMING
, mod
);
1911 /* Start the module */
1912 if (mod
->init
!= NULL
)
1915 /* Init routine failed: abort. Try to protect us from
1916 buggy refcounters. */
1917 mod
->state
= MODULE_STATE_GOING
;
1918 synchronize_sched();
1920 printk(KERN_ERR
"%s: module is now stuck!\n",
1924 down(&module_mutex
);
1931 /* Now it's a first class citizen! */
1932 down(&module_mutex
);
1933 mod
->state
= MODULE_STATE_LIVE
;
1934 /* Drop initial reference. */
1936 module_free(mod
, mod
->module_init
);
1937 mod
->module_init
= NULL
;
1939 mod
->init_text_size
= 0;
1945 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
1947 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
1950 #ifdef CONFIG_KALLSYMS
1952 * This ignores the intensely annoying "mapping symbols" found
1953 * in ARM ELF files: $a, $t and $d.
1955 static inline int is_arm_mapping_symbol(const char *str
)
1957 return str
[0] == '$' && strchr("atd", str
[1])
1958 && (str
[2] == '\0' || str
[2] == '.');
1961 static const char *get_ksymbol(struct module
*mod
,
1963 unsigned long *size
,
1964 unsigned long *offset
)
1966 unsigned int i
, best
= 0;
1967 unsigned long nextval
;
1969 /* At worse, next value is at end of module */
1970 if (within(addr
, mod
->module_init
, mod
->init_size
))
1971 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
1973 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
1975 /* Scan for closest preceeding symbol, and next symbol. (ELF
1976 starts real symbols at 1). */
1977 for (i
= 1; i
< mod
->num_symtab
; i
++) {
1978 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
1981 /* We ignore unnamed symbols: they're uninformative
1982 * and inserted at a whim. */
1983 if (mod
->symtab
[i
].st_value
<= addr
1984 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
1985 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
1986 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
1988 if (mod
->symtab
[i
].st_value
> addr
1989 && mod
->symtab
[i
].st_value
< nextval
1990 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
1991 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
1992 nextval
= mod
->symtab
[i
].st_value
;
1998 *size
= nextval
- mod
->symtab
[best
].st_value
;
1999 *offset
= addr
- mod
->symtab
[best
].st_value
;
2000 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2003 /* For kallsyms to ask for address resolution. NULL means not found.
2004 We don't lock, as this is used for oops resolution and races are a
2006 const char *module_address_lookup(unsigned long addr
,
2007 unsigned long *size
,
2008 unsigned long *offset
,
2013 list_for_each_entry(mod
, &modules
, list
) {
2014 if (within(addr
, mod
->module_init
, mod
->init_size
)
2015 || within(addr
, mod
->module_core
, mod
->core_size
)) {
2016 *modname
= mod
->name
;
2017 return get_ksymbol(mod
, addr
, size
, offset
);
2023 struct module
*module_get_kallsym(unsigned int symnum
,
2024 unsigned long *value
,
2030 down(&module_mutex
);
2031 list_for_each_entry(mod
, &modules
, list
) {
2032 if (symnum
< mod
->num_symtab
) {
2033 *value
= mod
->symtab
[symnum
].st_value
;
2034 *type
= mod
->symtab
[symnum
].st_info
;
2036 mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2041 symnum
-= mod
->num_symtab
;
2047 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2051 for (i
= 0; i
< mod
->num_symtab
; i
++)
2052 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0)
2053 return mod
->symtab
[i
].st_value
;
2057 /* Look for this name: can be of form module:name. */
2058 unsigned long module_kallsyms_lookup_name(const char *name
)
2062 unsigned long ret
= 0;
2064 /* Don't lock: we're in enough trouble already. */
2065 if ((colon
= strchr(name
, ':')) != NULL
) {
2067 if ((mod
= find_module(name
)) != NULL
)
2068 ret
= mod_find_symname(mod
, colon
+1);
2071 list_for_each_entry(mod
, &modules
, list
)
2072 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2077 #endif /* CONFIG_KALLSYMS */
2079 /* Called by the /proc file system to return a list of modules. */
2080 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2082 struct list_head
*i
;
2085 down(&module_mutex
);
2086 list_for_each(i
, &modules
) {
2095 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2097 struct list_head
*i
= p
;
2099 if (i
->next
== &modules
)
2104 static void m_stop(struct seq_file
*m
, void *p
)
2109 static int m_show(struct seq_file
*m
, void *p
)
2111 struct module
*mod
= list_entry(p
, struct module
, list
);
2112 seq_printf(m
, "%s %lu",
2113 mod
->name
, mod
->init_size
+ mod
->core_size
);
2114 print_unload_info(m
, mod
);
2116 /* Informative for users. */
2117 seq_printf(m
, " %s",
2118 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2119 mod
->state
== MODULE_STATE_COMING
? "Loading":
2121 /* Used by oprofile and other similar tools. */
2122 seq_printf(m
, " 0x%p", mod
->module_core
);
2124 seq_printf(m
, "\n");
2128 /* Format: modulename size refcount deps address
2130 Where refcount is a number or -, and deps is a comma-separated list
2133 struct seq_operations modules_op
= {
2140 /* Given an address, look for it in the module exception tables. */
2141 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2143 unsigned long flags
;
2144 const struct exception_table_entry
*e
= NULL
;
2147 spin_lock_irqsave(&modlist_lock
, flags
);
2148 list_for_each_entry(mod
, &modules
, list
) {
2149 if (mod
->num_exentries
== 0)
2152 e
= search_extable(mod
->extable
,
2153 mod
->extable
+ mod
->num_exentries
- 1,
2158 spin_unlock_irqrestore(&modlist_lock
, flags
);
2160 /* Now, if we found one, we are running inside it now, hence
2161 we cannot unload the module, hence no refcnt needed. */
2165 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2166 struct module
*__module_text_address(unsigned long addr
)
2170 list_for_each_entry(mod
, &modules
, list
)
2171 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
2172 || within(addr
, mod
->module_core
, mod
->core_text_size
))
2177 struct module
*module_text_address(unsigned long addr
)
2180 unsigned long flags
;
2182 spin_lock_irqsave(&modlist_lock
, flags
);
2183 mod
= __module_text_address(addr
);
2184 spin_unlock_irqrestore(&modlist_lock
, flags
);
2189 /* Don't grab lock, we're oopsing. */
2190 void print_modules(void)
2194 printk("Modules linked in:");
2195 list_for_each_entry(mod
, &modules
, list
)
2196 printk(" %s", mod
->name
);
2200 void module_add_driver(struct module
*mod
, struct device_driver
*drv
)
2205 /* Don't check return code; this call is idempotent */
2206 sysfs_create_link(&drv
->kobj
, &mod
->mkobj
.kobj
, "module");
2208 EXPORT_SYMBOL(module_add_driver
);
2210 void module_remove_driver(struct device_driver
*drv
)
2214 sysfs_remove_link(&drv
->kobj
, "module");
2216 EXPORT_SYMBOL(module_remove_driver
);
2218 #ifdef CONFIG_MODVERSIONS
2219 /* Generate the signature for struct module here, too, for modversions. */
2220 void struct_module(struct module
*mod
) { return; }
2221 EXPORT_SYMBOL(struct_module
);