1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/elf.h>
27 #include <linux/seq_file.h>
28 #include <linux/syscalls.h>
29 #include <linux/fcntl.h>
30 #include <linux/rcupdate.h>
31 #include <linux/capability.h>
32 #include <linux/cpu.h>
33 #include <linux/moduleparam.h>
34 #include <linux/errno.h>
35 #include <linux/err.h>
36 #include <linux/vermagic.h>
37 #include <linux/notifier.h>
38 #include <linux/stop_machine.h>
39 #include <linux/device.h>
40 #include <linux/string.h>
41 #include <linux/sched.h>
42 #include <linux/mutex.h>
43 #include <linux/unwind.h>
44 #include <asm/uaccess.h>
45 #include <asm/semaphore.h>
46 #include <asm/cacheflush.h>
51 #define DEBUGP(fmt , a...)
54 #ifndef ARCH_SHF_SMALL
55 #define ARCH_SHF_SMALL 0
58 /* If this is set, the section belongs in the init part of the module */
59 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
61 /* Protects module list */
62 static DEFINE_SPINLOCK(modlist_lock
);
64 /* List of modules, protected by module_mutex AND modlist_lock */
65 static DEFINE_MUTEX(module_mutex
);
66 static LIST_HEAD(modules
);
68 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
70 int register_module_notifier(struct notifier_block
* nb
)
72 return blocking_notifier_chain_register(&module_notify_list
, nb
);
74 EXPORT_SYMBOL(register_module_notifier
);
76 int unregister_module_notifier(struct notifier_block
* nb
)
78 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
80 EXPORT_SYMBOL(unregister_module_notifier
);
82 /* We require a truly strong try_module_get() */
83 static inline int strong_try_module_get(struct module
*mod
)
85 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
87 return try_module_get(mod
);
90 /* A thread that wants to hold a reference to a module only while it
91 * is running can call ths to safely exit.
92 * nfsd and lockd use this.
94 void __module_put_and_exit(struct module
*mod
, long code
)
99 EXPORT_SYMBOL(__module_put_and_exit
);
101 /* Find a module section: 0 means not found. */
102 static unsigned int find_sec(Elf_Ehdr
*hdr
,
104 const char *secstrings
,
109 for (i
= 1; i
< hdr
->e_shnum
; i
++)
110 /* Alloc bit cleared means "ignore it." */
111 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
112 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
117 /* Provided by the linker */
118 extern const struct kernel_symbol __start___ksymtab
[];
119 extern const struct kernel_symbol __stop___ksymtab
[];
120 extern const struct kernel_symbol __start___ksymtab_gpl
[];
121 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
122 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
123 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
124 extern const unsigned long __start___kcrctab
[];
125 extern const unsigned long __start___kcrctab_gpl
[];
126 extern const unsigned long __start___kcrctab_gpl_future
[];
128 #ifndef CONFIG_MODVERSIONS
129 #define symversion(base, idx) NULL
131 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
134 /* lookup symbol in given range of kernel_symbols */
135 static const struct kernel_symbol
*lookup_symbol(const char *name
,
136 const struct kernel_symbol
*start
,
137 const struct kernel_symbol
*stop
)
139 const struct kernel_symbol
*ks
= start
;
140 for (; ks
< stop
; ks
++)
141 if (strcmp(ks
->name
, name
) == 0)
146 /* Find a symbol, return value, crc and module which owns it */
147 static unsigned long __find_symbol(const char *name
,
148 struct module
**owner
,
149 const unsigned long **crc
,
153 const struct kernel_symbol
*ks
;
155 /* Core kernel first. */
157 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
159 *crc
= symversion(__start___kcrctab
, (ks
- __start___ksymtab
));
163 ks
= lookup_symbol(name
, __start___ksymtab_gpl
,
164 __stop___ksymtab_gpl
);
166 *crc
= symversion(__start___kcrctab_gpl
,
167 (ks
- __start___ksymtab_gpl
));
171 ks
= lookup_symbol(name
, __start___ksymtab_gpl_future
,
172 __stop___ksymtab_gpl_future
);
175 printk(KERN_WARNING
"Symbol %s is being used "
176 "by a non-GPL module, which will not "
177 "be allowed in the future\n", name
);
178 printk(KERN_WARNING
"Please see the file "
179 "Documentation/feature-removal-schedule.txt "
180 "in the kernel source tree for more "
183 *crc
= symversion(__start___kcrctab_gpl_future
,
184 (ks
- __start___ksymtab_gpl_future
));
188 /* Now try modules. */
189 list_for_each_entry(mod
, &modules
, list
) {
191 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
193 *crc
= symversion(mod
->crcs
, (ks
- mod
->syms
));
198 ks
= lookup_symbol(name
, mod
->gpl_syms
,
199 mod
->gpl_syms
+ mod
->num_gpl_syms
);
201 *crc
= symversion(mod
->gpl_crcs
,
202 (ks
- mod
->gpl_syms
));
206 ks
= lookup_symbol(name
, mod
->gpl_future_syms
,
207 (mod
->gpl_future_syms
+
208 mod
->num_gpl_future_syms
));
211 printk(KERN_WARNING
"Symbol %s is being used "
212 "by a non-GPL module, which will not "
213 "be allowed in the future\n", name
);
214 printk(KERN_WARNING
"Please see the file "
215 "Documentation/feature-removal-schedule.txt "
216 "in the kernel source tree for more "
219 *crc
= symversion(mod
->gpl_future_crcs
,
220 (ks
- mod
->gpl_future_syms
));
224 DEBUGP("Failed to find symbol %s\n", name
);
228 /* Search for module by name: must hold module_mutex. */
229 static struct module
*find_module(const char *name
)
233 list_for_each_entry(mod
, &modules
, list
) {
234 if (strcmp(mod
->name
, name
) == 0)
241 /* Number of blocks used and allocated. */
242 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
243 /* Size of each block. -ve means used. */
244 static int *pcpu_size
;
246 static int split_block(unsigned int i
, unsigned short size
)
248 /* Reallocation required? */
249 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
250 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated
*2,
255 memcpy(new, pcpu_size
, sizeof(new[0])*pcpu_num_allocated
);
256 pcpu_num_allocated
*= 2;
261 /* Insert a new subblock */
262 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
263 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
266 pcpu_size
[i
+1] -= size
;
271 static inline unsigned int block_size(int val
)
278 /* Created by linker magic */
279 extern char __per_cpu_start
[], __per_cpu_end
[];
281 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
288 if (align
> SMP_CACHE_BYTES
) {
289 printk(KERN_WARNING
"%s: per-cpu alignment %li > %i\n",
290 name
, align
, SMP_CACHE_BYTES
);
291 align
= SMP_CACHE_BYTES
;
294 ptr
= __per_cpu_start
;
295 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
296 /* Extra for alignment requirement. */
297 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
298 BUG_ON(i
== 0 && extra
!= 0);
300 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
303 /* Transfer extra to previous block. */
304 if (pcpu_size
[i
-1] < 0)
305 pcpu_size
[i
-1] -= extra
;
307 pcpu_size
[i
-1] += extra
;
308 pcpu_size
[i
] -= extra
;
311 /* Split block if warranted */
312 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
313 if (!split_block(i
, size
))
317 pcpu_size
[i
] = -pcpu_size
[i
];
321 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
326 static void percpu_modfree(void *freeme
)
329 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
331 /* First entry is core kernel percpu data. */
332 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
334 pcpu_size
[i
] = -pcpu_size
[i
];
341 /* Merge with previous? */
342 if (pcpu_size
[i
-1] >= 0) {
343 pcpu_size
[i
-1] += pcpu_size
[i
];
345 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
346 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
349 /* Merge with next? */
350 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
351 pcpu_size
[i
] += pcpu_size
[i
+1];
353 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
354 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
358 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
360 const char *secstrings
)
362 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
365 static int percpu_modinit(void)
368 pcpu_num_allocated
= 2;
369 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
371 /* Static in-kernel percpu data (used). */
372 pcpu_size
[0] = -ALIGN(__per_cpu_end
-__per_cpu_start
, SMP_CACHE_BYTES
);
374 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
375 if (pcpu_size
[1] < 0) {
376 printk(KERN_ERR
"No per-cpu room for modules.\n");
382 __initcall(percpu_modinit
);
383 #else /* ... !CONFIG_SMP */
384 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
389 static inline void percpu_modfree(void *pcpuptr
)
393 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
395 const char *secstrings
)
399 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
402 /* pcpusec should be 0, and size of that section should be 0. */
405 #endif /* CONFIG_SMP */
407 #define MODINFO_ATTR(field) \
408 static void setup_modinfo_##field(struct module *mod, const char *s) \
410 mod->field = kstrdup(s, GFP_KERNEL); \
412 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
413 struct module *mod, char *buffer) \
415 return sprintf(buffer, "%s\n", mod->field); \
417 static int modinfo_##field##_exists(struct module *mod) \
419 return mod->field != NULL; \
421 static void free_modinfo_##field(struct module *mod) \
426 static struct module_attribute modinfo_##field = { \
427 .attr = { .name = __stringify(field), .mode = 0444, \
428 .owner = THIS_MODULE }, \
429 .show = show_modinfo_##field, \
430 .setup = setup_modinfo_##field, \
431 .test = modinfo_##field##_exists, \
432 .free = free_modinfo_##field, \
435 MODINFO_ATTR(version
);
436 MODINFO_ATTR(srcversion
);
438 #ifdef CONFIG_MODULE_UNLOAD
439 /* Init the unload section of the module. */
440 static void module_unload_init(struct module
*mod
)
444 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
445 for (i
= 0; i
< NR_CPUS
; i
++)
446 local_set(&mod
->ref
[i
].count
, 0);
447 /* Hold reference count during initialization. */
448 local_set(&mod
->ref
[raw_smp_processor_id()].count
, 1);
449 /* Backwards compatibility macros put refcount during init. */
450 mod
->waiter
= current
;
453 /* modules using other modules */
456 struct list_head list
;
457 struct module
*module_which_uses
;
460 /* Does a already use b? */
461 static int already_uses(struct module
*a
, struct module
*b
)
463 struct module_use
*use
;
465 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
466 if (use
->module_which_uses
== a
) {
467 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
471 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
475 /* Module a uses b */
476 static int use_module(struct module
*a
, struct module
*b
)
478 struct module_use
*use
;
479 if (b
== NULL
|| already_uses(a
, b
)) return 1;
481 if (!strong_try_module_get(b
))
484 DEBUGP("Allocating new usage for %s.\n", a
->name
);
485 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
487 printk("%s: out of memory loading\n", a
->name
);
492 use
->module_which_uses
= a
;
493 list_add(&use
->list
, &b
->modules_which_use_me
);
497 /* Clear the unload stuff of the module. */
498 static void module_unload_free(struct module
*mod
)
502 list_for_each_entry(i
, &modules
, list
) {
503 struct module_use
*use
;
505 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
506 if (use
->module_which_uses
== mod
) {
507 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
509 list_del(&use
->list
);
511 /* There can be at most one match. */
518 #ifdef CONFIG_MODULE_FORCE_UNLOAD
519 static inline int try_force_unload(unsigned int flags
)
521 int ret
= (flags
& O_TRUNC
);
523 add_taint(TAINT_FORCED_RMMOD
);
527 static inline int try_force_unload(unsigned int flags
)
531 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
540 /* Whole machine is stopped with interrupts off when this runs. */
541 static int __try_stop_module(void *_sref
)
543 struct stopref
*sref
= _sref
;
545 /* If it's not unused, quit unless we are told to block. */
546 if ((sref
->flags
& O_NONBLOCK
) && module_refcount(sref
->mod
) != 0) {
547 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
551 /* Mark it as dying. */
552 sref
->mod
->state
= MODULE_STATE_GOING
;
556 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
558 struct stopref sref
= { mod
, flags
, forced
};
560 return stop_machine_run(__try_stop_module
, &sref
, NR_CPUS
);
563 unsigned int module_refcount(struct module
*mod
)
565 unsigned int i
, total
= 0;
567 for (i
= 0; i
< NR_CPUS
; i
++)
568 total
+= local_read(&mod
->ref
[i
].count
);
571 EXPORT_SYMBOL(module_refcount
);
573 /* This exists whether we can unload or not */
574 static void free_module(struct module
*mod
);
576 static void wait_for_zero_refcount(struct module
*mod
)
578 /* Since we might sleep for some time, drop the semaphore first */
579 mutex_unlock(&module_mutex
);
581 DEBUGP("Looking at refcount...\n");
582 set_current_state(TASK_UNINTERRUPTIBLE
);
583 if (module_refcount(mod
) == 0)
587 current
->state
= TASK_RUNNING
;
588 mutex_lock(&module_mutex
);
592 sys_delete_module(const char __user
*name_user
, unsigned int flags
)
595 char name
[MODULE_NAME_LEN
];
598 if (!capable(CAP_SYS_MODULE
))
601 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
603 name
[MODULE_NAME_LEN
-1] = '\0';
605 if (mutex_lock_interruptible(&module_mutex
) != 0)
608 mod
= find_module(name
);
614 if (!list_empty(&mod
->modules_which_use_me
)) {
615 /* Other modules depend on us: get rid of them first. */
620 /* Doing init or already dying? */
621 if (mod
->state
!= MODULE_STATE_LIVE
) {
622 /* FIXME: if (force), slam module count and wake up
624 DEBUGP("%s already dying\n", mod
->name
);
629 /* If it has an init func, it must have an exit func to unload */
630 if ((mod
->init
!= NULL
&& mod
->exit
== NULL
)
632 forced
= try_force_unload(flags
);
634 /* This module can't be removed */
640 /* Set this up before setting mod->state */
641 mod
->waiter
= current
;
643 /* Stop the machine so refcounts can't move and disable module. */
644 ret
= try_stop_module(mod
, flags
, &forced
);
648 /* Never wait if forced. */
649 if (!forced
&& module_refcount(mod
) != 0)
650 wait_for_zero_refcount(mod
);
652 /* Final destruction now noone is using it. */
653 if (mod
->exit
!= NULL
) {
654 mutex_unlock(&module_mutex
);
656 mutex_lock(&module_mutex
);
661 mutex_unlock(&module_mutex
);
665 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
667 struct module_use
*use
;
668 int printed_something
= 0;
670 seq_printf(m
, " %u ", module_refcount(mod
));
672 /* Always include a trailing , so userspace can differentiate
673 between this and the old multi-field proc format. */
674 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
675 printed_something
= 1;
676 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
680 printed_something
= 1;
681 seq_printf(m
, "[unsafe],");
684 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
685 printed_something
= 1;
686 seq_printf(m
, "[permanent],");
689 if (!printed_something
)
693 void __symbol_put(const char *symbol
)
695 struct module
*owner
;
697 const unsigned long *crc
;
699 spin_lock_irqsave(&modlist_lock
, flags
);
700 if (!__find_symbol(symbol
, &owner
, &crc
, 1))
703 spin_unlock_irqrestore(&modlist_lock
, flags
);
705 EXPORT_SYMBOL(__symbol_put
);
707 void symbol_put_addr(void *addr
)
709 struct module
*modaddr
;
711 if (core_kernel_text((unsigned long)addr
))
714 if (!(modaddr
= module_text_address((unsigned long)addr
)))
718 EXPORT_SYMBOL_GPL(symbol_put_addr
);
720 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
721 struct module
*mod
, char *buffer
)
723 /* sysfs holds a reference */
724 return sprintf(buffer
, "%u\n", module_refcount(mod
)-1);
727 static struct module_attribute refcnt
= {
728 .attr
= { .name
= "refcnt", .mode
= 0444, .owner
= THIS_MODULE
},
732 #else /* !CONFIG_MODULE_UNLOAD */
733 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
735 /* We don't know the usage count, or what modules are using. */
736 seq_printf(m
, " - -");
739 static inline void module_unload_free(struct module
*mod
)
743 static inline int use_module(struct module
*a
, struct module
*b
)
745 return strong_try_module_get(b
);
748 static inline void module_unload_init(struct module
*mod
)
751 #endif /* CONFIG_MODULE_UNLOAD */
753 static struct module_attribute
*modinfo_attrs
[] = {
756 #ifdef CONFIG_MODULE_UNLOAD
762 static const char vermagic
[] = VERMAGIC_STRING
;
764 #ifdef CONFIG_MODVERSIONS
765 static int check_version(Elf_Shdr
*sechdrs
,
766 unsigned int versindex
,
769 const unsigned long *crc
)
771 unsigned int i
, num_versions
;
772 struct modversion_info
*versions
;
774 /* Exporting module didn't supply crcs? OK, we're already tainted. */
778 versions
= (void *) sechdrs
[versindex
].sh_addr
;
779 num_versions
= sechdrs
[versindex
].sh_size
780 / sizeof(struct modversion_info
);
782 for (i
= 0; i
< num_versions
; i
++) {
783 if (strcmp(versions
[i
].name
, symname
) != 0)
786 if (versions
[i
].crc
== *crc
)
788 printk("%s: disagrees about version of symbol %s\n",
790 DEBUGP("Found checksum %lX vs module %lX\n",
791 *crc
, versions
[i
].crc
);
794 /* Not in module's version table. OK, but that taints the kernel. */
795 if (!(tainted
& TAINT_FORCED_MODULE
)) {
796 printk("%s: no version for \"%s\" found: kernel tainted.\n",
798 add_taint(TAINT_FORCED_MODULE
);
803 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
804 unsigned int versindex
,
807 const unsigned long *crc
;
808 struct module
*owner
;
810 if (!__find_symbol("struct_module", &owner
, &crc
, 1))
812 return check_version(sechdrs
, versindex
, "struct_module", mod
,
816 /* First part is kernel version, which we ignore. */
817 static inline int same_magic(const char *amagic
, const char *bmagic
)
819 amagic
+= strcspn(amagic
, " ");
820 bmagic
+= strcspn(bmagic
, " ");
821 return strcmp(amagic
, bmagic
) == 0;
824 static inline int check_version(Elf_Shdr
*sechdrs
,
825 unsigned int versindex
,
828 const unsigned long *crc
)
833 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
834 unsigned int versindex
,
840 static inline int same_magic(const char *amagic
, const char *bmagic
)
842 return strcmp(amagic
, bmagic
) == 0;
844 #endif /* CONFIG_MODVERSIONS */
846 /* Resolve a symbol for this module. I.e. if we find one, record usage.
847 Must be holding module_mutex. */
848 static unsigned long resolve_symbol(Elf_Shdr
*sechdrs
,
849 unsigned int versindex
,
853 struct module
*owner
;
855 const unsigned long *crc
;
857 ret
= __find_symbol(name
, &owner
, &crc
, mod
->license_gplok
);
859 /* use_module can fail due to OOM, or module unloading */
860 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
861 !use_module(mod
, owner
))
869 * /sys/module/foo/sections stuff
870 * J. Corbet <corbet@lwn.net>
872 #ifdef CONFIG_KALLSYMS
873 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
874 struct module
*mod
, char *buf
)
876 struct module_sect_attr
*sattr
=
877 container_of(mattr
, struct module_sect_attr
, mattr
);
878 return sprintf(buf
, "0x%lx\n", sattr
->address
);
881 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
882 char *secstrings
, Elf_Shdr
*sechdrs
)
884 unsigned int nloaded
= 0, i
, size
[2];
885 struct module_sect_attrs
*sect_attrs
;
886 struct module_sect_attr
*sattr
;
887 struct attribute
**gattr
;
889 /* Count loaded sections and allocate structures */
890 for (i
= 0; i
< nsect
; i
++)
891 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
893 size
[0] = ALIGN(sizeof(*sect_attrs
)
894 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
895 sizeof(sect_attrs
->grp
.attrs
[0]));
896 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
897 if (! (sect_attrs
= kmalloc(size
[0] + size
[1], GFP_KERNEL
)))
900 /* Setup section attributes. */
901 sect_attrs
->grp
.name
= "sections";
902 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
904 sattr
= §_attrs
->attrs
[0];
905 gattr
= §_attrs
->grp
.attrs
[0];
906 for (i
= 0; i
< nsect
; i
++) {
907 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
909 sattr
->address
= sechdrs
[i
].sh_addr
;
910 strlcpy(sattr
->name
, secstrings
+ sechdrs
[i
].sh_name
,
911 MODULE_SECT_NAME_LEN
);
912 sattr
->mattr
.show
= module_sect_show
;
913 sattr
->mattr
.store
= NULL
;
914 sattr
->mattr
.attr
.name
= sattr
->name
;
915 sattr
->mattr
.attr
.owner
= mod
;
916 sattr
->mattr
.attr
.mode
= S_IRUGO
;
917 *(gattr
++) = &(sattr
++)->mattr
.attr
;
921 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
924 mod
->sect_attrs
= sect_attrs
;
930 static void remove_sect_attrs(struct module
*mod
)
932 if (mod
->sect_attrs
) {
933 sysfs_remove_group(&mod
->mkobj
.kobj
,
934 &mod
->sect_attrs
->grp
);
935 /* We are positive that no one is using any sect attrs
936 * at this point. Deallocate immediately. */
937 kfree(mod
->sect_attrs
);
938 mod
->sect_attrs
= NULL
;
944 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
945 char *sectstrings
, Elf_Shdr
*sechdrs
)
949 static inline void remove_sect_attrs(struct module
*mod
)
952 #endif /* CONFIG_KALLSYMS */
954 static int module_add_modinfo_attrs(struct module
*mod
)
956 struct module_attribute
*attr
;
957 struct module_attribute
*temp_attr
;
961 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
962 (ARRAY_SIZE(modinfo_attrs
) + 1)),
964 if (!mod
->modinfo_attrs
)
967 temp_attr
= mod
->modinfo_attrs
;
968 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
970 (attr
->test
&& attr
->test(mod
))) {
971 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
972 temp_attr
->attr
.owner
= mod
;
973 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
980 static void module_remove_modinfo_attrs(struct module
*mod
)
982 struct module_attribute
*attr
;
985 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
986 /* pick a field to test for end of list */
987 if (!attr
->attr
.name
)
989 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
993 kfree(mod
->modinfo_attrs
);
996 static int mod_sysfs_setup(struct module
*mod
,
997 struct kernel_param
*kparam
,
998 unsigned int num_params
)
1002 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1003 err
= kobject_set_name(&mod
->mkobj
.kobj
, "%s", mod
->name
);
1006 kobj_set_kset_s(&mod
->mkobj
, module_subsys
);
1007 mod
->mkobj
.mod
= mod
;
1008 err
= kobject_register(&mod
->mkobj
.kobj
);
1012 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1016 err
= module_add_modinfo_attrs(mod
);
1023 kobject_unregister(&mod
->mkobj
.kobj
);
1028 static void mod_kobject_remove(struct module
*mod
)
1030 module_remove_modinfo_attrs(mod
);
1031 module_param_sysfs_remove(mod
);
1033 kobject_unregister(&mod
->mkobj
.kobj
);
1037 * unlink the module with the whole machine is stopped with interrupts off
1038 * - this defends against kallsyms not taking locks
1040 static int __unlink_module(void *_mod
)
1042 struct module
*mod
= _mod
;
1043 list_del(&mod
->list
);
1047 /* Free a module, remove from lists, etc (must hold module mutex). */
1048 static void free_module(struct module
*mod
)
1050 /* Delete from various lists */
1051 stop_machine_run(__unlink_module
, mod
, NR_CPUS
);
1052 remove_sect_attrs(mod
);
1053 mod_kobject_remove(mod
);
1055 unwind_remove_table(mod
->unwind_info
, 0);
1057 /* Arch-specific cleanup. */
1058 module_arch_cleanup(mod
);
1060 /* Module unload stuff */
1061 module_unload_free(mod
);
1063 /* This may be NULL, but that's OK */
1064 module_free(mod
, mod
->module_init
);
1067 percpu_modfree(mod
->percpu
);
1069 /* Finally, free the core (containing the module structure) */
1070 module_free(mod
, mod
->module_core
);
1073 void *__symbol_get(const char *symbol
)
1075 struct module
*owner
;
1076 unsigned long value
, flags
;
1077 const unsigned long *crc
;
1079 spin_lock_irqsave(&modlist_lock
, flags
);
1080 value
= __find_symbol(symbol
, &owner
, &crc
, 1);
1081 if (value
&& !strong_try_module_get(owner
))
1083 spin_unlock_irqrestore(&modlist_lock
, flags
);
1085 return (void *)value
;
1087 EXPORT_SYMBOL_GPL(__symbol_get
);
1090 * Ensure that an exported symbol [global namespace] does not already exist
1091 * in the Kernel or in some other modules exported symbol table.
1093 static int verify_export_symbols(struct module
*mod
)
1095 const char *name
= NULL
;
1096 unsigned long i
, ret
= 0;
1097 struct module
*owner
;
1098 const unsigned long *crc
;
1100 for (i
= 0; i
< mod
->num_syms
; i
++)
1101 if (__find_symbol(mod
->syms
[i
].name
, &owner
, &crc
, 1)) {
1102 name
= mod
->syms
[i
].name
;
1107 for (i
= 0; i
< mod
->num_gpl_syms
; i
++)
1108 if (__find_symbol(mod
->gpl_syms
[i
].name
, &owner
, &crc
, 1)) {
1109 name
= mod
->gpl_syms
[i
].name
;
1116 printk(KERN_ERR
"%s: exports duplicate symbol %s (owned by %s)\n",
1117 mod
->name
, name
, module_name(owner
));
1122 /* Change all symbols so that sh_value encodes the pointer directly. */
1123 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1124 unsigned int symindex
,
1126 unsigned int versindex
,
1127 unsigned int pcpuindex
,
1130 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1131 unsigned long secbase
;
1132 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1135 for (i
= 1; i
< n
; i
++) {
1136 switch (sym
[i
].st_shndx
) {
1138 /* We compiled with -fno-common. These are not
1139 supposed to happen. */
1140 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1141 printk("%s: please compile with -fno-common\n",
1147 /* Don't need to do anything */
1148 DEBUGP("Absolute symbol: 0x%08lx\n",
1149 (long)sym
[i
].st_value
);
1154 = resolve_symbol(sechdrs
, versindex
,
1155 strtab
+ sym
[i
].st_name
, mod
);
1157 /* Ok if resolved. */
1158 if (sym
[i
].st_value
!= 0)
1161 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1164 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1165 mod
->name
, strtab
+ sym
[i
].st_name
);
1170 /* Divert to percpu allocation if a percpu var. */
1171 if (sym
[i
].st_shndx
== pcpuindex
)
1172 secbase
= (unsigned long)mod
->percpu
;
1174 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1175 sym
[i
].st_value
+= secbase
;
1183 /* Update size with this section: return offset. */
1184 static long get_offset(unsigned long *size
, Elf_Shdr
*sechdr
)
1188 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1189 *size
= ret
+ sechdr
->sh_size
;
1193 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1194 might -- code, read-only data, read-write data, small data. Tally
1195 sizes, and place the offsets into sh_entsize fields: high bit means it
1197 static void layout_sections(struct module
*mod
,
1198 const Elf_Ehdr
*hdr
,
1200 const char *secstrings
)
1202 static unsigned long const masks
[][2] = {
1203 /* NOTE: all executable code must be the first section
1204 * in this array; otherwise modify the text_size
1205 * finder in the two loops below */
1206 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1207 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1208 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1209 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1213 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1214 sechdrs
[i
].sh_entsize
= ~0UL;
1216 DEBUGP("Core section allocation order:\n");
1217 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1218 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1219 Elf_Shdr
*s
= &sechdrs
[i
];
1221 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1222 || (s
->sh_flags
& masks
[m
][1])
1223 || s
->sh_entsize
!= ~0UL
1224 || strncmp(secstrings
+ s
->sh_name
,
1227 s
->sh_entsize
= get_offset(&mod
->core_size
, s
);
1228 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1231 mod
->core_text_size
= mod
->core_size
;
1234 DEBUGP("Init section allocation order:\n");
1235 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1236 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1237 Elf_Shdr
*s
= &sechdrs
[i
];
1239 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1240 || (s
->sh_flags
& masks
[m
][1])
1241 || s
->sh_entsize
!= ~0UL
1242 || strncmp(secstrings
+ s
->sh_name
,
1245 s
->sh_entsize
= (get_offset(&mod
->init_size
, s
)
1246 | INIT_OFFSET_MASK
);
1247 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1250 mod
->init_text_size
= mod
->init_size
;
1254 static inline int license_is_gpl_compatible(const char *license
)
1256 return (strcmp(license
, "GPL") == 0
1257 || strcmp(license
, "GPL v2") == 0
1258 || strcmp(license
, "GPL and additional rights") == 0
1259 || strcmp(license
, "Dual BSD/GPL") == 0
1260 || strcmp(license
, "Dual MIT/GPL") == 0
1261 || strcmp(license
, "Dual MPL/GPL") == 0);
1264 static void set_license(struct module
*mod
, const char *license
)
1267 license
= "unspecified";
1269 mod
->license_gplok
= license_is_gpl_compatible(license
);
1270 if (!mod
->license_gplok
&& !(tainted
& TAINT_PROPRIETARY_MODULE
)) {
1271 printk(KERN_WARNING
"%s: module license '%s' taints kernel.\n",
1272 mod
->name
, license
);
1273 add_taint(TAINT_PROPRIETARY_MODULE
);
1277 /* Parse tag=value strings from .modinfo section */
1278 static char *next_string(char *string
, unsigned long *secsize
)
1280 /* Skip non-zero chars */
1283 if ((*secsize
)-- <= 1)
1287 /* Skip any zero padding. */
1288 while (!string
[0]) {
1290 if ((*secsize
)-- <= 1)
1296 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1301 unsigned int taglen
= strlen(tag
);
1302 unsigned long size
= sechdrs
[info
].sh_size
;
1304 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1305 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1306 return p
+ taglen
+ 1;
1311 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1312 unsigned int infoindex
)
1314 struct module_attribute
*attr
;
1317 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1320 get_modinfo(sechdrs
,
1326 #ifdef CONFIG_KALLSYMS
1327 int is_exported(const char *name
, const struct module
*mod
)
1329 if (!mod
&& lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
))
1332 if (mod
&& lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
))
1339 static char elf_type(const Elf_Sym
*sym
,
1341 const char *secstrings
,
1344 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1345 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1350 if (sym
->st_shndx
== SHN_UNDEF
)
1352 if (sym
->st_shndx
== SHN_ABS
)
1354 if (sym
->st_shndx
>= SHN_LORESERVE
)
1356 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1358 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1359 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1360 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1362 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1367 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1368 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1373 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1374 ".debug", strlen(".debug")) == 0)
1379 static void add_kallsyms(struct module
*mod
,
1381 unsigned int symindex
,
1382 unsigned int strindex
,
1383 const char *secstrings
)
1387 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1388 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1389 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1391 /* Set types up while we still have access to sections. */
1392 for (i
= 0; i
< mod
->num_symtab
; i
++)
1393 mod
->symtab
[i
].st_info
1394 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1397 static inline void add_kallsyms(struct module
*mod
,
1399 unsigned int symindex
,
1400 unsigned int strindex
,
1401 const char *secstrings
)
1404 #endif /* CONFIG_KALLSYMS */
1406 /* Allocate and load the module: note that size of section 0 is always
1407 zero, and we rely on this for optional sections. */
1408 static struct module
*load_module(void __user
*umod
,
1410 const char __user
*uargs
)
1414 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1415 unsigned int i
, symindex
= 0, strindex
= 0, setupindex
, exindex
,
1416 exportindex
, modindex
, obsparmindex
, infoindex
, gplindex
,
1417 crcindex
, gplcrcindex
, versindex
, pcpuindex
, gplfutureindex
,
1418 gplfuturecrcindex
, unwindex
= 0;
1421 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1422 struct exception_table_entry
*extable
;
1423 mm_segment_t old_fs
;
1425 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1427 if (len
< sizeof(*hdr
))
1428 return ERR_PTR(-ENOEXEC
);
1430 /* Suck in entire file: we'll want most of it. */
1431 /* vmalloc barfs on "unusual" numbers. Check here */
1432 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1433 return ERR_PTR(-ENOMEM
);
1434 if (copy_from_user(hdr
, umod
, len
) != 0) {
1439 /* Sanity checks against insmoding binaries or wrong arch,
1440 weird elf version */
1441 if (memcmp(hdr
->e_ident
, ELFMAG
, 4) != 0
1442 || hdr
->e_type
!= ET_REL
1443 || !elf_check_arch(hdr
)
1444 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1449 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1452 /* Convenience variables */
1453 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1454 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1455 sechdrs
[0].sh_addr
= 0;
1457 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1458 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1459 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1462 /* Mark all sections sh_addr with their address in the
1464 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1466 /* Internal symbols and strings. */
1467 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1469 strindex
= sechdrs
[i
].sh_link
;
1470 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1472 #ifndef CONFIG_MODULE_UNLOAD
1473 /* Don't load .exit sections */
1474 if (strncmp(secstrings
+sechdrs
[i
].sh_name
, ".exit", 5) == 0)
1475 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1479 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1480 ".gnu.linkonce.this_module");
1482 printk(KERN_WARNING
"No module found in object\n");
1486 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1488 if (symindex
== 0) {
1489 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1495 /* Optional sections */
1496 exportindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab");
1497 gplindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl");
1498 gplfutureindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl_future");
1499 crcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab");
1500 gplcrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
1501 gplfuturecrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl_future");
1502 setupindex
= find_sec(hdr
, sechdrs
, secstrings
, "__param");
1503 exindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ex_table");
1504 obsparmindex
= find_sec(hdr
, sechdrs
, secstrings
, "__obsparm");
1505 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1506 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1507 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1508 #ifdef ARCH_UNWIND_SECTION_NAME
1509 unwindex
= find_sec(hdr
, sechdrs
, secstrings
, ARCH_UNWIND_SECTION_NAME
);
1512 /* Don't keep modinfo section */
1513 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1514 #ifdef CONFIG_KALLSYMS
1515 /* Keep symbol and string tables for decoding later. */
1516 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1517 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1520 sechdrs
[unwindex
].sh_flags
|= SHF_ALLOC
;
1522 /* Check module struct version now, before we try to use module. */
1523 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1528 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1529 /* This is allowed: modprobe --force will invalidate it. */
1531 add_taint(TAINT_FORCED_MODULE
);
1532 printk(KERN_WARNING
"%s: no version magic, tainting kernel.\n",
1534 } else if (!same_magic(modmagic
, vermagic
)) {
1535 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
1536 mod
->name
, modmagic
, vermagic
);
1541 /* Now copy in args */
1542 args
= strndup_user(uargs
, ~0UL >> 1);
1544 err
= PTR_ERR(args
);
1548 if (find_module(mod
->name
)) {
1553 mod
->state
= MODULE_STATE_COMING
;
1555 /* Allow arches to frob section contents and sizes. */
1556 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
1561 /* We have a special allocation for this section. */
1562 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
1563 sechdrs
[pcpuindex
].sh_addralign
,
1569 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1570 mod
->percpu
= percpu
;
1573 /* Determine total sizes, and put offsets in sh_entsize. For now
1574 this is done generically; there doesn't appear to be any
1575 special cases for the architectures. */
1576 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
1578 /* Do the allocs. */
1579 ptr
= module_alloc(mod
->core_size
);
1584 memset(ptr
, 0, mod
->core_size
);
1585 mod
->module_core
= ptr
;
1587 ptr
= module_alloc(mod
->init_size
);
1588 if (!ptr
&& mod
->init_size
) {
1592 memset(ptr
, 0, mod
->init_size
);
1593 mod
->module_init
= ptr
;
1595 /* Transfer each section which specifies SHF_ALLOC */
1596 DEBUGP("final section addresses:\n");
1597 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
1600 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1603 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
1604 dest
= mod
->module_init
1605 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
1607 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
1609 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
1610 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
1611 sechdrs
[i
].sh_size
);
1612 /* Update sh_addr to point to copy in image. */
1613 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
1614 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
1616 /* Module has been moved. */
1617 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1619 /* Now we've moved module, initialize linked lists, etc. */
1620 module_unload_init(mod
);
1622 /* Set up license info based on the info section */
1623 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
1625 if (strcmp(mod
->name
, "ndiswrapper") == 0)
1626 add_taint(TAINT_PROPRIETARY_MODULE
);
1627 if (strcmp(mod
->name
, "driverloader") == 0)
1628 add_taint(TAINT_PROPRIETARY_MODULE
);
1630 /* Set up MODINFO_ATTR fields */
1631 setup_modinfo(mod
, sechdrs
, infoindex
);
1633 /* Fix up syms, so that st_value is a pointer to location. */
1634 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
1639 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1640 mod
->num_syms
= sechdrs
[exportindex
].sh_size
/ sizeof(*mod
->syms
);
1641 mod
->syms
= (void *)sechdrs
[exportindex
].sh_addr
;
1643 mod
->crcs
= (void *)sechdrs
[crcindex
].sh_addr
;
1644 mod
->num_gpl_syms
= sechdrs
[gplindex
].sh_size
/ sizeof(*mod
->gpl_syms
);
1645 mod
->gpl_syms
= (void *)sechdrs
[gplindex
].sh_addr
;
1647 mod
->gpl_crcs
= (void *)sechdrs
[gplcrcindex
].sh_addr
;
1648 mod
->num_gpl_future_syms
= sechdrs
[gplfutureindex
].sh_size
/
1649 sizeof(*mod
->gpl_future_syms
);
1650 mod
->gpl_future_syms
= (void *)sechdrs
[gplfutureindex
].sh_addr
;
1651 if (gplfuturecrcindex
)
1652 mod
->gpl_future_crcs
= (void *)sechdrs
[gplfuturecrcindex
].sh_addr
;
1654 #ifdef CONFIG_MODVERSIONS
1655 if ((mod
->num_syms
&& !crcindex
) ||
1656 (mod
->num_gpl_syms
&& !gplcrcindex
) ||
1657 (mod
->num_gpl_future_syms
&& !gplfuturecrcindex
)) {
1658 printk(KERN_WARNING
"%s: No versions for exported symbols."
1659 " Tainting kernel.\n", mod
->name
);
1660 add_taint(TAINT_FORCED_MODULE
);
1664 /* Now do relocations. */
1665 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1666 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
1667 unsigned int info
= sechdrs
[i
].sh_info
;
1669 /* Not a valid relocation section? */
1670 if (info
>= hdr
->e_shnum
)
1673 /* Don't bother with non-allocated sections */
1674 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
1677 if (sechdrs
[i
].sh_type
== SHT_REL
)
1678 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
1679 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
1680 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
1686 /* Find duplicate symbols */
1687 err
= verify_export_symbols(mod
);
1692 /* Set up and sort exception table */
1693 mod
->num_exentries
= sechdrs
[exindex
].sh_size
/ sizeof(*mod
->extable
);
1694 mod
->extable
= extable
= (void *)sechdrs
[exindex
].sh_addr
;
1695 sort_extable(extable
, extable
+ mod
->num_exentries
);
1697 /* Finally, copy percpu area over. */
1698 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
1699 sechdrs
[pcpuindex
].sh_size
);
1701 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
1703 err
= module_finalize(hdr
, sechdrs
, mod
);
1707 /* flush the icache in correct context */
1712 * Flush the instruction cache, since we've played with text.
1713 * Do it before processing of module parameters, so the module
1714 * can provide parameter accessor functions of its own.
1716 if (mod
->module_init
)
1717 flush_icache_range((unsigned long)mod
->module_init
,
1718 (unsigned long)mod
->module_init
1720 flush_icache_range((unsigned long)mod
->module_core
,
1721 (unsigned long)mod
->module_core
+ mod
->core_size
);
1727 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
1730 /* Size of section 0 is 0, so this works well if no params */
1731 err
= parse_args(mod
->name
, mod
->args
,
1732 (struct kernel_param
*)
1733 sechdrs
[setupindex
].sh_addr
,
1734 sechdrs
[setupindex
].sh_size
1735 / sizeof(struct kernel_param
),
1740 err
= mod_sysfs_setup(mod
,
1741 (struct kernel_param
*)
1742 sechdrs
[setupindex
].sh_addr
,
1743 sechdrs
[setupindex
].sh_size
1744 / sizeof(struct kernel_param
));
1747 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
1749 /* Size of section 0 is 0, so this works well if no unwind info. */
1750 mod
->unwind_info
= unwind_add_table(mod
,
1751 (void *)sechdrs
[unwindex
].sh_addr
,
1752 sechdrs
[unwindex
].sh_size
);
1754 /* Get rid of temporary copy */
1761 module_arch_cleanup(mod
);
1763 module_unload_free(mod
);
1764 module_free(mod
, mod
->module_init
);
1766 module_free(mod
, mod
->module_core
);
1769 percpu_modfree(percpu
);
1774 return ERR_PTR(err
);
1777 printk(KERN_ERR
"Module len %lu truncated\n", len
);
1783 * link the module with the whole machine is stopped with interrupts off
1784 * - this defends against kallsyms not taking locks
1786 static int __link_module(void *_mod
)
1788 struct module
*mod
= _mod
;
1789 list_add(&mod
->list
, &modules
);
1793 /* This is where the real work happens */
1795 sys_init_module(void __user
*umod
,
1797 const char __user
*uargs
)
1802 /* Must have permission */
1803 if (!capable(CAP_SYS_MODULE
))
1806 /* Only one module load at a time, please */
1807 if (mutex_lock_interruptible(&module_mutex
) != 0)
1810 /* Do all the hard work */
1811 mod
= load_module(umod
, len
, uargs
);
1813 mutex_unlock(&module_mutex
);
1814 return PTR_ERR(mod
);
1817 /* Now sew it into the lists. They won't access us, since
1818 strong_try_module_get() will fail. */
1819 stop_machine_run(__link_module
, mod
, NR_CPUS
);
1821 /* Drop lock so they can recurse */
1822 mutex_unlock(&module_mutex
);
1824 blocking_notifier_call_chain(&module_notify_list
,
1825 MODULE_STATE_COMING
, mod
);
1827 /* Start the module */
1828 if (mod
->init
!= NULL
)
1831 /* Init routine failed: abort. Try to protect us from
1832 buggy refcounters. */
1833 mod
->state
= MODULE_STATE_GOING
;
1834 synchronize_sched();
1836 printk(KERN_ERR
"%s: module is now stuck!\n",
1840 mutex_lock(&module_mutex
);
1842 mutex_unlock(&module_mutex
);
1847 /* Now it's a first class citizen! */
1848 mutex_lock(&module_mutex
);
1849 mod
->state
= MODULE_STATE_LIVE
;
1850 /* Drop initial reference. */
1852 unwind_remove_table(mod
->unwind_info
, 1);
1853 module_free(mod
, mod
->module_init
);
1854 mod
->module_init
= NULL
;
1856 mod
->init_text_size
= 0;
1857 mutex_unlock(&module_mutex
);
1862 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
1864 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
1867 #ifdef CONFIG_KALLSYMS
1869 * This ignores the intensely annoying "mapping symbols" found
1870 * in ARM ELF files: $a, $t and $d.
1872 static inline int is_arm_mapping_symbol(const char *str
)
1874 return str
[0] == '$' && strchr("atd", str
[1])
1875 && (str
[2] == '\0' || str
[2] == '.');
1878 static const char *get_ksymbol(struct module
*mod
,
1880 unsigned long *size
,
1881 unsigned long *offset
)
1883 unsigned int i
, best
= 0;
1884 unsigned long nextval
;
1886 /* At worse, next value is at end of module */
1887 if (within(addr
, mod
->module_init
, mod
->init_size
))
1888 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
1890 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
1892 /* Scan for closest preceeding symbol, and next symbol. (ELF
1893 starts real symbols at 1). */
1894 for (i
= 1; i
< mod
->num_symtab
; i
++) {
1895 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
1898 /* We ignore unnamed symbols: they're uninformative
1899 * and inserted at a whim. */
1900 if (mod
->symtab
[i
].st_value
<= addr
1901 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
1902 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
1903 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
1905 if (mod
->symtab
[i
].st_value
> addr
1906 && mod
->symtab
[i
].st_value
< nextval
1907 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
1908 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
1909 nextval
= mod
->symtab
[i
].st_value
;
1915 *size
= nextval
- mod
->symtab
[best
].st_value
;
1916 *offset
= addr
- mod
->symtab
[best
].st_value
;
1917 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
1920 /* For kallsyms to ask for address resolution. NULL means not found.
1921 We don't lock, as this is used for oops resolution and races are a
1923 const char *module_address_lookup(unsigned long addr
,
1924 unsigned long *size
,
1925 unsigned long *offset
,
1930 list_for_each_entry(mod
, &modules
, list
) {
1931 if (within(addr
, mod
->module_init
, mod
->init_size
)
1932 || within(addr
, mod
->module_core
, mod
->core_size
)) {
1933 *modname
= mod
->name
;
1934 return get_ksymbol(mod
, addr
, size
, offset
);
1940 struct module
*module_get_kallsym(unsigned int symnum
,
1941 unsigned long *value
,
1947 mutex_lock(&module_mutex
);
1948 list_for_each_entry(mod
, &modules
, list
) {
1949 if (symnum
< mod
->num_symtab
) {
1950 *value
= mod
->symtab
[symnum
].st_value
;
1951 *type
= mod
->symtab
[symnum
].st_info
;
1953 mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
1955 mutex_unlock(&module_mutex
);
1958 symnum
-= mod
->num_symtab
;
1960 mutex_unlock(&module_mutex
);
1964 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
1968 for (i
= 0; i
< mod
->num_symtab
; i
++)
1969 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
1970 mod
->symtab
[i
].st_info
!= 'U')
1971 return mod
->symtab
[i
].st_value
;
1975 /* Look for this name: can be of form module:name. */
1976 unsigned long module_kallsyms_lookup_name(const char *name
)
1980 unsigned long ret
= 0;
1982 /* Don't lock: we're in enough trouble already. */
1983 if ((colon
= strchr(name
, ':')) != NULL
) {
1985 if ((mod
= find_module(name
)) != NULL
)
1986 ret
= mod_find_symname(mod
, colon
+1);
1989 list_for_each_entry(mod
, &modules
, list
)
1990 if ((ret
= mod_find_symname(mod
, name
)) != 0)
1995 #endif /* CONFIG_KALLSYMS */
1997 /* Called by the /proc file system to return a list of modules. */
1998 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2000 struct list_head
*i
;
2003 mutex_lock(&module_mutex
);
2004 list_for_each(i
, &modules
) {
2013 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2015 struct list_head
*i
= p
;
2017 if (i
->next
== &modules
)
2022 static void m_stop(struct seq_file
*m
, void *p
)
2024 mutex_unlock(&module_mutex
);
2027 static int m_show(struct seq_file
*m
, void *p
)
2029 struct module
*mod
= list_entry(p
, struct module
, list
);
2030 seq_printf(m
, "%s %lu",
2031 mod
->name
, mod
->init_size
+ mod
->core_size
);
2032 print_unload_info(m
, mod
);
2034 /* Informative for users. */
2035 seq_printf(m
, " %s",
2036 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2037 mod
->state
== MODULE_STATE_COMING
? "Loading":
2039 /* Used by oprofile and other similar tools. */
2040 seq_printf(m
, " 0x%p", mod
->module_core
);
2042 seq_printf(m
, "\n");
2046 /* Format: modulename size refcount deps address
2048 Where refcount is a number or -, and deps is a comma-separated list
2051 struct seq_operations modules_op
= {
2058 /* Given an address, look for it in the module exception tables. */
2059 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2061 unsigned long flags
;
2062 const struct exception_table_entry
*e
= NULL
;
2065 spin_lock_irqsave(&modlist_lock
, flags
);
2066 list_for_each_entry(mod
, &modules
, list
) {
2067 if (mod
->num_exentries
== 0)
2070 e
= search_extable(mod
->extable
,
2071 mod
->extable
+ mod
->num_exentries
- 1,
2076 spin_unlock_irqrestore(&modlist_lock
, flags
);
2078 /* Now, if we found one, we are running inside it now, hence
2079 we cannot unload the module, hence no refcnt needed. */
2083 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2084 struct module
*__module_text_address(unsigned long addr
)
2088 list_for_each_entry(mod
, &modules
, list
)
2089 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
2090 || within(addr
, mod
->module_core
, mod
->core_text_size
))
2095 struct module
*module_text_address(unsigned long addr
)
2098 unsigned long flags
;
2100 spin_lock_irqsave(&modlist_lock
, flags
);
2101 mod
= __module_text_address(addr
);
2102 spin_unlock_irqrestore(&modlist_lock
, flags
);
2107 /* Don't grab lock, we're oopsing. */
2108 void print_modules(void)
2112 printk("Modules linked in:");
2113 list_for_each_entry(mod
, &modules
, list
)
2114 printk(" %s", mod
->name
);
2118 void module_add_driver(struct module
*mod
, struct device_driver
*drv
)
2123 /* Don't check return code; this call is idempotent */
2124 sysfs_create_link(&drv
->kobj
, &mod
->mkobj
.kobj
, "module");
2126 EXPORT_SYMBOL(module_add_driver
);
2128 void module_remove_driver(struct device_driver
*drv
)
2132 sysfs_remove_link(&drv
->kobj
, "module");
2134 EXPORT_SYMBOL(module_remove_driver
);
2136 #ifdef CONFIG_MODVERSIONS
2137 /* Generate the signature for struct module here, too, for modversions. */
2138 void struct_module(struct module
*mod
) { return; }
2139 EXPORT_SYMBOL(struct_module
);