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
;
1513 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1515 if (len
< sizeof(*hdr
))
1516 return ERR_PTR(-ENOEXEC
);
1518 /* Suck in entire file: we'll want most of it. */
1519 /* vmalloc barfs on "unusual" numbers. Check here */
1520 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1521 return ERR_PTR(-ENOMEM
);
1522 if (copy_from_user(hdr
, umod
, len
) != 0) {
1527 /* Sanity checks against insmoding binaries or wrong arch,
1528 weird elf version */
1529 if (memcmp(hdr
->e_ident
, ELFMAG
, 4) != 0
1530 || hdr
->e_type
!= ET_REL
1531 || !elf_check_arch(hdr
)
1532 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1537 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1540 /* Convenience variables */
1541 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1542 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1543 sechdrs
[0].sh_addr
= 0;
1545 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1546 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1547 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1550 /* Mark all sections sh_addr with their address in the
1552 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1554 /* Internal symbols and strings. */
1555 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1557 strindex
= sechdrs
[i
].sh_link
;
1558 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1560 #ifndef CONFIG_MODULE_UNLOAD
1561 /* Don't load .exit sections */
1562 if (strncmp(secstrings
+sechdrs
[i
].sh_name
, ".exit", 5) == 0)
1563 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1567 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1568 ".gnu.linkonce.this_module");
1570 printk(KERN_WARNING
"No module found in object\n");
1574 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1576 if (symindex
== 0) {
1577 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1583 /* Optional sections */
1584 exportindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab");
1585 gplindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl");
1586 crcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab");
1587 gplcrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
1588 setupindex
= find_sec(hdr
, sechdrs
, secstrings
, "__param");
1589 exindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ex_table");
1590 obsparmindex
= find_sec(hdr
, sechdrs
, secstrings
, "__obsparm");
1591 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1592 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1593 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1595 /* Don't keep modinfo section */
1596 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1597 #ifdef CONFIG_KALLSYMS
1598 /* Keep symbol and string tables for decoding later. */
1599 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1600 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1603 /* Check module struct version now, before we try to use module. */
1604 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1609 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1610 /* This is allowed: modprobe --force will invalidate it. */
1612 tainted
|= TAINT_FORCED_MODULE
;
1613 printk(KERN_WARNING
"%s: no version magic, tainting kernel.\n",
1615 } else if (!same_magic(modmagic
, vermagic
)) {
1616 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
1617 mod
->name
, modmagic
, vermagic
);
1622 /* Now copy in args */
1623 arglen
= strlen_user(uargs
);
1628 args
= kmalloc(arglen
, GFP_KERNEL
);
1633 if (copy_from_user(args
, uargs
, arglen
) != 0) {
1638 if (find_module(mod
->name
)) {
1643 mod
->state
= MODULE_STATE_COMING
;
1645 /* Allow arches to frob section contents and sizes. */
1646 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
1651 /* We have a special allocation for this section. */
1652 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
1653 sechdrs
[pcpuindex
].sh_addralign
,
1659 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1660 mod
->percpu
= percpu
;
1663 /* Determine total sizes, and put offsets in sh_entsize. For now
1664 this is done generically; there doesn't appear to be any
1665 special cases for the architectures. */
1666 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
1668 /* Do the allocs. */
1669 ptr
= module_alloc(mod
->core_size
);
1674 memset(ptr
, 0, mod
->core_size
);
1675 mod
->module_core
= ptr
;
1677 ptr
= module_alloc(mod
->init_size
);
1678 if (!ptr
&& mod
->init_size
) {
1682 memset(ptr
, 0, mod
->init_size
);
1683 mod
->module_init
= ptr
;
1685 /* Transfer each section which specifies SHF_ALLOC */
1686 DEBUGP("final section addresses:\n");
1687 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
1690 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1693 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
1694 dest
= mod
->module_init
1695 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
1697 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
1699 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
1700 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
1701 sechdrs
[i
].sh_size
);
1702 /* Update sh_addr to point to copy in image. */
1703 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
1704 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
1706 /* Module has been moved. */
1707 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1709 /* Now we've moved module, initialize linked lists, etc. */
1710 module_unload_init(mod
);
1712 /* Set up license info based on the info section */
1713 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
1715 #ifdef CONFIG_MODULE_UNLOAD
1716 /* Set up MODINFO_ATTR fields */
1717 setup_modinfo(mod
, sechdrs
, infoindex
);
1720 /* Fix up syms, so that st_value is a pointer to location. */
1721 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
1726 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1727 mod
->num_syms
= sechdrs
[exportindex
].sh_size
/ sizeof(*mod
->syms
);
1728 mod
->syms
= (void *)sechdrs
[exportindex
].sh_addr
;
1730 mod
->crcs
= (void *)sechdrs
[crcindex
].sh_addr
;
1731 mod
->num_gpl_syms
= sechdrs
[gplindex
].sh_size
/ sizeof(*mod
->gpl_syms
);
1732 mod
->gpl_syms
= (void *)sechdrs
[gplindex
].sh_addr
;
1734 mod
->gpl_crcs
= (void *)sechdrs
[gplcrcindex
].sh_addr
;
1736 #ifdef CONFIG_MODVERSIONS
1737 if ((mod
->num_syms
&& !crcindex
) ||
1738 (mod
->num_gpl_syms
&& !gplcrcindex
)) {
1739 printk(KERN_WARNING
"%s: No versions for exported symbols."
1740 " Tainting kernel.\n", mod
->name
);
1741 tainted
|= TAINT_FORCED_MODULE
;
1745 /* Now do relocations. */
1746 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1747 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
1748 unsigned int info
= sechdrs
[i
].sh_info
;
1750 /* Not a valid relocation section? */
1751 if (info
>= hdr
->e_shnum
)
1754 /* Don't bother with non-allocated sections */
1755 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
1758 if (sechdrs
[i
].sh_type
== SHT_REL
)
1759 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
1760 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
1761 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
1767 /* Set up and sort exception table */
1768 mod
->num_exentries
= sechdrs
[exindex
].sh_size
/ sizeof(*mod
->extable
);
1769 mod
->extable
= extable
= (void *)sechdrs
[exindex
].sh_addr
;
1770 sort_extable(extable
, extable
+ mod
->num_exentries
);
1772 /* Finally, copy percpu area over. */
1773 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
1774 sechdrs
[pcpuindex
].sh_size
);
1776 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
1778 err
= module_finalize(hdr
, sechdrs
, mod
);
1784 err
= obsolete_params(mod
->name
, mod
->args
,
1785 (struct obsolete_modparm
*)
1786 sechdrs
[obsparmindex
].sh_addr
,
1787 sechdrs
[obsparmindex
].sh_size
1788 / sizeof(struct obsolete_modparm
),
1790 (char *)sechdrs
[strindex
].sh_addr
);
1792 printk(KERN_WARNING
"%s: Ignoring new-style "
1793 "parameters in presence of obsolete ones\n",
1796 /* Size of section 0 is 0, so this works well if no params */
1797 err
= parse_args(mod
->name
, mod
->args
,
1798 (struct kernel_param
*)
1799 sechdrs
[setupindex
].sh_addr
,
1800 sechdrs
[setupindex
].sh_size
1801 / sizeof(struct kernel_param
),
1807 err
= mod_sysfs_setup(mod
,
1808 (struct kernel_param
*)
1809 sechdrs
[setupindex
].sh_addr
,
1810 sechdrs
[setupindex
].sh_size
1811 / sizeof(struct kernel_param
));
1814 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
1816 /* Get rid of temporary copy */
1823 module_arch_cleanup(mod
);
1825 module_unload_free(mod
);
1826 module_free(mod
, mod
->module_init
);
1828 module_free(mod
, mod
->module_core
);
1831 percpu_modfree(percpu
);
1836 if (err
< 0) return ERR_PTR(err
);
1840 printk(KERN_ERR
"Module len %lu truncated\n", len
);
1846 * link the module with the whole machine is stopped with interrupts off
1847 * - this defends against kallsyms not taking locks
1849 static int __link_module(void *_mod
)
1851 struct module
*mod
= _mod
;
1852 list_add(&mod
->list
, &modules
);
1856 /* This is where the real work happens */
1858 sys_init_module(void __user
*umod
,
1860 const char __user
*uargs
)
1863 mm_segment_t old_fs
= get_fs();
1866 /* Must have permission */
1867 if (!capable(CAP_SYS_MODULE
))
1870 /* Only one module load at a time, please */
1871 if (down_interruptible(&module_mutex
) != 0)
1874 /* Do all the hard work */
1875 mod
= load_module(umod
, len
, uargs
);
1878 return PTR_ERR(mod
);
1881 /* flush the icache in correct context */
1884 /* Flush the instruction cache, since we've played with text */
1885 if (mod
->module_init
)
1886 flush_icache_range((unsigned long)mod
->module_init
,
1887 (unsigned long)mod
->module_init
1889 flush_icache_range((unsigned long)mod
->module_core
,
1890 (unsigned long)mod
->module_core
+ mod
->core_size
);
1894 /* Now sew it into the lists. They won't access us, since
1895 strong_try_module_get() will fail. */
1896 stop_machine_run(__link_module
, mod
, NR_CPUS
);
1898 /* Drop lock so they can recurse */
1901 down(¬ify_mutex
);
1902 notifier_call_chain(&module_notify_list
, MODULE_STATE_COMING
, mod
);
1905 /* Start the module */
1906 if (mod
->init
!= NULL
)
1909 /* Init routine failed: abort. Try to protect us from
1910 buggy refcounters. */
1911 mod
->state
= MODULE_STATE_GOING
;
1912 synchronize_sched();
1914 printk(KERN_ERR
"%s: module is now stuck!\n",
1918 down(&module_mutex
);
1925 /* Now it's a first class citizen! */
1926 down(&module_mutex
);
1927 mod
->state
= MODULE_STATE_LIVE
;
1928 /* Drop initial reference. */
1930 module_free(mod
, mod
->module_init
);
1931 mod
->module_init
= NULL
;
1933 mod
->init_text_size
= 0;
1939 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
1941 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
1944 #ifdef CONFIG_KALLSYMS
1946 * This ignores the intensely annoying "mapping symbols" found
1947 * in ARM ELF files: $a, $t and $d.
1949 static inline int is_arm_mapping_symbol(const char *str
)
1951 return str
[0] == '$' && strchr("atd", str
[1])
1952 && (str
[2] == '\0' || str
[2] == '.');
1955 static const char *get_ksymbol(struct module
*mod
,
1957 unsigned long *size
,
1958 unsigned long *offset
)
1960 unsigned int i
, best
= 0;
1961 unsigned long nextval
;
1963 /* At worse, next value is at end of module */
1964 if (within(addr
, mod
->module_init
, mod
->init_size
))
1965 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
1967 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
1969 /* Scan for closest preceeding symbol, and next symbol. (ELF
1970 starts real symbols at 1). */
1971 for (i
= 1; i
< mod
->num_symtab
; i
++) {
1972 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
1975 /* We ignore unnamed symbols: they're uninformative
1976 * and inserted at a whim. */
1977 if (mod
->symtab
[i
].st_value
<= addr
1978 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
1979 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
1980 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
1982 if (mod
->symtab
[i
].st_value
> addr
1983 && mod
->symtab
[i
].st_value
< nextval
1984 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
1985 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
1986 nextval
= mod
->symtab
[i
].st_value
;
1992 *size
= nextval
- mod
->symtab
[best
].st_value
;
1993 *offset
= addr
- mod
->symtab
[best
].st_value
;
1994 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
1997 /* For kallsyms to ask for address resolution. NULL means not found.
1998 We don't lock, as this is used for oops resolution and races are a
2000 const char *module_address_lookup(unsigned long addr
,
2001 unsigned long *size
,
2002 unsigned long *offset
,
2007 list_for_each_entry(mod
, &modules
, list
) {
2008 if (within(addr
, mod
->module_init
, mod
->init_size
)
2009 || within(addr
, mod
->module_core
, mod
->core_size
)) {
2010 *modname
= mod
->name
;
2011 return get_ksymbol(mod
, addr
, size
, offset
);
2017 struct module
*module_get_kallsym(unsigned int symnum
,
2018 unsigned long *value
,
2024 down(&module_mutex
);
2025 list_for_each_entry(mod
, &modules
, list
) {
2026 if (symnum
< mod
->num_symtab
) {
2027 *value
= mod
->symtab
[symnum
].st_value
;
2028 *type
= mod
->symtab
[symnum
].st_info
;
2030 mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2035 symnum
-= mod
->num_symtab
;
2041 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2045 for (i
= 0; i
< mod
->num_symtab
; i
++)
2046 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0)
2047 return mod
->symtab
[i
].st_value
;
2051 /* Look for this name: can be of form module:name. */
2052 unsigned long module_kallsyms_lookup_name(const char *name
)
2056 unsigned long ret
= 0;
2058 /* Don't lock: we're in enough trouble already. */
2059 if ((colon
= strchr(name
, ':')) != NULL
) {
2061 if ((mod
= find_module(name
)) != NULL
)
2062 ret
= mod_find_symname(mod
, colon
+1);
2065 list_for_each_entry(mod
, &modules
, list
)
2066 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2071 #endif /* CONFIG_KALLSYMS */
2073 /* Called by the /proc file system to return a list of modules. */
2074 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2076 struct list_head
*i
;
2079 down(&module_mutex
);
2080 list_for_each(i
, &modules
) {
2089 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2091 struct list_head
*i
= p
;
2093 if (i
->next
== &modules
)
2098 static void m_stop(struct seq_file
*m
, void *p
)
2103 static int m_show(struct seq_file
*m
, void *p
)
2105 struct module
*mod
= list_entry(p
, struct module
, list
);
2106 seq_printf(m
, "%s %lu",
2107 mod
->name
, mod
->init_size
+ mod
->core_size
);
2108 print_unload_info(m
, mod
);
2110 /* Informative for users. */
2111 seq_printf(m
, " %s",
2112 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2113 mod
->state
== MODULE_STATE_COMING
? "Loading":
2115 /* Used by oprofile and other similar tools. */
2116 seq_printf(m
, " 0x%p", mod
->module_core
);
2118 seq_printf(m
, "\n");
2122 /* Format: modulename size refcount deps address
2124 Where refcount is a number or -, and deps is a comma-separated list
2127 struct seq_operations modules_op
= {
2134 /* Given an address, look for it in the module exception tables. */
2135 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2137 unsigned long flags
;
2138 const struct exception_table_entry
*e
= NULL
;
2141 spin_lock_irqsave(&modlist_lock
, flags
);
2142 list_for_each_entry(mod
, &modules
, list
) {
2143 if (mod
->num_exentries
== 0)
2146 e
= search_extable(mod
->extable
,
2147 mod
->extable
+ mod
->num_exentries
- 1,
2152 spin_unlock_irqrestore(&modlist_lock
, flags
);
2154 /* Now, if we found one, we are running inside it now, hence
2155 we cannot unload the module, hence no refcnt needed. */
2159 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2160 struct module
*__module_text_address(unsigned long addr
)
2164 list_for_each_entry(mod
, &modules
, list
)
2165 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
2166 || within(addr
, mod
->module_core
, mod
->core_text_size
))
2171 struct module
*module_text_address(unsigned long addr
)
2174 unsigned long flags
;
2176 spin_lock_irqsave(&modlist_lock
, flags
);
2177 mod
= __module_text_address(addr
);
2178 spin_unlock_irqrestore(&modlist_lock
, flags
);
2183 /* Don't grab lock, we're oopsing. */
2184 void print_modules(void)
2188 printk("Modules linked in:");
2189 list_for_each_entry(mod
, &modules
, list
)
2190 printk(" %s", mod
->name
);
2194 void module_add_driver(struct module
*mod
, struct device_driver
*drv
)
2199 /* Don't check return code; this call is idempotent */
2200 sysfs_create_link(&drv
->kobj
, &mod
->mkobj
.kobj
, "module");
2202 EXPORT_SYMBOL(module_add_driver
);
2204 void module_remove_driver(struct device_driver
*drv
)
2208 sysfs_remove_link(&drv
->kobj
, "module");
2210 EXPORT_SYMBOL(module_remove_driver
);
2212 #ifdef CONFIG_MODVERSIONS
2213 /* Generate the signature for struct module here, too, for modversions. */
2214 void struct_module(struct module
*mod
) { return; }
2215 EXPORT_SYMBOL(struct_module
);