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 <asm/uaccess.h>
38 #include <asm/semaphore.h>
39 #include <asm/cacheflush.h>
44 #define DEBUGP(fmt , a...)
47 #ifndef ARCH_SHF_SMALL
48 #define ARCH_SHF_SMALL 0
51 /* If this is set, the section belongs in the init part of the module */
52 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
54 /* Protects module list */
55 static spinlock_t modlist_lock
= SPIN_LOCK_UNLOCKED
;
57 /* List of modules, protected by module_mutex AND modlist_lock */
58 static DECLARE_MUTEX(module_mutex
);
59 static LIST_HEAD(modules
);
61 static DECLARE_MUTEX(notify_mutex
);
62 static struct notifier_block
* module_notify_list
;
64 int register_module_notifier(struct notifier_block
* nb
)
68 err
= notifier_chain_register(&module_notify_list
, nb
);
72 EXPORT_SYMBOL(register_module_notifier
);
74 int unregister_module_notifier(struct notifier_block
* nb
)
78 err
= notifier_chain_unregister(&module_notify_list
, nb
);
82 EXPORT_SYMBOL(unregister_module_notifier
);
84 /* We require a truly strong try_module_get() */
85 static inline int strong_try_module_get(struct module
*mod
)
87 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
89 return try_module_get(mod
);
92 /* A thread that wants to hold a reference to a module only while it
93 * is running can call ths to safely exit.
94 * nfsd and lockd use this.
96 void __module_put_and_exit(struct module
*mod
, long code
)
101 EXPORT_SYMBOL(__module_put_and_exit
);
103 /* Find a module section: 0 means not found. */
104 static unsigned int find_sec(Elf_Ehdr
*hdr
,
106 const char *secstrings
,
111 for (i
= 1; i
< hdr
->e_shnum
; i
++)
112 /* Alloc bit cleared means "ignore it." */
113 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
114 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
119 /* Provided by the linker */
120 extern const struct kernel_symbol __start___ksymtab
[];
121 extern const struct kernel_symbol __stop___ksymtab
[];
122 extern const struct kernel_symbol __start___ksymtab_gpl
[];
123 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
124 extern const unsigned long __start___kcrctab
[];
125 extern const unsigned long __start___kcrctab_gpl
[];
127 #ifndef CONFIG_MODVERSIONS
128 #define symversion(base, idx) NULL
130 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
133 /* Find a symbol, return value, crc and module which owns it */
134 static unsigned long __find_symbol(const char *name
,
135 struct module
**owner
,
136 const unsigned long **crc
,
142 /* Core kernel first. */
144 for (i
= 0; __start___ksymtab
+i
< __stop___ksymtab
; i
++) {
145 if (strcmp(__start___ksymtab
[i
].name
, name
) == 0) {
146 *crc
= symversion(__start___kcrctab
, i
);
147 return __start___ksymtab
[i
].value
;
151 for (i
= 0; __start___ksymtab_gpl
+i
<__stop___ksymtab_gpl
; i
++)
152 if (strcmp(__start___ksymtab_gpl
[i
].name
, name
) == 0) {
153 *crc
= symversion(__start___kcrctab_gpl
, i
);
154 return __start___ksymtab_gpl
[i
].value
;
158 /* Now try modules. */
159 list_for_each_entry(mod
, &modules
, list
) {
161 for (i
= 0; i
< mod
->num_syms
; i
++)
162 if (strcmp(mod
->syms
[i
].name
, name
) == 0) {
163 *crc
= symversion(mod
->crcs
, i
);
164 return mod
->syms
[i
].value
;
168 for (i
= 0; i
< mod
->num_gpl_syms
; i
++) {
169 if (strcmp(mod
->gpl_syms
[i
].name
, name
) == 0) {
170 *crc
= symversion(mod
->gpl_crcs
, i
);
171 return mod
->gpl_syms
[i
].value
;
176 DEBUGP("Failed to find symbol %s\n", name
);
180 /* Find a symbol in this elf symbol table */
181 static unsigned long find_local_symbol(Elf_Shdr
*sechdrs
,
182 unsigned int symindex
,
187 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
189 /* Search (defined) internal symbols first. */
190 for (i
= 1; i
< sechdrs
[symindex
].sh_size
/sizeof(*sym
); i
++) {
191 if (sym
[i
].st_shndx
!= SHN_UNDEF
192 && strcmp(name
, strtab
+ sym
[i
].st_name
) == 0)
193 return sym
[i
].st_value
;
198 /* Search for module by name: must hold module_mutex. */
199 static struct module
*find_module(const char *name
)
203 list_for_each_entry(mod
, &modules
, list
) {
204 if (strcmp(mod
->name
, name
) == 0)
211 /* Number of blocks used and allocated. */
212 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
213 /* Size of each block. -ve means used. */
214 static int *pcpu_size
;
216 static int split_block(unsigned int i
, unsigned short size
)
218 /* Reallocation required? */
219 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
220 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated
*2,
225 memcpy(new, pcpu_size
, sizeof(new[0])*pcpu_num_allocated
);
226 pcpu_num_allocated
*= 2;
231 /* Insert a new subblock */
232 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
233 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
236 pcpu_size
[i
+1] -= size
;
241 static inline unsigned int block_size(int val
)
248 /* Created by linker magic */
249 extern char __per_cpu_start
[], __per_cpu_end
[];
251 static void *percpu_modalloc(unsigned long size
, unsigned long align
)
257 BUG_ON(align
> SMP_CACHE_BYTES
);
259 ptr
= __per_cpu_start
;
260 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
261 /* Extra for alignment requirement. */
262 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
263 BUG_ON(i
== 0 && extra
!= 0);
265 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
268 /* Transfer extra to previous block. */
269 if (pcpu_size
[i
-1] < 0)
270 pcpu_size
[i
-1] -= extra
;
272 pcpu_size
[i
-1] += extra
;
273 pcpu_size
[i
] -= extra
;
276 /* Split block if warranted */
277 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
278 if (!split_block(i
, size
))
282 pcpu_size
[i
] = -pcpu_size
[i
];
286 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
291 static void percpu_modfree(void *freeme
)
294 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
296 /* First entry is core kernel percpu data. */
297 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
299 pcpu_size
[i
] = -pcpu_size
[i
];
306 /* Merge with previous? */
307 if (pcpu_size
[i
-1] >= 0) {
308 pcpu_size
[i
-1] += pcpu_size
[i
];
310 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
311 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
314 /* Merge with next? */
315 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
316 pcpu_size
[i
] += pcpu_size
[i
+1];
318 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
319 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
323 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
325 const char *secstrings
)
327 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
330 static int percpu_modinit(void)
333 pcpu_num_allocated
= 2;
334 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
336 /* Static in-kernel percpu data (used). */
337 pcpu_size
[0] = -ALIGN(__per_cpu_end
-__per_cpu_start
, SMP_CACHE_BYTES
);
339 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
340 if (pcpu_size
[1] < 0) {
341 printk(KERN_ERR
"No per-cpu room for modules.\n");
347 __initcall(percpu_modinit
);
348 #else /* ... !CONFIG_SMP */
349 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
)
353 static inline void percpu_modfree(void *pcpuptr
)
357 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
359 const char *secstrings
)
363 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
366 /* pcpusec should be 0, and size of that section should be 0. */
369 #endif /* CONFIG_SMP */
371 static int add_attribute(struct module
*mod
, struct kernel_param
*kp
)
373 struct module_attribute
*a
;
376 a
= &mod
->mkobj
->attr
[mod
->mkobj
->num_attributes
];
377 a
->attr
.name
= (char *)kp
->name
;
379 a
->attr
.mode
= kp
->perm
;
381 retval
= sysfs_create_file(&mod
->mkobj
->kobj
, &a
->attr
);
383 mod
->mkobj
->num_attributes
++;
387 #ifdef CONFIG_MODULE_UNLOAD
388 /* Init the unload section of the module. */
389 static void module_unload_init(struct module
*mod
)
393 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
394 for (i
= 0; i
< NR_CPUS
; i
++)
395 local_set(&mod
->ref
[i
].count
, 0);
396 /* Hold reference count during initialization. */
397 local_set(&mod
->ref
[smp_processor_id()].count
, 1);
398 /* Backwards compatibility macros put refcount during init. */
399 mod
->waiter
= current
;
402 /* modules using other modules */
405 struct list_head list
;
406 struct module
*module_which_uses
;
409 /* Does a already use b? */
410 static int already_uses(struct module
*a
, struct module
*b
)
412 struct module_use
*use
;
414 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
415 if (use
->module_which_uses
== a
) {
416 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
420 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
424 /* Module a uses b */
425 static int use_module(struct module
*a
, struct module
*b
)
427 struct module_use
*use
;
428 if (b
== NULL
|| already_uses(a
, b
)) return 1;
430 if (!strong_try_module_get(b
))
433 DEBUGP("Allocating new usage for %s.\n", a
->name
);
434 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
436 printk("%s: out of memory loading\n", a
->name
);
441 use
->module_which_uses
= a
;
442 list_add(&use
->list
, &b
->modules_which_use_me
);
446 /* Clear the unload stuff of the module. */
447 static void module_unload_free(struct module
*mod
)
451 list_for_each_entry(i
, &modules
, list
) {
452 struct module_use
*use
;
454 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
455 if (use
->module_which_uses
== mod
) {
456 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
458 list_del(&use
->list
);
460 /* There can be at most one match. */
467 #ifdef CONFIG_MODULE_FORCE_UNLOAD
468 static inline int try_force(unsigned int flags
)
470 int ret
= (flags
& O_TRUNC
);
472 tainted
|= TAINT_FORCED_MODULE
;
476 static inline int try_force(unsigned int flags
)
480 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
489 /* Whole machine is stopped with interrupts off when this runs. */
490 static inline int __try_stop_module(void *_sref
)
492 struct stopref
*sref
= _sref
;
494 /* If it's not unused, quit unless we are told to block. */
495 if ((sref
->flags
& O_NONBLOCK
) && module_refcount(sref
->mod
) != 0) {
496 if (!(*sref
->forced
= try_force(sref
->flags
)))
500 /* Mark it as dying. */
501 sref
->mod
->state
= MODULE_STATE_GOING
;
505 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
507 struct stopref sref
= { mod
, flags
, forced
};
509 return stop_machine_run(__try_stop_module
, &sref
, NR_CPUS
);
512 unsigned int module_refcount(struct module
*mod
)
514 unsigned int i
, total
= 0;
516 for (i
= 0; i
< NR_CPUS
; i
++)
517 total
+= local_read(&mod
->ref
[i
].count
);
520 EXPORT_SYMBOL(module_refcount
);
522 /* This exists whether we can unload or not */
523 static void free_module(struct module
*mod
);
525 static void wait_for_zero_refcount(struct module
*mod
)
527 /* Since we might sleep for some time, drop the semaphore first */
530 DEBUGP("Looking at refcount...\n");
531 set_current_state(TASK_UNINTERRUPTIBLE
);
532 if (module_refcount(mod
) == 0)
536 current
->state
= TASK_RUNNING
;
541 sys_delete_module(const char __user
*name_user
, unsigned int flags
)
544 char name
[MODULE_NAME_LEN
];
547 if (!capable(CAP_SYS_MODULE
))
550 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
552 name
[MODULE_NAME_LEN
-1] = '\0';
554 if (down_interruptible(&module_mutex
) != 0)
557 mod
= find_module(name
);
563 if (!list_empty(&mod
->modules_which_use_me
)) {
564 /* Other modules depend on us: get rid of them first. */
569 /* Doing init or already dying? */
570 if (mod
->state
!= MODULE_STATE_LIVE
) {
571 /* FIXME: if (force), slam module count and wake up
573 DEBUGP("%s already dying\n", mod
->name
);
578 /* If it has an init func, it must have an exit func to unload */
579 if ((mod
->init
!= NULL
&& mod
->exit
== NULL
)
581 forced
= try_force(flags
);
583 /* This module can't be removed */
589 /* Set this up before setting mod->state */
590 mod
->waiter
= current
;
592 /* Stop the machine so refcounts can't move and disable module. */
593 ret
= try_stop_module(mod
, flags
, &forced
);
595 /* Never wait if forced. */
596 if (!forced
&& module_refcount(mod
) != 0)
597 wait_for_zero_refcount(mod
);
599 /* Final destruction now noone is using it. */
600 if (mod
->exit
!= NULL
) {
612 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
614 struct module_use
*use
;
615 int printed_something
= 0;
617 seq_printf(m
, " %u ", module_refcount(mod
));
619 /* Always include a trailing , so userspace can differentiate
620 between this and the old multi-field proc format. */
621 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
622 printed_something
= 1;
623 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
627 printed_something
= 1;
628 seq_printf(m
, "[unsafe],");
631 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
632 printed_something
= 1;
633 seq_printf(m
, "[permanent],");
636 if (!printed_something
)
640 void __symbol_put(const char *symbol
)
642 struct module
*owner
;
644 const unsigned long *crc
;
646 spin_lock_irqsave(&modlist_lock
, flags
);
647 if (!__find_symbol(symbol
, &owner
, &crc
, 1))
650 spin_unlock_irqrestore(&modlist_lock
, flags
);
652 EXPORT_SYMBOL(__symbol_put
);
654 void symbol_put_addr(void *addr
)
658 spin_lock_irqsave(&modlist_lock
, flags
);
659 if (!kernel_text_address((unsigned long)addr
))
662 module_put(module_text_address((unsigned long)addr
));
663 spin_unlock_irqrestore(&modlist_lock
, flags
);
665 EXPORT_SYMBOL_GPL(symbol_put_addr
);
667 static int refcnt_get_fn(char *buffer
, struct kernel_param
*kp
)
669 struct module
*mod
= container_of(kp
, struct module
, refcnt_param
);
671 /* sysfs holds one reference. */
672 return sprintf(buffer
, "%u", module_refcount(mod
)-1);
675 static inline int sysfs_unload_setup(struct module
*mod
)
677 mod
->refcnt_param
.name
= "refcnt";
678 mod
->refcnt_param
.perm
= 0444;
679 mod
->refcnt_param
.get
= refcnt_get_fn
;
681 return add_attribute(mod
, &mod
->refcnt_param
);
684 #else /* !CONFIG_MODULE_UNLOAD */
685 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
687 /* We don't know the usage count, or what modules are using. */
688 seq_printf(m
, " - -");
691 static inline void module_unload_free(struct module
*mod
)
695 static inline int use_module(struct module
*a
, struct module
*b
)
697 return strong_try_module_get(b
);
700 static inline void module_unload_init(struct module
*mod
)
705 sys_delete_module(const char __user
*name_user
, unsigned int flags
)
710 static inline int sysfs_unload_setup(struct module
*mod
)
714 #endif /* CONFIG_MODULE_UNLOAD */
716 #ifdef CONFIG_OBSOLETE_MODPARM
717 /* Bounds checking done below */
718 static int obsparm_copy_string(const char *val
, struct kernel_param
*kp
)
720 strcpy(kp
->arg
, val
);
724 int set_obsolete(const char *val
, struct kernel_param
*kp
)
726 unsigned int min
, max
;
727 unsigned int size
, maxsize
;
731 struct obsolete_modparm
*obsparm
= kp
->arg
;
734 printk(KERN_ERR
"Parameter %s needs an argument\n", kp
->name
);
738 /* type is: [min[-max]]{b,h,i,l,s} */
740 min
= simple_strtol(p
, &endp
, 10);
741 if (endp
== obsparm
->type
)
743 else if (*endp
== '-') {
745 max
= simple_strtol(p
, &endp
, 10);
750 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
751 1, param_set_byte
, &dummy
);
753 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
754 sizeof(short), param_set_short
, &dummy
);
756 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
757 sizeof(int), param_set_int
, &dummy
);
759 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
760 sizeof(long), param_set_long
, &dummy
);
762 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
763 sizeof(char *), param_set_charp
, &dummy
);
766 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
767 and the decl is "char xxx[5][50];" */
769 maxsize
= simple_strtol(p
, &endp
, 10);
770 /* We check lengths here (yes, this is a hack). */
772 while (p
[size
= strcspn(p
, ",")]) {
779 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
780 maxsize
, obsparm_copy_string
, &dummy
);
782 printk(KERN_ERR
"Unknown obsolete parameter type %s\n", obsparm
->type
);
786 "Parameter %s doesn't fit in %u chars.\n", kp
->name
, maxsize
);
790 static int obsolete_params(const char *name
,
792 struct obsolete_modparm obsparm
[],
795 unsigned int symindex
,
798 struct kernel_param
*kp
;
802 kp
= kmalloc(sizeof(kp
[0]) * num
, GFP_KERNEL
);
806 for (i
= 0; i
< num
; i
++) {
807 char sym_name
[128 + sizeof(MODULE_SYMBOL_PREFIX
)];
809 snprintf(sym_name
, sizeof(sym_name
), "%s%s",
810 MODULE_SYMBOL_PREFIX
, obsparm
[i
].name
);
812 kp
[i
].name
= obsparm
[i
].name
;
814 kp
[i
].set
= set_obsolete
;
817 = (void *)find_local_symbol(sechdrs
, symindex
, strtab
,
819 if (!obsparm
[i
].addr
) {
820 printk("%s: falsely claims to have parameter %s\n",
821 name
, obsparm
[i
].name
);
825 kp
[i
].arg
= &obsparm
[i
];
828 ret
= parse_args(name
, args
, kp
, num
, NULL
);
834 static int obsolete_params(const char *name
,
836 struct obsolete_modparm obsparm
[],
839 unsigned int symindex
,
843 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
847 #endif /* CONFIG_OBSOLETE_MODPARM */
849 static const char vermagic
[] = VERMAGIC_STRING
;
851 #ifdef CONFIG_MODVERSIONS
852 static int check_version(Elf_Shdr
*sechdrs
,
853 unsigned int versindex
,
856 const unsigned long *crc
)
858 unsigned int i
, num_versions
;
859 struct modversion_info
*versions
;
861 /* Exporting module didn't supply crcs? OK, we're already tainted. */
865 versions
= (void *) sechdrs
[versindex
].sh_addr
;
866 num_versions
= sechdrs
[versindex
].sh_size
867 / sizeof(struct modversion_info
);
869 for (i
= 0; i
< num_versions
; i
++) {
870 if (strcmp(versions
[i
].name
, symname
) != 0)
873 if (versions
[i
].crc
== *crc
)
875 printk("%s: disagrees about version of symbol %s\n",
877 DEBUGP("Found checksum %lX vs module %lX\n",
878 *crc
, versions
[i
].crc
);
881 /* Not in module's version table. OK, but that taints the kernel. */
882 if (!(tainted
& TAINT_FORCED_MODULE
)) {
883 printk("%s: no version for \"%s\" found: kernel tainted.\n",
885 tainted
|= TAINT_FORCED_MODULE
;
890 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
891 unsigned int versindex
,
894 const unsigned long *crc
;
895 struct module
*owner
;
897 if (!__find_symbol("struct_module", &owner
, &crc
, 1))
899 return check_version(sechdrs
, versindex
, "struct_module", mod
,
903 /* First part is kernel version, which we ignore. */
904 static inline int same_magic(const char *amagic
, const char *bmagic
)
906 amagic
+= strcspn(amagic
, " ");
907 bmagic
+= strcspn(bmagic
, " ");
908 return strcmp(amagic
, bmagic
) == 0;
911 static inline int check_version(Elf_Shdr
*sechdrs
,
912 unsigned int versindex
,
915 const unsigned long *crc
)
920 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
921 unsigned int versindex
,
927 static inline int same_magic(const char *amagic
, const char *bmagic
)
929 return strcmp(amagic
, bmagic
) == 0;
931 #endif /* CONFIG_MODVERSIONS */
933 /* Resolve a symbol for this module. I.e. if we find one, record usage.
934 Must be holding module_mutex. */
935 static unsigned long resolve_symbol(Elf_Shdr
*sechdrs
,
936 unsigned int versindex
,
940 struct module
*owner
;
942 const unsigned long *crc
;
944 spin_lock_irq(&modlist_lock
);
945 ret
= __find_symbol(name
, &owner
, &crc
, mod
->license_gplok
);
947 /* use_module can fail due to OOM, or module unloading */
948 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
949 !use_module(mod
, owner
))
952 spin_unlock_irq(&modlist_lock
);
958 * /sys/module/foo/sections stuff
959 * J. Corbet <corbet@lwn.net>
961 #ifdef CONFIG_KALLSYMS
962 static void module_sect_attrs_release(struct kobject
*kobj
)
964 kfree(container_of(kobj
, struct module_sections
, kobj
));
967 static ssize_t
module_sect_show(struct kobject
*kobj
, struct attribute
*attr
,
970 struct module_sect_attr
*sattr
=
971 container_of(attr
, struct module_sect_attr
, attr
);
972 return sprintf(buf
, "0x%lx\n", sattr
->address
);
975 static struct sysfs_ops module_sect_ops
= {
976 .show
= module_sect_show
,
979 static struct kobj_type module_sect_ktype
= {
980 .sysfs_ops
= &module_sect_ops
,
981 .release
= module_sect_attrs_release
,
984 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
985 char *secstrings
, Elf_Shdr
*sechdrs
)
987 unsigned int nloaded
= 0, i
;
988 struct module_sect_attr
*sattr
;
993 /* Count loaded sections and allocate structures */
994 for (i
= 0; i
< nsect
; i
++)
995 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
997 mod
->sect_attrs
= kmalloc(sizeof(struct module_sections
) +
998 nloaded
*sizeof(mod
->sect_attrs
->attrs
[0]), GFP_KERNEL
);
999 if (! mod
->sect_attrs
)
1002 /* sections entry setup */
1003 memset(mod
->sect_attrs
, 0, sizeof(struct module_sections
));
1004 if (kobject_set_name(&mod
->sect_attrs
->kobj
, "sections"))
1006 mod
->sect_attrs
->kobj
.parent
= &mod
->mkobj
->kobj
;
1007 mod
->sect_attrs
->kobj
.ktype
= &module_sect_ktype
;
1008 if (kobject_register(&mod
->sect_attrs
->kobj
))
1011 /* And the section attributes. */
1012 sattr
= &mod
->sect_attrs
->attrs
[0];
1013 for (i
= 0; i
< nsect
; i
++) {
1014 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1016 sattr
->address
= sechdrs
[i
].sh_addr
;
1017 strlcpy(sattr
->name
, secstrings
+ sechdrs
[i
].sh_name
,
1018 MODULE_SECT_NAME_LEN
);
1019 sattr
->attr
.name
= sattr
->name
;
1020 sattr
->attr
.owner
= mod
;
1021 sattr
->attr
.mode
= S_IRUGO
;
1022 (void) sysfs_create_file(&mod
->sect_attrs
->kobj
, &sattr
->attr
);
1027 kfree(mod
->sect_attrs
);
1028 mod
->sect_attrs
= NULL
;
1031 static void remove_sect_attrs(struct module
*mod
)
1033 if (mod
->sect_attrs
) {
1034 kobject_unregister(&mod
->sect_attrs
->kobj
);
1035 mod
->sect_attrs
= NULL
;
1041 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1042 char *sectstrings
, Elf_Shdr
*sechdrs
)
1046 static inline void remove_sect_attrs(struct module
*mod
)
1049 #endif /* CONFIG_KALLSYMS */
1054 #define to_module_attr(n) container_of(n, struct module_attribute, attr);
1056 static ssize_t
module_attr_show(struct kobject
*kobj
,
1057 struct attribute
*attr
,
1061 struct module_attribute
*attribute
= to_module_attr(attr
);
1063 if (!attribute
->param
->get
)
1066 count
= attribute
->param
->get(buf
, attribute
->param
);
1074 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
1075 static ssize_t
module_attr_store(struct kobject
*kobj
,
1076 struct attribute
*attr
,
1077 const char *buf
, size_t len
)
1080 struct module_attribute
*attribute
= to_module_attr(attr
);
1082 if (!attribute
->param
->set
)
1085 err
= attribute
->param
->set(buf
, attribute
->param
);
1091 static struct sysfs_ops module_sysfs_ops
= {
1092 .show
= module_attr_show
,
1093 .store
= module_attr_store
,
1096 static void module_kobj_release(struct kobject
*kobj
)
1098 kfree(container_of(kobj
, struct module_kobject
, kobj
));
1101 static struct kobj_type module_ktype
= {
1102 .sysfs_ops
= &module_sysfs_ops
,
1103 .release
= &module_kobj_release
,
1105 static decl_subsys(module
, &module_ktype
, NULL
);
1107 static int mod_sysfs_setup(struct module
*mod
,
1108 struct kernel_param
*kparam
,
1109 unsigned int num_params
)
1114 /* We overallocate: not every param is in sysfs, and maybe no refcnt */
1115 mod
->mkobj
= kmalloc(sizeof(*mod
->mkobj
)
1116 + sizeof(mod
->mkobj
->attr
[0]) * (num_params
+1),
1121 memset(&mod
->mkobj
->kobj
, 0, sizeof(mod
->mkobj
->kobj
));
1122 err
= kobject_set_name(&mod
->mkobj
->kobj
, mod
->name
);
1125 kobj_set_kset_s(mod
->mkobj
, module_subsys
);
1126 err
= kobject_register(&mod
->mkobj
->kobj
);
1130 mod
->mkobj
->num_attributes
= 0;
1132 for (i
= 0; i
< num_params
; i
++) {
1133 if (kparam
[i
].perm
) {
1134 err
= add_attribute(mod
, &kparam
[i
]);
1139 err
= sysfs_unload_setup(mod
);
1145 for (i
= 0; i
< mod
->mkobj
->num_attributes
; i
++)
1146 sysfs_remove_file(&mod
->mkobj
->kobj
,&mod
->mkobj
->attr
[i
].attr
);
1147 /* Calls module_kobj_release */
1148 kobject_unregister(&mod
->mkobj
->kobj
);
1155 static void mod_kobject_remove(struct module
*mod
)
1158 for (i
= 0; i
< mod
->mkobj
->num_attributes
; i
++)
1159 sysfs_remove_file(&mod
->mkobj
->kobj
,&mod
->mkobj
->attr
[i
].attr
);
1160 /* Calls module_kobj_release */
1161 kobject_unregister(&mod
->mkobj
->kobj
);
1164 /* Free a module, remove from lists, etc (must hold module mutex). */
1165 static void free_module(struct module
*mod
)
1167 /* Delete from various lists */
1168 spin_lock_irq(&modlist_lock
);
1169 list_del(&mod
->list
);
1170 spin_unlock_irq(&modlist_lock
);
1172 remove_sect_attrs(mod
);
1173 mod_kobject_remove(mod
);
1175 /* Arch-specific cleanup. */
1176 module_arch_cleanup(mod
);
1178 /* Module unload stuff */
1179 module_unload_free(mod
);
1181 /* This may be NULL, but that's OK */
1182 module_free(mod
, mod
->module_init
);
1185 percpu_modfree(mod
->percpu
);
1187 /* Finally, free the core (containing the module structure) */
1188 module_free(mod
, mod
->module_core
);
1191 void *__symbol_get(const char *symbol
)
1193 struct module
*owner
;
1194 unsigned long value
, flags
;
1195 const unsigned long *crc
;
1197 spin_lock_irqsave(&modlist_lock
, flags
);
1198 value
= __find_symbol(symbol
, &owner
, &crc
, 1);
1199 if (value
&& !strong_try_module_get(owner
))
1201 spin_unlock_irqrestore(&modlist_lock
, flags
);
1203 return (void *)value
;
1205 EXPORT_SYMBOL_GPL(__symbol_get
);
1207 /* Change all symbols so that sh_value encodes the pointer directly. */
1208 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1209 unsigned int symindex
,
1211 unsigned int versindex
,
1212 unsigned int pcpuindex
,
1215 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1216 unsigned long secbase
;
1217 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1220 for (i
= 1; i
< n
; i
++) {
1221 switch (sym
[i
].st_shndx
) {
1223 /* We compiled with -fno-common. These are not
1224 supposed to happen. */
1225 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1226 printk("%s: please compile with -fno-common\n",
1232 /* Don't need to do anything */
1233 DEBUGP("Absolute symbol: 0x%08lx\n",
1234 (long)sym
[i
].st_value
);
1239 = resolve_symbol(sechdrs
, versindex
,
1240 strtab
+ sym
[i
].st_name
, mod
);
1242 /* Ok if resolved. */
1243 if (sym
[i
].st_value
!= 0)
1246 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1249 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1250 mod
->name
, strtab
+ sym
[i
].st_name
);
1255 /* Divert to percpu allocation if a percpu var. */
1256 if (sym
[i
].st_shndx
== pcpuindex
)
1257 secbase
= (unsigned long)mod
->percpu
;
1259 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1260 sym
[i
].st_value
+= secbase
;
1268 /* Update size with this section: return offset. */
1269 static long get_offset(unsigned long *size
, Elf_Shdr
*sechdr
)
1273 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1274 *size
= ret
+ sechdr
->sh_size
;
1278 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1279 might -- code, read-only data, read-write data, small data. Tally
1280 sizes, and place the offsets into sh_entsize fields: high bit means it
1282 static void layout_sections(struct module
*mod
,
1283 const Elf_Ehdr
*hdr
,
1285 const char *secstrings
)
1287 static unsigned long const masks
[][2] = {
1288 /* NOTE: all executable code must be the first section
1289 * in this array; otherwise modify the text_size
1290 * finder in the two loops below */
1291 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1292 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1293 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1294 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1298 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1299 sechdrs
[i
].sh_entsize
= ~0UL;
1301 DEBUGP("Core section allocation order:\n");
1302 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1303 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1304 Elf_Shdr
*s
= &sechdrs
[i
];
1306 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1307 || (s
->sh_flags
& masks
[m
][1])
1308 || s
->sh_entsize
!= ~0UL
1309 || strncmp(secstrings
+ s
->sh_name
,
1312 s
->sh_entsize
= get_offset(&mod
->core_size
, s
);
1313 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1316 mod
->core_text_size
= mod
->core_size
;
1319 DEBUGP("Init section allocation order:\n");
1320 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1321 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1322 Elf_Shdr
*s
= &sechdrs
[i
];
1324 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1325 || (s
->sh_flags
& masks
[m
][1])
1326 || s
->sh_entsize
!= ~0UL
1327 || strncmp(secstrings
+ s
->sh_name
,
1330 s
->sh_entsize
= (get_offset(&mod
->init_size
, s
)
1331 | INIT_OFFSET_MASK
);
1332 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1335 mod
->init_text_size
= mod
->init_size
;
1339 static inline int license_is_gpl_compatible(const char *license
)
1341 return (strcmp(license
, "GPL") == 0
1342 || strcmp(license
, "GPL v2") == 0
1343 || strcmp(license
, "GPL and additional rights") == 0
1344 || strcmp(license
, "Dual BSD/GPL") == 0
1345 || strcmp(license
, "Dual MPL/GPL") == 0);
1348 static void set_license(struct module
*mod
, const char *license
)
1351 license
= "unspecified";
1353 mod
->license_gplok
= license_is_gpl_compatible(license
);
1354 if (!mod
->license_gplok
&& !(tainted
& TAINT_PROPRIETARY_MODULE
)) {
1355 printk(KERN_WARNING
"%s: module license '%s' taints kernel.\n",
1356 mod
->name
, license
);
1357 tainted
|= TAINT_PROPRIETARY_MODULE
;
1361 /* Parse tag=value strings from .modinfo section */
1362 static char *next_string(char *string
, unsigned long *secsize
)
1364 /* Skip non-zero chars */
1367 if ((*secsize
)-- <= 1)
1371 /* Skip any zero padding. */
1372 while (!string
[0]) {
1374 if ((*secsize
)-- <= 1)
1380 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1385 unsigned int taglen
= strlen(tag
);
1386 unsigned long size
= sechdrs
[info
].sh_size
;
1388 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1389 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1390 return p
+ taglen
+ 1;
1395 #ifdef CONFIG_KALLSYMS
1396 int is_exported(const char *name
, const struct module
*mod
)
1401 for (i
= 0; __start___ksymtab
+i
< __stop___ksymtab
; i
++)
1402 if (strcmp(__start___ksymtab
[i
].name
, name
) == 0)
1406 for (i
= 0; i
< mod
->num_syms
; i
++)
1407 if (strcmp(mod
->syms
[i
].name
, name
) == 0)
1413 static char elf_type(const Elf_Sym
*sym
,
1415 const char *secstrings
,
1418 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1419 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1424 if (sym
->st_shndx
== SHN_UNDEF
)
1426 if (sym
->st_shndx
== SHN_ABS
)
1428 if (sym
->st_shndx
>= SHN_LORESERVE
)
1430 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1432 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1433 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1434 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1436 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1441 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1442 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1447 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1448 ".debug", strlen(".debug")) == 0)
1453 static void add_kallsyms(struct module
*mod
,
1455 unsigned int symindex
,
1456 unsigned int strindex
,
1457 const char *secstrings
)
1461 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1462 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1463 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1465 /* Set types up while we still have access to sections. */
1466 for (i
= 0; i
< mod
->num_symtab
; i
++)
1467 mod
->symtab
[i
].st_info
1468 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1471 static inline void add_kallsyms(struct module
*mod
,
1473 unsigned int symindex
,
1474 unsigned int strindex
,
1475 const char *secstrings
)
1478 #endif /* CONFIG_KALLSYMS */
1480 /* Allocate and load the module: note that size of section 0 is always
1481 zero, and we rely on this for optional sections. */
1482 static struct module
*load_module(void __user
*umod
,
1484 const char __user
*uargs
)
1488 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1489 unsigned int i
, symindex
= 0, strindex
= 0, setupindex
, exindex
,
1490 exportindex
, modindex
, obsparmindex
, infoindex
, gplindex
,
1491 crcindex
, gplcrcindex
, versindex
, pcpuindex
;
1495 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1496 struct exception_table_entry
*extable
;
1498 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1500 if (len
< sizeof(*hdr
))
1501 return ERR_PTR(-ENOEXEC
);
1503 /* Suck in entire file: we'll want most of it. */
1504 /* vmalloc barfs on "unusual" numbers. Check here */
1505 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1506 return ERR_PTR(-ENOMEM
);
1507 if (copy_from_user(hdr
, umod
, len
) != 0) {
1512 /* Sanity checks against insmoding binaries or wrong arch,
1513 weird elf version */
1514 if (memcmp(hdr
->e_ident
, ELFMAG
, 4) != 0
1515 || hdr
->e_type
!= ET_REL
1516 || !elf_check_arch(hdr
)
1517 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1522 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1525 /* Convenience variables */
1526 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1527 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1528 sechdrs
[0].sh_addr
= 0;
1530 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1531 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1532 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1535 /* Mark all sections sh_addr with their address in the
1537 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1539 /* Internal symbols and strings. */
1540 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1542 strindex
= sechdrs
[i
].sh_link
;
1543 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1545 #ifndef CONFIG_MODULE_UNLOAD
1546 /* Don't load .exit sections */
1547 if (strncmp(secstrings
+sechdrs
[i
].sh_name
, ".exit", 5) == 0)
1548 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1552 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1553 ".gnu.linkonce.this_module");
1555 printk(KERN_WARNING
"No module found in object\n");
1559 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1561 if (symindex
== 0) {
1562 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1568 /* Optional sections */
1569 exportindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab");
1570 gplindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl");
1571 crcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab");
1572 gplcrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
1573 setupindex
= find_sec(hdr
, sechdrs
, secstrings
, "__param");
1574 exindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ex_table");
1575 obsparmindex
= find_sec(hdr
, sechdrs
, secstrings
, "__obsparm");
1576 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1577 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1578 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1580 /* Don't keep modinfo section */
1581 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1582 #ifdef CONFIG_KALLSYMS
1583 /* Keep symbol and string tables for decoding later. */
1584 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1585 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1588 /* Check module struct version now, before we try to use module. */
1589 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1594 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1595 /* This is allowed: modprobe --force will invalidate it. */
1597 tainted
|= TAINT_FORCED_MODULE
;
1598 printk(KERN_WARNING
"%s: no version magic, tainting kernel.\n",
1600 } else if (!same_magic(modmagic
, vermagic
)) {
1601 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
1602 mod
->name
, modmagic
, vermagic
);
1607 /* Now copy in args */
1608 arglen
= strlen_user(uargs
);
1613 args
= kmalloc(arglen
, GFP_KERNEL
);
1618 if (copy_from_user(args
, uargs
, arglen
) != 0) {
1623 if (find_module(mod
->name
)) {
1628 mod
->state
= MODULE_STATE_COMING
;
1630 /* Allow arches to frob section contents and sizes. */
1631 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
1636 /* We have a special allocation for this section. */
1637 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
1638 sechdrs
[pcpuindex
].sh_addralign
);
1643 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1644 mod
->percpu
= percpu
;
1647 /* Determine total sizes, and put offsets in sh_entsize. For now
1648 this is done generically; there doesn't appear to be any
1649 special cases for the architectures. */
1650 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
1652 /* Do the allocs. */
1653 ptr
= module_alloc(mod
->core_size
);
1658 memset(ptr
, 0, mod
->core_size
);
1659 mod
->module_core
= ptr
;
1661 ptr
= module_alloc(mod
->init_size
);
1662 if (!ptr
&& mod
->init_size
) {
1666 memset(ptr
, 0, mod
->init_size
);
1667 mod
->module_init
= ptr
;
1669 /* Transfer each section which specifies SHF_ALLOC */
1670 DEBUGP("final section addresses:\n");
1671 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
1674 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1677 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
1678 dest
= mod
->module_init
1679 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
1681 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
1683 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
1684 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
1685 sechdrs
[i
].sh_size
);
1686 /* Update sh_addr to point to copy in image. */
1687 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
1688 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
1690 /* Module has been moved. */
1691 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1693 /* Now we've moved module, initialize linked lists, etc. */
1694 module_unload_init(mod
);
1696 /* Set up license info based on the info section */
1697 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
1699 /* Fix up syms, so that st_value is a pointer to location. */
1700 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
1705 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1706 mod
->num_syms
= sechdrs
[exportindex
].sh_size
/ sizeof(*mod
->syms
);
1707 mod
->syms
= (void *)sechdrs
[exportindex
].sh_addr
;
1709 mod
->crcs
= (void *)sechdrs
[crcindex
].sh_addr
;
1710 mod
->num_gpl_syms
= sechdrs
[gplindex
].sh_size
/ sizeof(*mod
->gpl_syms
);
1711 mod
->gpl_syms
= (void *)sechdrs
[gplindex
].sh_addr
;
1713 mod
->gpl_crcs
= (void *)sechdrs
[gplcrcindex
].sh_addr
;
1715 #ifdef CONFIG_MODVERSIONS
1716 if ((mod
->num_syms
&& !crcindex
) ||
1717 (mod
->num_gpl_syms
&& !gplcrcindex
)) {
1718 printk(KERN_WARNING
"%s: No versions for exported symbols."
1719 " Tainting kernel.\n", mod
->name
);
1720 tainted
|= TAINT_FORCED_MODULE
;
1724 /* Now do relocations. */
1725 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1726 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
1727 unsigned int info
= sechdrs
[i
].sh_info
;
1729 /* Not a valid relocation section? */
1730 if (info
>= hdr
->e_shnum
)
1733 /* Don't bother with non-allocated sections */
1734 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
1737 if (sechdrs
[i
].sh_type
== SHT_REL
)
1738 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
1739 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
1740 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
1746 /* Set up and sort exception table */
1747 mod
->num_exentries
= sechdrs
[exindex
].sh_size
/ sizeof(*mod
->extable
);
1748 mod
->extable
= extable
= (void *)sechdrs
[exindex
].sh_addr
;
1749 sort_extable(extable
, extable
+ mod
->num_exentries
);
1751 /* Finally, copy percpu area over. */
1752 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
1753 sechdrs
[pcpuindex
].sh_size
);
1755 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
1757 err
= module_finalize(hdr
, sechdrs
, mod
);
1763 err
= obsolete_params(mod
->name
, mod
->args
,
1764 (struct obsolete_modparm
*)
1765 sechdrs
[obsparmindex
].sh_addr
,
1766 sechdrs
[obsparmindex
].sh_size
1767 / sizeof(struct obsolete_modparm
),
1769 (char *)sechdrs
[strindex
].sh_addr
);
1771 printk(KERN_WARNING
"%s: Ignoring new-style "
1772 "parameters in presence of obsolete ones\n",
1775 /* Size of section 0 is 0, so this works well if no params */
1776 err
= parse_args(mod
->name
, mod
->args
,
1777 (struct kernel_param
*)
1778 sechdrs
[setupindex
].sh_addr
,
1779 sechdrs
[setupindex
].sh_size
1780 / sizeof(struct kernel_param
),
1783 err
= mod_sysfs_setup(mod
,
1784 (struct kernel_param
*)
1785 sechdrs
[setupindex
].sh_addr
,
1786 sechdrs
[setupindex
].sh_size
1787 / sizeof(struct kernel_param
));
1790 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
1792 /* Get rid of temporary copy */
1799 module_arch_cleanup(mod
);
1801 module_unload_free(mod
);
1802 module_free(mod
, mod
->module_init
);
1804 module_free(mod
, mod
->module_core
);
1807 percpu_modfree(percpu
);
1812 if (err
< 0) return ERR_PTR(err
);
1816 printk(KERN_ERR
"Module len %lu truncated\n", len
);
1821 /* This is where the real work happens */
1823 sys_init_module(void __user
*umod
,
1825 const char __user
*uargs
)
1830 /* Must have permission */
1831 if (!capable(CAP_SYS_MODULE
))
1834 /* Only one module load at a time, please */
1835 if (down_interruptible(&module_mutex
) != 0)
1838 /* Do all the hard work */
1839 mod
= load_module(umod
, len
, uargs
);
1842 return PTR_ERR(mod
);
1845 /* Flush the instruction cache, since we've played with text */
1846 if (mod
->module_init
)
1847 flush_icache_range((unsigned long)mod
->module_init
,
1848 (unsigned long)mod
->module_init
1850 flush_icache_range((unsigned long)mod
->module_core
,
1851 (unsigned long)mod
->module_core
+ mod
->core_size
);
1853 /* Now sew it into the lists. They won't access us, since
1854 strong_try_module_get() will fail. */
1855 spin_lock_irq(&modlist_lock
);
1856 list_add(&mod
->list
, &modules
);
1857 spin_unlock_irq(&modlist_lock
);
1859 /* Drop lock so they can recurse */
1862 down(¬ify_mutex
);
1863 notifier_call_chain(&module_notify_list
, MODULE_STATE_COMING
, mod
);
1866 /* Start the module */
1867 if (mod
->init
!= NULL
)
1870 /* Init routine failed: abort. Try to protect us from
1871 buggy refcounters. */
1872 mod
->state
= MODULE_STATE_GOING
;
1873 synchronize_kernel();
1875 printk(KERN_ERR
"%s: module is now stuck!\n",
1879 down(&module_mutex
);
1886 /* Now it's a first class citizen! */
1887 down(&module_mutex
);
1888 mod
->state
= MODULE_STATE_LIVE
;
1889 /* Drop initial reference. */
1891 module_free(mod
, mod
->module_init
);
1892 mod
->module_init
= NULL
;
1894 mod
->init_text_size
= 0;
1900 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
1902 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
1905 #ifdef CONFIG_KALLSYMS
1906 static const char *get_ksymbol(struct module
*mod
,
1908 unsigned long *size
,
1909 unsigned long *offset
)
1911 unsigned int i
, best
= 0;
1912 unsigned long nextval
;
1914 /* At worse, next value is at end of module */
1915 if (within(addr
, mod
->module_init
, mod
->init_size
))
1916 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
1918 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
1920 /* Scan for closest preceeding symbol, and next symbol. (ELF
1921 starts real symbols at 1). */
1922 for (i
= 1; i
< mod
->num_symtab
; i
++) {
1923 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
1926 /* We ignore unnamed symbols: they're uninformative
1927 * and inserted at a whim. */
1928 if (mod
->symtab
[i
].st_value
<= addr
1929 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
1930 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0' )
1932 if (mod
->symtab
[i
].st_value
> addr
1933 && mod
->symtab
[i
].st_value
< nextval
1934 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0')
1935 nextval
= mod
->symtab
[i
].st_value
;
1941 *size
= nextval
- mod
->symtab
[best
].st_value
;
1942 *offset
= addr
- mod
->symtab
[best
].st_value
;
1943 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
1946 /* For kallsyms to ask for address resolution. NULL means not found.
1947 We don't lock, as this is used for oops resolution and races are a
1949 const char *module_address_lookup(unsigned long addr
,
1950 unsigned long *size
,
1951 unsigned long *offset
,
1956 list_for_each_entry(mod
, &modules
, list
) {
1957 if (within(addr
, mod
->module_init
, mod
->init_size
)
1958 || within(addr
, mod
->module_core
, mod
->core_size
)) {
1959 *modname
= mod
->name
;
1960 return get_ksymbol(mod
, addr
, size
, offset
);
1966 struct module
*module_get_kallsym(unsigned int symnum
,
1967 unsigned long *value
,
1973 down(&module_mutex
);
1974 list_for_each_entry(mod
, &modules
, list
) {
1975 if (symnum
< mod
->num_symtab
) {
1976 *value
= mod
->symtab
[symnum
].st_value
;
1977 *type
= mod
->symtab
[symnum
].st_info
;
1979 mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
1984 symnum
-= mod
->num_symtab
;
1990 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
1994 for (i
= 0; i
< mod
->num_symtab
; i
++)
1995 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0)
1996 return mod
->symtab
[i
].st_value
;
2000 /* Look for this name: can be of form module:name. */
2001 unsigned long module_kallsyms_lookup_name(const char *name
)
2005 unsigned long ret
= 0;
2007 /* Don't lock: we're in enough trouble already. */
2008 if ((colon
= strchr(name
, ':')) != NULL
) {
2010 if ((mod
= find_module(name
)) != NULL
)
2011 ret
= mod_find_symname(mod
, colon
+1);
2014 list_for_each_entry(mod
, &modules
, list
)
2015 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2020 #endif /* CONFIG_KALLSYMS */
2022 /* Called by the /proc file system to return a list of modules. */
2023 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2025 struct list_head
*i
;
2028 down(&module_mutex
);
2029 list_for_each(i
, &modules
) {
2038 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2040 struct list_head
*i
= p
;
2042 if (i
->next
== &modules
)
2047 static void m_stop(struct seq_file
*m
, void *p
)
2052 static int m_show(struct seq_file
*m
, void *p
)
2054 struct module
*mod
= list_entry(p
, struct module
, list
);
2055 seq_printf(m
, "%s %lu",
2056 mod
->name
, mod
->init_size
+ mod
->core_size
);
2057 print_unload_info(m
, mod
);
2059 /* Informative for users. */
2060 seq_printf(m
, " %s",
2061 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2062 mod
->state
== MODULE_STATE_COMING
? "Loading":
2064 /* Used by oprofile and other similar tools. */
2065 seq_printf(m
, " 0x%p", mod
->module_core
);
2067 seq_printf(m
, "\n");
2071 /* Format: modulename size refcount deps address
2073 Where refcount is a number or -, and deps is a comma-separated list
2076 struct seq_operations modules_op
= {
2083 /* Given an address, look for it in the module exception tables. */
2084 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2086 unsigned long flags
;
2087 const struct exception_table_entry
*e
= NULL
;
2090 spin_lock_irqsave(&modlist_lock
, flags
);
2091 list_for_each_entry(mod
, &modules
, list
) {
2092 if (mod
->num_exentries
== 0)
2095 e
= search_extable(mod
->extable
,
2096 mod
->extable
+ mod
->num_exentries
- 1,
2101 spin_unlock_irqrestore(&modlist_lock
, flags
);
2103 /* Now, if we found one, we are running inside it now, hence
2104 we cannot unload the module, hence no refcnt needed. */
2108 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2109 struct module
*__module_text_address(unsigned long addr
)
2113 list_for_each_entry(mod
, &modules
, list
)
2114 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
2115 || within(addr
, mod
->module_core
, mod
->core_text_size
))
2120 struct module
*module_text_address(unsigned long addr
)
2123 unsigned long flags
;
2125 spin_lock_irqsave(&modlist_lock
, flags
);
2126 mod
= __module_text_address(addr
);
2127 spin_unlock_irqrestore(&modlist_lock
, flags
);
2132 /* Don't grab lock, we're oopsing. */
2133 void print_modules(void)
2137 printk("Modules linked in:");
2138 list_for_each_entry(mod
, &modules
, list
)
2139 printk(" %s", mod
->name
);
2143 #ifdef CONFIG_MODVERSIONS
2144 /* Generate the signature for struct module here, too, for modversions. */
2145 void struct_module(struct module
*mod
) { return; }
2146 EXPORT_SYMBOL(struct_module
);
2149 static int __init
modules_init(void)
2151 return subsystem_register(&module_subsys
);
2153 __initcall(modules_init
);