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/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/init.h>
22 #include <linux/kallsyms.h>
23 #include <linux/sysfs.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/elf.h>
28 #include <linux/seq_file.h>
29 #include <linux/syscalls.h>
30 #include <linux/fcntl.h>
31 #include <linux/rcupdate.h>
32 #include <linux/capability.h>
33 #include <linux/cpu.h>
34 #include <linux/moduleparam.h>
35 #include <linux/errno.h>
36 #include <linux/err.h>
37 #include <linux/vermagic.h>
38 #include <linux/notifier.h>
39 #include <linux/sched.h>
40 #include <linux/stop_machine.h>
41 #include <linux/device.h>
42 #include <linux/string.h>
43 #include <linux/mutex.h>
44 #include <linux/unwind.h>
45 #include <asm/uaccess.h>
46 #include <asm/cacheflush.h>
47 #include <linux/license.h>
48 #include <asm/sections.h>
53 #define DEBUGP(fmt , a...)
56 #ifndef ARCH_SHF_SMALL
57 #define ARCH_SHF_SMALL 0
60 /* If this is set, the section belongs in the init part of the module */
61 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
63 /* List of modules, protected by module_mutex or preempt_disable
64 * (add/delete uses stop_machine). */
65 static DEFINE_MUTEX(module_mutex
);
66 static LIST_HEAD(modules
);
68 /* Waiting for a module to finish initializing? */
69 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
71 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
73 int register_module_notifier(struct notifier_block
* nb
)
75 return blocking_notifier_chain_register(&module_notify_list
, nb
);
77 EXPORT_SYMBOL(register_module_notifier
);
79 int unregister_module_notifier(struct notifier_block
* nb
)
81 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
83 EXPORT_SYMBOL(unregister_module_notifier
);
85 /* We require a truly strong try_module_get(): 0 means failure due to
86 ongoing or failed initialization etc. */
87 static inline int strong_try_module_get(struct module
*mod
)
89 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
91 if (try_module_get(mod
))
97 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
104 * A thread that wants to hold a reference to a module only while it
105 * is running can call this to safely exit. nfsd and lockd use this.
107 void __module_put_and_exit(struct module
*mod
, long code
)
112 EXPORT_SYMBOL(__module_put_and_exit
);
114 /* Find a module section: 0 means not found. */
115 static unsigned int find_sec(Elf_Ehdr
*hdr
,
117 const char *secstrings
,
122 for (i
= 1; i
< hdr
->e_shnum
; i
++)
123 /* Alloc bit cleared means "ignore it." */
124 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
125 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
130 /* Provided by the linker */
131 extern const struct kernel_symbol __start___ksymtab
[];
132 extern const struct kernel_symbol __stop___ksymtab
[];
133 extern const struct kernel_symbol __start___ksymtab_gpl
[];
134 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
135 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
136 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
137 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
138 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
139 extern const unsigned long __start___kcrctab
[];
140 extern const unsigned long __start___kcrctab_gpl
[];
141 extern const unsigned long __start___kcrctab_gpl_future
[];
142 #ifdef CONFIG_UNUSED_SYMBOLS
143 extern const struct kernel_symbol __start___ksymtab_unused
[];
144 extern const struct kernel_symbol __stop___ksymtab_unused
[];
145 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
146 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
147 extern const unsigned long __start___kcrctab_unused
[];
148 extern const unsigned long __start___kcrctab_unused_gpl
[];
151 #ifndef CONFIG_MODVERSIONS
152 #define symversion(base, idx) NULL
154 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
158 const struct kernel_symbol
*start
, *stop
;
159 const unsigned long *crcs
;
168 static bool each_symbol_in_section(const struct symsearch
*arr
,
169 unsigned int arrsize
,
170 struct module
*owner
,
171 bool (*fn
)(const struct symsearch
*syms
,
172 struct module
*owner
,
173 unsigned int symnum
, void *data
),
178 for (j
= 0; j
< arrsize
; j
++) {
179 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
180 if (fn(&arr
[j
], owner
, i
, data
))
187 /* Returns true as soon as fn returns true, otherwise false. */
188 static bool each_symbol(bool (*fn
)(const struct symsearch
*arr
,
189 struct module
*owner
,
190 unsigned int symnum
, void *data
),
194 const struct symsearch arr
[] = {
195 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
196 NOT_GPL_ONLY
, false },
197 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
198 __start___kcrctab_gpl
,
200 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
201 __start___kcrctab_gpl_future
,
202 WILL_BE_GPL_ONLY
, false },
203 #ifdef CONFIG_UNUSED_SYMBOLS
204 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
205 __start___kcrctab_unused
,
206 NOT_GPL_ONLY
, true },
207 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
208 __start___kcrctab_unused_gpl
,
213 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
216 list_for_each_entry(mod
, &modules
, list
) {
217 struct symsearch arr
[] = {
218 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
219 NOT_GPL_ONLY
, false },
220 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
223 { mod
->gpl_future_syms
,
224 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
225 mod
->gpl_future_crcs
,
226 WILL_BE_GPL_ONLY
, false },
227 #ifdef CONFIG_UNUSED_SYMBOLS
229 mod
->unused_syms
+ mod
->num_unused_syms
,
231 NOT_GPL_ONLY
, true },
232 { mod
->unused_gpl_syms
,
233 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
234 mod
->unused_gpl_crcs
,
239 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
245 struct find_symbol_arg
{
252 struct module
*owner
;
253 const unsigned long *crc
;
257 static bool find_symbol_in_section(const struct symsearch
*syms
,
258 struct module
*owner
,
259 unsigned int symnum
, void *data
)
261 struct find_symbol_arg
*fsa
= data
;
263 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
267 if (syms
->licence
== GPL_ONLY
)
269 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
270 printk(KERN_WARNING
"Symbol %s is being used "
271 "by a non-GPL module, which will not "
272 "be allowed in the future\n", fsa
->name
);
273 printk(KERN_WARNING
"Please see the file "
274 "Documentation/feature-removal-schedule.txt "
275 "in the kernel source tree for more details.\n");
279 #ifdef CONFIG_UNUSED_SYMBOLS
280 if (syms
->unused
&& fsa
->warn
) {
281 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
282 "however this module is using it.\n", fsa
->name
);
284 "This symbol will go away in the future.\n");
286 "Please evalute if this is the right api to use and if "
287 "it really is, submit a report the linux kernel "
288 "mailinglist together with submitting your code for "
294 fsa
->crc
= symversion(syms
->crcs
, symnum
);
295 fsa
->value
= syms
->start
[symnum
].value
;
299 /* Find a symbol, return value, (optional) crc and (optional) module
301 static unsigned long find_symbol(const char *name
,
302 struct module
**owner
,
303 const unsigned long **crc
,
307 struct find_symbol_arg fsa
;
313 if (each_symbol(find_symbol_in_section
, &fsa
)) {
321 DEBUGP("Failed to find symbol %s\n", name
);
325 /* lookup symbol in given range of kernel_symbols */
326 static const struct kernel_symbol
*lookup_symbol(const char *name
,
327 const struct kernel_symbol
*start
,
328 const struct kernel_symbol
*stop
)
330 const struct kernel_symbol
*ks
= start
;
331 for (; ks
< stop
; ks
++)
332 if (strcmp(ks
->name
, name
) == 0)
337 /* Search for module by name: must hold module_mutex. */
338 static struct module
*find_module(const char *name
)
342 list_for_each_entry(mod
, &modules
, list
) {
343 if (strcmp(mod
->name
, name
) == 0)
350 /* Number of blocks used and allocated. */
351 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
352 /* Size of each block. -ve means used. */
353 static int *pcpu_size
;
355 static int split_block(unsigned int i
, unsigned short size
)
357 /* Reallocation required? */
358 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
361 new = krealloc(pcpu_size
, sizeof(new[0])*pcpu_num_allocated
*2,
366 pcpu_num_allocated
*= 2;
370 /* Insert a new subblock */
371 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
372 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
375 pcpu_size
[i
+1] -= size
;
380 static inline unsigned int block_size(int val
)
387 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
394 if (align
> PAGE_SIZE
) {
395 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
396 name
, align
, PAGE_SIZE
);
400 ptr
= __per_cpu_start
;
401 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
402 /* Extra for alignment requirement. */
403 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
404 BUG_ON(i
== 0 && extra
!= 0);
406 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
409 /* Transfer extra to previous block. */
410 if (pcpu_size
[i
-1] < 0)
411 pcpu_size
[i
-1] -= extra
;
413 pcpu_size
[i
-1] += extra
;
414 pcpu_size
[i
] -= extra
;
417 /* Split block if warranted */
418 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
419 if (!split_block(i
, size
))
423 pcpu_size
[i
] = -pcpu_size
[i
];
427 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
432 static void percpu_modfree(void *freeme
)
435 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
437 /* First entry is core kernel percpu data. */
438 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
440 pcpu_size
[i
] = -pcpu_size
[i
];
447 /* Merge with previous? */
448 if (pcpu_size
[i
-1] >= 0) {
449 pcpu_size
[i
-1] += pcpu_size
[i
];
451 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
452 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
455 /* Merge with next? */
456 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
457 pcpu_size
[i
] += pcpu_size
[i
+1];
459 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
460 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
464 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
466 const char *secstrings
)
468 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
471 static void percpu_modcopy(void *pcpudest
, const void *from
, unsigned long size
)
475 for_each_possible_cpu(cpu
)
476 memcpy(pcpudest
+ per_cpu_offset(cpu
), from
, size
);
479 static int percpu_modinit(void)
482 pcpu_num_allocated
= 2;
483 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
485 /* Static in-kernel percpu data (used). */
486 pcpu_size
[0] = -(__per_cpu_end
-__per_cpu_start
);
488 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
489 if (pcpu_size
[1] < 0) {
490 printk(KERN_ERR
"No per-cpu room for modules.\n");
496 __initcall(percpu_modinit
);
497 #else /* ... !CONFIG_SMP */
498 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
503 static inline void percpu_modfree(void *pcpuptr
)
507 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
509 const char *secstrings
)
513 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
516 /* pcpusec should be 0, and size of that section should be 0. */
519 #endif /* CONFIG_SMP */
521 #define MODINFO_ATTR(field) \
522 static void setup_modinfo_##field(struct module *mod, const char *s) \
524 mod->field = kstrdup(s, GFP_KERNEL); \
526 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
527 struct module *mod, char *buffer) \
529 return sprintf(buffer, "%s\n", mod->field); \
531 static int modinfo_##field##_exists(struct module *mod) \
533 return mod->field != NULL; \
535 static void free_modinfo_##field(struct module *mod) \
540 static struct module_attribute modinfo_##field = { \
541 .attr = { .name = __stringify(field), .mode = 0444 }, \
542 .show = show_modinfo_##field, \
543 .setup = setup_modinfo_##field, \
544 .test = modinfo_##field##_exists, \
545 .free = free_modinfo_##field, \
548 MODINFO_ATTR(version
);
549 MODINFO_ATTR(srcversion
);
551 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
553 #ifdef CONFIG_MODULE_UNLOAD
554 /* Init the unload section of the module. */
555 static void module_unload_init(struct module
*mod
)
559 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
560 for (i
= 0; i
< NR_CPUS
; i
++)
561 local_set(&mod
->ref
[i
].count
, 0);
562 /* Hold reference count during initialization. */
563 local_set(&mod
->ref
[raw_smp_processor_id()].count
, 1);
564 /* Backwards compatibility macros put refcount during init. */
565 mod
->waiter
= current
;
568 /* modules using other modules */
571 struct list_head list
;
572 struct module
*module_which_uses
;
575 /* Does a already use b? */
576 static int already_uses(struct module
*a
, struct module
*b
)
578 struct module_use
*use
;
580 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
581 if (use
->module_which_uses
== a
) {
582 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
586 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
590 /* Module a uses b */
591 static int use_module(struct module
*a
, struct module
*b
)
593 struct module_use
*use
;
596 if (b
== NULL
|| already_uses(a
, b
)) return 1;
598 /* If we're interrupted or time out, we fail. */
599 if (wait_event_interruptible_timeout(
600 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
602 printk("%s: gave up waiting for init of module %s.\n",
607 /* If strong_try_module_get() returned a different error, we fail. */
611 DEBUGP("Allocating new usage for %s.\n", a
->name
);
612 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
614 printk("%s: out of memory loading\n", a
->name
);
619 use
->module_which_uses
= a
;
620 list_add(&use
->list
, &b
->modules_which_use_me
);
621 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
625 /* Clear the unload stuff of the module. */
626 static void module_unload_free(struct module
*mod
)
630 list_for_each_entry(i
, &modules
, list
) {
631 struct module_use
*use
;
633 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
634 if (use
->module_which_uses
== mod
) {
635 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
637 list_del(&use
->list
);
639 sysfs_remove_link(i
->holders_dir
, mod
->name
);
640 /* There can be at most one match. */
647 #ifdef CONFIG_MODULE_FORCE_UNLOAD
648 static inline int try_force_unload(unsigned int flags
)
650 int ret
= (flags
& O_TRUNC
);
652 add_taint(TAINT_FORCED_RMMOD
);
656 static inline int try_force_unload(unsigned int flags
)
660 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
669 /* Whole machine is stopped with interrupts off when this runs. */
670 static int __try_stop_module(void *_sref
)
672 struct stopref
*sref
= _sref
;
674 /* If it's not unused, quit unless we're forcing. */
675 if (module_refcount(sref
->mod
) != 0) {
676 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
680 /* Mark it as dying. */
681 sref
->mod
->state
= MODULE_STATE_GOING
;
685 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
687 if (flags
& O_NONBLOCK
) {
688 struct stopref sref
= { mod
, flags
, forced
};
690 return stop_machine_run(__try_stop_module
, &sref
, NR_CPUS
);
692 /* We don't need to stop the machine for this. */
693 mod
->state
= MODULE_STATE_GOING
;
699 unsigned int module_refcount(struct module
*mod
)
701 unsigned int i
, total
= 0;
703 for (i
= 0; i
< NR_CPUS
; i
++)
704 total
+= local_read(&mod
->ref
[i
].count
);
707 EXPORT_SYMBOL(module_refcount
);
709 /* This exists whether we can unload or not */
710 static void free_module(struct module
*mod
);
712 static void wait_for_zero_refcount(struct module
*mod
)
714 /* Since we might sleep for some time, release the mutex first */
715 mutex_unlock(&module_mutex
);
717 DEBUGP("Looking at refcount...\n");
718 set_current_state(TASK_UNINTERRUPTIBLE
);
719 if (module_refcount(mod
) == 0)
723 current
->state
= TASK_RUNNING
;
724 mutex_lock(&module_mutex
);
728 sys_delete_module(const char __user
*name_user
, unsigned int flags
)
731 char name
[MODULE_NAME_LEN
];
734 if (!capable(CAP_SYS_MODULE
))
737 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
739 name
[MODULE_NAME_LEN
-1] = '\0';
741 if (mutex_lock_interruptible(&module_mutex
) != 0)
744 mod
= find_module(name
);
750 if (!list_empty(&mod
->modules_which_use_me
)) {
751 /* Other modules depend on us: get rid of them first. */
756 /* Doing init or already dying? */
757 if (mod
->state
!= MODULE_STATE_LIVE
) {
758 /* FIXME: if (force), slam module count and wake up
760 DEBUGP("%s already dying\n", mod
->name
);
765 /* If it has an init func, it must have an exit func to unload */
766 if (mod
->init
&& !mod
->exit
) {
767 forced
= try_force_unload(flags
);
769 /* This module can't be removed */
775 /* Set this up before setting mod->state */
776 mod
->waiter
= current
;
778 /* Stop the machine so refcounts can't move and disable module. */
779 ret
= try_stop_module(mod
, flags
, &forced
);
783 /* Never wait if forced. */
784 if (!forced
&& module_refcount(mod
) != 0)
785 wait_for_zero_refcount(mod
);
787 mutex_unlock(&module_mutex
);
788 /* Final destruction now noone is using it. */
789 if (mod
->exit
!= NULL
)
791 blocking_notifier_call_chain(&module_notify_list
,
792 MODULE_STATE_GOING
, mod
);
793 mutex_lock(&module_mutex
);
794 /* Store the name of the last unloaded module for diagnostic purposes */
795 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
799 mutex_unlock(&module_mutex
);
803 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
805 struct module_use
*use
;
806 int printed_something
= 0;
808 seq_printf(m
, " %u ", module_refcount(mod
));
810 /* Always include a trailing , so userspace can differentiate
811 between this and the old multi-field proc format. */
812 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
813 printed_something
= 1;
814 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
817 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
818 printed_something
= 1;
819 seq_printf(m
, "[permanent],");
822 if (!printed_something
)
826 void __symbol_put(const char *symbol
)
828 struct module
*owner
;
831 if (IS_ERR_VALUE(find_symbol(symbol
, &owner
, NULL
, true, false)))
836 EXPORT_SYMBOL(__symbol_put
);
838 void symbol_put_addr(void *addr
)
840 struct module
*modaddr
;
842 if (core_kernel_text((unsigned long)addr
))
845 if (!(modaddr
= module_text_address((unsigned long)addr
)))
849 EXPORT_SYMBOL_GPL(symbol_put_addr
);
851 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
852 struct module
*mod
, char *buffer
)
854 return sprintf(buffer
, "%u\n", module_refcount(mod
));
857 static struct module_attribute refcnt
= {
858 .attr
= { .name
= "refcnt", .mode
= 0444 },
862 void module_put(struct module
*module
)
865 unsigned int cpu
= get_cpu();
866 local_dec(&module
->ref
[cpu
].count
);
867 /* Maybe they're waiting for us to drop reference? */
868 if (unlikely(!module_is_live(module
)))
869 wake_up_process(module
->waiter
);
873 EXPORT_SYMBOL(module_put
);
875 #else /* !CONFIG_MODULE_UNLOAD */
876 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
878 /* We don't know the usage count, or what modules are using. */
879 seq_printf(m
, " - -");
882 static inline void module_unload_free(struct module
*mod
)
886 static inline int use_module(struct module
*a
, struct module
*b
)
888 return strong_try_module_get(b
) == 0;
891 static inline void module_unload_init(struct module
*mod
)
894 #endif /* CONFIG_MODULE_UNLOAD */
896 static ssize_t
show_initstate(struct module_attribute
*mattr
,
897 struct module
*mod
, char *buffer
)
899 const char *state
= "unknown";
901 switch (mod
->state
) {
902 case MODULE_STATE_LIVE
:
905 case MODULE_STATE_COMING
:
908 case MODULE_STATE_GOING
:
912 return sprintf(buffer
, "%s\n", state
);
915 static struct module_attribute initstate
= {
916 .attr
= { .name
= "initstate", .mode
= 0444 },
917 .show
= show_initstate
,
920 static struct module_attribute
*modinfo_attrs
[] = {
924 #ifdef CONFIG_MODULE_UNLOAD
930 static const char vermagic
[] = VERMAGIC_STRING
;
932 static int try_to_force_load(struct module
*mod
, const char *symname
)
934 #ifdef CONFIG_MODULE_FORCE_LOAD
935 if (!(tainted
& TAINT_FORCED_MODULE
))
936 printk("%s: no version for \"%s\" found: kernel tainted.\n",
938 add_taint_module(mod
, TAINT_FORCED_MODULE
);
945 #ifdef CONFIG_MODVERSIONS
946 static int check_version(Elf_Shdr
*sechdrs
,
947 unsigned int versindex
,
950 const unsigned long *crc
)
952 unsigned int i
, num_versions
;
953 struct modversion_info
*versions
;
955 /* Exporting module didn't supply crcs? OK, we're already tainted. */
959 /* No versions at all? modprobe --force does this. */
961 return try_to_force_load(mod
, symname
) == 0;
963 versions
= (void *) sechdrs
[versindex
].sh_addr
;
964 num_versions
= sechdrs
[versindex
].sh_size
965 / sizeof(struct modversion_info
);
967 for (i
= 0; i
< num_versions
; i
++) {
968 if (strcmp(versions
[i
].name
, symname
) != 0)
971 if (versions
[i
].crc
== *crc
)
973 DEBUGP("Found checksum %lX vs module %lX\n",
974 *crc
, versions
[i
].crc
);
978 printk(KERN_WARNING
"%s: no symbol version for %s\n",
983 printk("%s: disagrees about version of symbol %s\n",
988 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
989 unsigned int versindex
,
992 const unsigned long *crc
;
994 if (IS_ERR_VALUE(find_symbol("struct_module", NULL
, &crc
, true, false)))
996 return check_version(sechdrs
, versindex
, "struct_module", mod
, crc
);
999 /* First part is kernel version, which we ignore if module has crcs. */
1000 static inline int same_magic(const char *amagic
, const char *bmagic
,
1004 amagic
+= strcspn(amagic
, " ");
1005 bmagic
+= strcspn(bmagic
, " ");
1007 return strcmp(amagic
, bmagic
) == 0;
1010 static inline int check_version(Elf_Shdr
*sechdrs
,
1011 unsigned int versindex
,
1012 const char *symname
,
1014 const unsigned long *crc
)
1019 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1020 unsigned int versindex
,
1026 static inline int same_magic(const char *amagic
, const char *bmagic
,
1029 return strcmp(amagic
, bmagic
) == 0;
1031 #endif /* CONFIG_MODVERSIONS */
1033 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1034 Must be holding module_mutex. */
1035 static unsigned long resolve_symbol(Elf_Shdr
*sechdrs
,
1036 unsigned int versindex
,
1040 struct module
*owner
;
1042 const unsigned long *crc
;
1044 ret
= find_symbol(name
, &owner
, &crc
,
1045 !(mod
->taints
& TAINT_PROPRIETARY_MODULE
), true);
1046 if (!IS_ERR_VALUE(ret
)) {
1047 /* use_module can fail due to OOM,
1048 or module initialization or unloading */
1049 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1050 !use_module(mod
, owner
))
1057 * /sys/module/foo/sections stuff
1058 * J. Corbet <corbet@lwn.net>
1060 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1061 struct module_sect_attr
1063 struct module_attribute mattr
;
1065 unsigned long address
;
1068 struct module_sect_attrs
1070 struct attribute_group grp
;
1071 unsigned int nsections
;
1072 struct module_sect_attr attrs
[0];
1075 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1076 struct module
*mod
, char *buf
)
1078 struct module_sect_attr
*sattr
=
1079 container_of(mattr
, struct module_sect_attr
, mattr
);
1080 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1083 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1085 unsigned int section
;
1087 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1088 kfree(sect_attrs
->attrs
[section
].name
);
1092 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1093 char *secstrings
, Elf_Shdr
*sechdrs
)
1095 unsigned int nloaded
= 0, i
, size
[2];
1096 struct module_sect_attrs
*sect_attrs
;
1097 struct module_sect_attr
*sattr
;
1098 struct attribute
**gattr
;
1100 /* Count loaded sections and allocate structures */
1101 for (i
= 0; i
< nsect
; i
++)
1102 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
1104 size
[0] = ALIGN(sizeof(*sect_attrs
)
1105 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1106 sizeof(sect_attrs
->grp
.attrs
[0]));
1107 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1108 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1109 if (sect_attrs
== NULL
)
1112 /* Setup section attributes. */
1113 sect_attrs
->grp
.name
= "sections";
1114 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1116 sect_attrs
->nsections
= 0;
1117 sattr
= §_attrs
->attrs
[0];
1118 gattr
= §_attrs
->grp
.attrs
[0];
1119 for (i
= 0; i
< nsect
; i
++) {
1120 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1122 sattr
->address
= sechdrs
[i
].sh_addr
;
1123 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1125 if (sattr
->name
== NULL
)
1127 sect_attrs
->nsections
++;
1128 sattr
->mattr
.show
= module_sect_show
;
1129 sattr
->mattr
.store
= NULL
;
1130 sattr
->mattr
.attr
.name
= sattr
->name
;
1131 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1132 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1136 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1139 mod
->sect_attrs
= sect_attrs
;
1142 free_sect_attrs(sect_attrs
);
1145 static void remove_sect_attrs(struct module
*mod
)
1147 if (mod
->sect_attrs
) {
1148 sysfs_remove_group(&mod
->mkobj
.kobj
,
1149 &mod
->sect_attrs
->grp
);
1150 /* We are positive that no one is using any sect attrs
1151 * at this point. Deallocate immediately. */
1152 free_sect_attrs(mod
->sect_attrs
);
1153 mod
->sect_attrs
= NULL
;
1158 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1161 struct module_notes_attrs
{
1162 struct kobject
*dir
;
1164 struct bin_attribute attrs
[0];
1167 static ssize_t
module_notes_read(struct kobject
*kobj
,
1168 struct bin_attribute
*bin_attr
,
1169 char *buf
, loff_t pos
, size_t count
)
1172 * The caller checked the pos and count against our size.
1174 memcpy(buf
, bin_attr
->private + pos
, count
);
1178 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1181 if (notes_attrs
->dir
) {
1183 sysfs_remove_bin_file(notes_attrs
->dir
,
1184 ¬es_attrs
->attrs
[i
]);
1185 kobject_del(notes_attrs
->dir
);
1190 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1191 char *secstrings
, Elf_Shdr
*sechdrs
)
1193 unsigned int notes
, loaded
, i
;
1194 struct module_notes_attrs
*notes_attrs
;
1195 struct bin_attribute
*nattr
;
1197 /* Count notes sections and allocate structures. */
1199 for (i
= 0; i
< nsect
; i
++)
1200 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
) &&
1201 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1207 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1208 + notes
* sizeof(notes_attrs
->attrs
[0]),
1210 if (notes_attrs
== NULL
)
1213 notes_attrs
->notes
= notes
;
1214 nattr
= ¬es_attrs
->attrs
[0];
1215 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1216 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1218 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1219 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1220 nattr
->attr
.mode
= S_IRUGO
;
1221 nattr
->size
= sechdrs
[i
].sh_size
;
1222 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1223 nattr
->read
= module_notes_read
;
1229 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1230 if (!notes_attrs
->dir
)
1233 for (i
= 0; i
< notes
; ++i
)
1234 if (sysfs_create_bin_file(notes_attrs
->dir
,
1235 ¬es_attrs
->attrs
[i
]))
1238 mod
->notes_attrs
= notes_attrs
;
1242 free_notes_attrs(notes_attrs
, i
);
1245 static void remove_notes_attrs(struct module
*mod
)
1247 if (mod
->notes_attrs
)
1248 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1253 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1254 char *sectstrings
, Elf_Shdr
*sechdrs
)
1258 static inline void remove_sect_attrs(struct module
*mod
)
1262 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1263 char *sectstrings
, Elf_Shdr
*sechdrs
)
1267 static inline void remove_notes_attrs(struct module
*mod
)
1273 int module_add_modinfo_attrs(struct module
*mod
)
1275 struct module_attribute
*attr
;
1276 struct module_attribute
*temp_attr
;
1280 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1281 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1283 if (!mod
->modinfo_attrs
)
1286 temp_attr
= mod
->modinfo_attrs
;
1287 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1289 (attr
->test
&& attr
->test(mod
))) {
1290 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1291 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1298 void module_remove_modinfo_attrs(struct module
*mod
)
1300 struct module_attribute
*attr
;
1303 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1304 /* pick a field to test for end of list */
1305 if (!attr
->attr
.name
)
1307 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1311 kfree(mod
->modinfo_attrs
);
1314 int mod_sysfs_init(struct module
*mod
)
1317 struct kobject
*kobj
;
1319 if (!module_sysfs_initialized
) {
1320 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1326 kobj
= kset_find_obj(module_kset
, mod
->name
);
1328 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1334 mod
->mkobj
.mod
= mod
;
1336 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1337 mod
->mkobj
.kobj
.kset
= module_kset
;
1338 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1341 kobject_put(&mod
->mkobj
.kobj
);
1343 /* delay uevent until full sysfs population */
1348 int mod_sysfs_setup(struct module
*mod
,
1349 struct kernel_param
*kparam
,
1350 unsigned int num_params
)
1354 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1355 if (!mod
->holders_dir
) {
1360 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1362 goto out_unreg_holders
;
1364 err
= module_add_modinfo_attrs(mod
);
1366 goto out_unreg_param
;
1368 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1372 module_param_sysfs_remove(mod
);
1374 kobject_put(mod
->holders_dir
);
1376 kobject_put(&mod
->mkobj
.kobj
);
1380 static void mod_sysfs_fini(struct module
*mod
)
1382 kobject_put(&mod
->mkobj
.kobj
);
1385 #else /* CONFIG_SYSFS */
1387 static void mod_sysfs_fini(struct module
*mod
)
1391 #endif /* CONFIG_SYSFS */
1393 static void mod_kobject_remove(struct module
*mod
)
1395 module_remove_modinfo_attrs(mod
);
1396 module_param_sysfs_remove(mod
);
1397 kobject_put(mod
->mkobj
.drivers_dir
);
1398 kobject_put(mod
->holders_dir
);
1399 mod_sysfs_fini(mod
);
1403 * link the module with the whole machine is stopped with interrupts off
1404 * - this defends against kallsyms not taking locks
1406 static int __link_module(void *_mod
)
1408 struct module
*mod
= _mod
;
1409 list_add(&mod
->list
, &modules
);
1414 * unlink the module with the whole machine is stopped with interrupts off
1415 * - this defends against kallsyms not taking locks
1417 static int __unlink_module(void *_mod
)
1419 struct module
*mod
= _mod
;
1420 list_del(&mod
->list
);
1424 /* Free a module, remove from lists, etc (must hold module_mutex). */
1425 static void free_module(struct module
*mod
)
1427 /* Delete from various lists */
1428 stop_machine_run(__unlink_module
, mod
, NR_CPUS
);
1429 remove_notes_attrs(mod
);
1430 remove_sect_attrs(mod
);
1431 mod_kobject_remove(mod
);
1433 unwind_remove_table(mod
->unwind_info
, 0);
1435 /* Arch-specific cleanup. */
1436 module_arch_cleanup(mod
);
1438 /* Module unload stuff */
1439 module_unload_free(mod
);
1441 /* This may be NULL, but that's OK */
1442 module_free(mod
, mod
->module_init
);
1445 percpu_modfree(mod
->percpu
);
1447 /* Free lock-classes: */
1448 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1450 /* Finally, free the core (containing the module structure) */
1451 module_free(mod
, mod
->module_core
);
1454 void *__symbol_get(const char *symbol
)
1456 struct module
*owner
;
1457 unsigned long value
;
1460 value
= find_symbol(symbol
, &owner
, NULL
, true, true);
1461 if (IS_ERR_VALUE(value
))
1463 else if (strong_try_module_get(owner
))
1467 return (void *)value
;
1469 EXPORT_SYMBOL_GPL(__symbol_get
);
1472 * Ensure that an exported symbol [global namespace] does not already exist
1473 * in the kernel or in some other module's exported symbol table.
1475 static int verify_export_symbols(struct module
*mod
)
1478 struct module
*owner
;
1479 const struct kernel_symbol
*s
;
1481 const struct kernel_symbol
*sym
;
1484 { mod
->syms
, mod
->num_syms
},
1485 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1486 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1487 #ifdef CONFIG_UNUSED_SYMBOLS
1488 { mod
->unused_syms
, mod
->num_unused_syms
},
1489 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1493 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1494 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1495 if (!IS_ERR_VALUE(find_symbol(s
->name
, &owner
,
1496 NULL
, true, false))) {
1498 "%s: exports duplicate symbol %s"
1500 mod
->name
, s
->name
, module_name(owner
));
1508 /* Change all symbols so that st_value encodes the pointer directly. */
1509 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1510 unsigned int symindex
,
1512 unsigned int versindex
,
1513 unsigned int pcpuindex
,
1516 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1517 unsigned long secbase
;
1518 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1521 for (i
= 1; i
< n
; i
++) {
1522 switch (sym
[i
].st_shndx
) {
1524 /* We compiled with -fno-common. These are not
1525 supposed to happen. */
1526 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1527 printk("%s: please compile with -fno-common\n",
1533 /* Don't need to do anything */
1534 DEBUGP("Absolute symbol: 0x%08lx\n",
1535 (long)sym
[i
].st_value
);
1540 = resolve_symbol(sechdrs
, versindex
,
1541 strtab
+ sym
[i
].st_name
, mod
);
1543 /* Ok if resolved. */
1544 if (!IS_ERR_VALUE(sym
[i
].st_value
))
1547 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1550 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1551 mod
->name
, strtab
+ sym
[i
].st_name
);
1556 /* Divert to percpu allocation if a percpu var. */
1557 if (sym
[i
].st_shndx
== pcpuindex
)
1558 secbase
= (unsigned long)mod
->percpu
;
1560 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1561 sym
[i
].st_value
+= secbase
;
1569 /* Update size with this section: return offset. */
1570 static long get_offset(unsigned int *size
, Elf_Shdr
*sechdr
)
1574 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1575 *size
= ret
+ sechdr
->sh_size
;
1579 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1580 might -- code, read-only data, read-write data, small data. Tally
1581 sizes, and place the offsets into sh_entsize fields: high bit means it
1583 static void layout_sections(struct module
*mod
,
1584 const Elf_Ehdr
*hdr
,
1586 const char *secstrings
)
1588 static unsigned long const masks
[][2] = {
1589 /* NOTE: all executable code must be the first section
1590 * in this array; otherwise modify the text_size
1591 * finder in the two loops below */
1592 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1593 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1594 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1595 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1599 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1600 sechdrs
[i
].sh_entsize
= ~0UL;
1602 DEBUGP("Core section allocation order:\n");
1603 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1604 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1605 Elf_Shdr
*s
= &sechdrs
[i
];
1607 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1608 || (s
->sh_flags
& masks
[m
][1])
1609 || s
->sh_entsize
!= ~0UL
1610 || strncmp(secstrings
+ s
->sh_name
,
1613 s
->sh_entsize
= get_offset(&mod
->core_size
, s
);
1614 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1617 mod
->core_text_size
= mod
->core_size
;
1620 DEBUGP("Init section allocation order:\n");
1621 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1622 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1623 Elf_Shdr
*s
= &sechdrs
[i
];
1625 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1626 || (s
->sh_flags
& masks
[m
][1])
1627 || s
->sh_entsize
!= ~0UL
1628 || strncmp(secstrings
+ s
->sh_name
,
1631 s
->sh_entsize
= (get_offset(&mod
->init_size
, s
)
1632 | INIT_OFFSET_MASK
);
1633 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1636 mod
->init_text_size
= mod
->init_size
;
1640 static void set_license(struct module
*mod
, const char *license
)
1643 license
= "unspecified";
1645 if (!license_is_gpl_compatible(license
)) {
1646 if (!(tainted
& TAINT_PROPRIETARY_MODULE
))
1647 printk(KERN_WARNING
"%s: module license '%s' taints "
1648 "kernel.\n", mod
->name
, license
);
1649 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1653 /* Parse tag=value strings from .modinfo section */
1654 static char *next_string(char *string
, unsigned long *secsize
)
1656 /* Skip non-zero chars */
1659 if ((*secsize
)-- <= 1)
1663 /* Skip any zero padding. */
1664 while (!string
[0]) {
1666 if ((*secsize
)-- <= 1)
1672 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1677 unsigned int taglen
= strlen(tag
);
1678 unsigned long size
= sechdrs
[info
].sh_size
;
1680 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1681 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1682 return p
+ taglen
+ 1;
1687 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1688 unsigned int infoindex
)
1690 struct module_attribute
*attr
;
1693 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1696 get_modinfo(sechdrs
,
1702 #ifdef CONFIG_KALLSYMS
1703 static int is_exported(const char *name
, const struct module
*mod
)
1705 if (!mod
&& lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
))
1708 if (mod
&& lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
))
1715 static char elf_type(const Elf_Sym
*sym
,
1717 const char *secstrings
,
1720 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1721 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1726 if (sym
->st_shndx
== SHN_UNDEF
)
1728 if (sym
->st_shndx
== SHN_ABS
)
1730 if (sym
->st_shndx
>= SHN_LORESERVE
)
1732 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1734 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1735 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1736 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1738 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1743 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1744 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1749 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1750 ".debug", strlen(".debug")) == 0)
1755 static void add_kallsyms(struct module
*mod
,
1757 unsigned int symindex
,
1758 unsigned int strindex
,
1759 const char *secstrings
)
1763 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1764 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1765 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1767 /* Set types up while we still have access to sections. */
1768 for (i
= 0; i
< mod
->num_symtab
; i
++)
1769 mod
->symtab
[i
].st_info
1770 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1773 static inline void add_kallsyms(struct module
*mod
,
1775 unsigned int symindex
,
1776 unsigned int strindex
,
1777 const char *secstrings
)
1780 #endif /* CONFIG_KALLSYMS */
1782 /* Allocate and load the module: note that size of section 0 is always
1783 zero, and we rely on this for optional sections. */
1784 static struct module
*load_module(void __user
*umod
,
1786 const char __user
*uargs
)
1790 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1792 unsigned int symindex
= 0;
1793 unsigned int strindex
= 0;
1794 unsigned int setupindex
;
1795 unsigned int exindex
;
1796 unsigned int exportindex
;
1797 unsigned int modindex
;
1798 unsigned int obsparmindex
;
1799 unsigned int infoindex
;
1800 unsigned int gplindex
;
1801 unsigned int crcindex
;
1802 unsigned int gplcrcindex
;
1803 unsigned int versindex
;
1804 unsigned int pcpuindex
;
1805 unsigned int gplfutureindex
;
1806 unsigned int gplfuturecrcindex
;
1807 unsigned int unwindex
= 0;
1808 #ifdef CONFIG_UNUSED_SYMBOLS
1809 unsigned int unusedindex
;
1810 unsigned int unusedcrcindex
;
1811 unsigned int unusedgplindex
;
1812 unsigned int unusedgplcrcindex
;
1814 unsigned int markersindex
;
1815 unsigned int markersstringsindex
;
1818 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1819 struct exception_table_entry
*extable
;
1820 mm_segment_t old_fs
;
1822 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1824 if (len
< sizeof(*hdr
))
1825 return ERR_PTR(-ENOEXEC
);
1827 /* Suck in entire file: we'll want most of it. */
1828 /* vmalloc barfs on "unusual" numbers. Check here */
1829 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1830 return ERR_PTR(-ENOMEM
);
1831 if (copy_from_user(hdr
, umod
, len
) != 0) {
1836 /* Sanity checks against insmoding binaries or wrong arch,
1837 weird elf version */
1838 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
1839 || hdr
->e_type
!= ET_REL
1840 || !elf_check_arch(hdr
)
1841 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1846 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1849 /* Convenience variables */
1850 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1851 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1852 sechdrs
[0].sh_addr
= 0;
1854 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1855 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1856 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1859 /* Mark all sections sh_addr with their address in the
1861 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1863 /* Internal symbols and strings. */
1864 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1866 strindex
= sechdrs
[i
].sh_link
;
1867 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1869 #ifndef CONFIG_MODULE_UNLOAD
1870 /* Don't load .exit sections */
1871 if (strncmp(secstrings
+sechdrs
[i
].sh_name
, ".exit", 5) == 0)
1872 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1876 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1877 ".gnu.linkonce.this_module");
1879 printk(KERN_WARNING
"No module found in object\n");
1883 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1885 if (symindex
== 0) {
1886 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1892 /* Optional sections */
1893 exportindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab");
1894 gplindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl");
1895 gplfutureindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl_future");
1896 crcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab");
1897 gplcrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
1898 gplfuturecrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl_future");
1899 #ifdef CONFIG_UNUSED_SYMBOLS
1900 unusedindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_unused");
1901 unusedgplindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_unused_gpl");
1902 unusedcrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_unused");
1903 unusedgplcrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_unused_gpl");
1905 setupindex
= find_sec(hdr
, sechdrs
, secstrings
, "__param");
1906 exindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ex_table");
1907 obsparmindex
= find_sec(hdr
, sechdrs
, secstrings
, "__obsparm");
1908 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1909 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1910 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1911 #ifdef ARCH_UNWIND_SECTION_NAME
1912 unwindex
= find_sec(hdr
, sechdrs
, secstrings
, ARCH_UNWIND_SECTION_NAME
);
1915 /* Don't keep modinfo and version sections. */
1916 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1917 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1918 #ifdef CONFIG_KALLSYMS
1919 /* Keep symbol and string tables for decoding later. */
1920 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1921 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1924 sechdrs
[unwindex
].sh_flags
|= SHF_ALLOC
;
1926 /* Check module struct version now, before we try to use module. */
1927 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1932 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1933 /* This is allowed: modprobe --force will invalidate it. */
1935 err
= try_to_force_load(mod
, "magic");
1938 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
1939 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
1940 mod
->name
, modmagic
, vermagic
);
1945 /* Now copy in args */
1946 args
= strndup_user(uargs
, ~0UL >> 1);
1948 err
= PTR_ERR(args
);
1952 if (find_module(mod
->name
)) {
1957 mod
->state
= MODULE_STATE_COMING
;
1959 /* Allow arches to frob section contents and sizes. */
1960 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
1965 /* We have a special allocation for this section. */
1966 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
1967 sechdrs
[pcpuindex
].sh_addralign
,
1973 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1974 mod
->percpu
= percpu
;
1977 /* Determine total sizes, and put offsets in sh_entsize. For now
1978 this is done generically; there doesn't appear to be any
1979 special cases for the architectures. */
1980 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
1982 /* Do the allocs. */
1983 ptr
= module_alloc(mod
->core_size
);
1988 memset(ptr
, 0, mod
->core_size
);
1989 mod
->module_core
= ptr
;
1991 ptr
= module_alloc(mod
->init_size
);
1992 if (!ptr
&& mod
->init_size
) {
1996 memset(ptr
, 0, mod
->init_size
);
1997 mod
->module_init
= ptr
;
1999 /* Transfer each section which specifies SHF_ALLOC */
2000 DEBUGP("final section addresses:\n");
2001 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2004 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2007 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2008 dest
= mod
->module_init
2009 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2011 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2013 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2014 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2015 sechdrs
[i
].sh_size
);
2016 /* Update sh_addr to point to copy in image. */
2017 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2018 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2020 /* Module has been moved. */
2021 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2023 /* Now we've moved module, initialize linked lists, etc. */
2024 module_unload_init(mod
);
2026 /* add kobject, so we can reference it. */
2027 err
= mod_sysfs_init(mod
);
2031 /* Set up license info based on the info section */
2032 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2035 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2036 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2037 * using GPL-only symbols it needs.
2039 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2040 add_taint(TAINT_PROPRIETARY_MODULE
);
2042 /* driverloader was caught wrongly pretending to be under GPL */
2043 if (strcmp(mod
->name
, "driverloader") == 0)
2044 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2046 /* Set up MODINFO_ATTR fields */
2047 setup_modinfo(mod
, sechdrs
, infoindex
);
2049 /* Fix up syms, so that st_value is a pointer to location. */
2050 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2055 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
2056 mod
->num_syms
= sechdrs
[exportindex
].sh_size
/ sizeof(*mod
->syms
);
2057 mod
->syms
= (void *)sechdrs
[exportindex
].sh_addr
;
2059 mod
->crcs
= (void *)sechdrs
[crcindex
].sh_addr
;
2060 mod
->num_gpl_syms
= sechdrs
[gplindex
].sh_size
/ sizeof(*mod
->gpl_syms
);
2061 mod
->gpl_syms
= (void *)sechdrs
[gplindex
].sh_addr
;
2063 mod
->gpl_crcs
= (void *)sechdrs
[gplcrcindex
].sh_addr
;
2064 mod
->num_gpl_future_syms
= sechdrs
[gplfutureindex
].sh_size
/
2065 sizeof(*mod
->gpl_future_syms
);
2066 mod
->gpl_future_syms
= (void *)sechdrs
[gplfutureindex
].sh_addr
;
2067 if (gplfuturecrcindex
)
2068 mod
->gpl_future_crcs
= (void *)sechdrs
[gplfuturecrcindex
].sh_addr
;
2070 #ifdef CONFIG_UNUSED_SYMBOLS
2071 mod
->num_unused_syms
= sechdrs
[unusedindex
].sh_size
/
2072 sizeof(*mod
->unused_syms
);
2073 mod
->num_unused_gpl_syms
= sechdrs
[unusedgplindex
].sh_size
/
2074 sizeof(*mod
->unused_gpl_syms
);
2075 mod
->unused_syms
= (void *)sechdrs
[unusedindex
].sh_addr
;
2077 mod
->unused_crcs
= (void *)sechdrs
[unusedcrcindex
].sh_addr
;
2078 mod
->unused_gpl_syms
= (void *)sechdrs
[unusedgplindex
].sh_addr
;
2079 if (unusedgplcrcindex
)
2080 mod
->unused_gpl_crcs
2081 = (void *)sechdrs
[unusedgplcrcindex
].sh_addr
;
2084 #ifdef CONFIG_MODVERSIONS
2085 if ((mod
->num_syms
&& !crcindex
)
2086 || (mod
->num_gpl_syms
&& !gplcrcindex
)
2087 || (mod
->num_gpl_future_syms
&& !gplfuturecrcindex
)
2088 #ifdef CONFIG_UNUSED_SYMBOLS
2089 || (mod
->num_unused_syms
&& !unusedcrcindex
)
2090 || (mod
->num_unused_gpl_syms
&& !unusedgplcrcindex
)
2093 printk(KERN_WARNING
"%s: No versions for exported symbols.\n", mod
->name
);
2094 err
= try_to_force_load(mod
, "nocrc");
2099 markersindex
= find_sec(hdr
, sechdrs
, secstrings
, "__markers");
2100 markersstringsindex
= find_sec(hdr
, sechdrs
, secstrings
,
2101 "__markers_strings");
2103 /* Now do relocations. */
2104 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2105 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2106 unsigned int info
= sechdrs
[i
].sh_info
;
2108 /* Not a valid relocation section? */
2109 if (info
>= hdr
->e_shnum
)
2112 /* Don't bother with non-allocated sections */
2113 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2116 if (sechdrs
[i
].sh_type
== SHT_REL
)
2117 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2118 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2119 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2124 #ifdef CONFIG_MARKERS
2125 mod
->markers
= (void *)sechdrs
[markersindex
].sh_addr
;
2127 sechdrs
[markersindex
].sh_size
/ sizeof(*mod
->markers
);
2130 /* Find duplicate symbols */
2131 err
= verify_export_symbols(mod
);
2136 /* Set up and sort exception table */
2137 mod
->num_exentries
= sechdrs
[exindex
].sh_size
/ sizeof(*mod
->extable
);
2138 mod
->extable
= extable
= (void *)sechdrs
[exindex
].sh_addr
;
2139 sort_extable(extable
, extable
+ mod
->num_exentries
);
2141 /* Finally, copy percpu area over. */
2142 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2143 sechdrs
[pcpuindex
].sh_size
);
2145 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
2147 #ifdef CONFIG_MARKERS
2149 marker_update_probe_range(mod
->markers
,
2150 mod
->markers
+ mod
->num_markers
);
2152 err
= module_finalize(hdr
, sechdrs
, mod
);
2156 /* flush the icache in correct context */
2161 * Flush the instruction cache, since we've played with text.
2162 * Do it before processing of module parameters, so the module
2163 * can provide parameter accessor functions of its own.
2165 if (mod
->module_init
)
2166 flush_icache_range((unsigned long)mod
->module_init
,
2167 (unsigned long)mod
->module_init
2169 flush_icache_range((unsigned long)mod
->module_core
,
2170 (unsigned long)mod
->module_core
+ mod
->core_size
);
2176 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2179 /* Now sew it into the lists so we can get lockdep and oops
2180 * info during argument parsing. Noone should access us, since
2181 * strong_try_module_get() will fail. */
2182 stop_machine_run(__link_module
, mod
, NR_CPUS
);
2184 /* Size of section 0 is 0, so this works well if no params */
2185 err
= parse_args(mod
->name
, mod
->args
,
2186 (struct kernel_param
*)
2187 sechdrs
[setupindex
].sh_addr
,
2188 sechdrs
[setupindex
].sh_size
2189 / sizeof(struct kernel_param
),
2194 err
= mod_sysfs_setup(mod
,
2195 (struct kernel_param
*)
2196 sechdrs
[setupindex
].sh_addr
,
2197 sechdrs
[setupindex
].sh_size
2198 / sizeof(struct kernel_param
));
2201 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2202 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2204 /* Size of section 0 is 0, so this works well if no unwind info. */
2205 mod
->unwind_info
= unwind_add_table(mod
,
2206 (void *)sechdrs
[unwindex
].sh_addr
,
2207 sechdrs
[unwindex
].sh_size
);
2209 /* Get rid of temporary copy */
2216 stop_machine_run(__unlink_module
, mod
, NR_CPUS
);
2217 module_arch_cleanup(mod
);
2219 kobject_del(&mod
->mkobj
.kobj
);
2220 kobject_put(&mod
->mkobj
.kobj
);
2222 module_unload_free(mod
);
2223 module_free(mod
, mod
->module_init
);
2225 module_free(mod
, mod
->module_core
);
2228 percpu_modfree(percpu
);
2233 return ERR_PTR(err
);
2236 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2241 /* This is where the real work happens */
2243 sys_init_module(void __user
*umod
,
2245 const char __user
*uargs
)
2250 /* Must have permission */
2251 if (!capable(CAP_SYS_MODULE
))
2254 /* Only one module load at a time, please */
2255 if (mutex_lock_interruptible(&module_mutex
) != 0)
2258 /* Do all the hard work */
2259 mod
= load_module(umod
, len
, uargs
);
2261 mutex_unlock(&module_mutex
);
2262 return PTR_ERR(mod
);
2265 /* Drop lock so they can recurse */
2266 mutex_unlock(&module_mutex
);
2268 blocking_notifier_call_chain(&module_notify_list
,
2269 MODULE_STATE_COMING
, mod
);
2271 /* Start the module */
2272 if (mod
->init
!= NULL
)
2275 /* Init routine failed: abort. Try to protect us from
2276 buggy refcounters. */
2277 mod
->state
= MODULE_STATE_GOING
;
2278 synchronize_sched();
2280 blocking_notifier_call_chain(&module_notify_list
,
2281 MODULE_STATE_GOING
, mod
);
2282 mutex_lock(&module_mutex
);
2284 mutex_unlock(&module_mutex
);
2285 wake_up(&module_wq
);
2289 printk(KERN_WARNING
"%s: '%s'->init suspiciously returned %d, "
2290 "it should follow 0/-E convention\n"
2291 KERN_WARNING
"%s: loading module anyway...\n",
2292 __func__
, mod
->name
, ret
,
2297 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2298 mod
->state
= MODULE_STATE_LIVE
;
2299 wake_up(&module_wq
);
2301 mutex_lock(&module_mutex
);
2302 /* Drop initial reference. */
2304 unwind_remove_table(mod
->unwind_info
, 1);
2305 module_free(mod
, mod
->module_init
);
2306 mod
->module_init
= NULL
;
2308 mod
->init_text_size
= 0;
2309 mutex_unlock(&module_mutex
);
2314 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2316 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2319 #ifdef CONFIG_KALLSYMS
2321 * This ignores the intensely annoying "mapping symbols" found
2322 * in ARM ELF files: $a, $t and $d.
2324 static inline int is_arm_mapping_symbol(const char *str
)
2326 return str
[0] == '$' && strchr("atd", str
[1])
2327 && (str
[2] == '\0' || str
[2] == '.');
2330 static const char *get_ksymbol(struct module
*mod
,
2332 unsigned long *size
,
2333 unsigned long *offset
)
2335 unsigned int i
, best
= 0;
2336 unsigned long nextval
;
2338 /* At worse, next value is at end of module */
2339 if (within(addr
, mod
->module_init
, mod
->init_size
))
2340 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2342 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2344 /* Scan for closest preceeding symbol, and next symbol. (ELF
2345 starts real symbols at 1). */
2346 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2347 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2350 /* We ignore unnamed symbols: they're uninformative
2351 * and inserted at a whim. */
2352 if (mod
->symtab
[i
].st_value
<= addr
2353 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2354 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2355 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2357 if (mod
->symtab
[i
].st_value
> addr
2358 && mod
->symtab
[i
].st_value
< nextval
2359 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2360 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2361 nextval
= mod
->symtab
[i
].st_value
;
2368 *size
= nextval
- mod
->symtab
[best
].st_value
;
2370 *offset
= addr
- mod
->symtab
[best
].st_value
;
2371 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2374 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2375 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2376 const char *module_address_lookup(unsigned long addr
,
2377 unsigned long *size
,
2378 unsigned long *offset
,
2383 const char *ret
= NULL
;
2386 list_for_each_entry(mod
, &modules
, list
) {
2387 if (within(addr
, mod
->module_init
, mod
->init_size
)
2388 || within(addr
, mod
->module_core
, mod
->core_size
)) {
2390 *modname
= mod
->name
;
2391 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2395 /* Make a copy in here where it's safe */
2397 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2404 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2409 list_for_each_entry(mod
, &modules
, list
) {
2410 if (within(addr
, mod
->module_init
, mod
->init_size
) ||
2411 within(addr
, mod
->module_core
, mod
->core_size
)) {
2414 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2417 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2427 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2428 unsigned long *offset
, char *modname
, char *name
)
2433 list_for_each_entry(mod
, &modules
, list
) {
2434 if (within(addr
, mod
->module_init
, mod
->init_size
) ||
2435 within(addr
, mod
->module_core
, mod
->core_size
)) {
2438 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2442 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2444 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2454 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2455 char *name
, char *module_name
, int *exported
)
2460 list_for_each_entry(mod
, &modules
, list
) {
2461 if (symnum
< mod
->num_symtab
) {
2462 *value
= mod
->symtab
[symnum
].st_value
;
2463 *type
= mod
->symtab
[symnum
].st_info
;
2464 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2466 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2467 *exported
= is_exported(name
, mod
);
2471 symnum
-= mod
->num_symtab
;
2477 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2481 for (i
= 0; i
< mod
->num_symtab
; i
++)
2482 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2483 mod
->symtab
[i
].st_info
!= 'U')
2484 return mod
->symtab
[i
].st_value
;
2488 /* Look for this name: can be of form module:name. */
2489 unsigned long module_kallsyms_lookup_name(const char *name
)
2493 unsigned long ret
= 0;
2495 /* Don't lock: we're in enough trouble already. */
2497 if ((colon
= strchr(name
, ':')) != NULL
) {
2499 if ((mod
= find_module(name
)) != NULL
)
2500 ret
= mod_find_symname(mod
, colon
+1);
2503 list_for_each_entry(mod
, &modules
, list
)
2504 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2510 #endif /* CONFIG_KALLSYMS */
2512 /* Called by the /proc file system to return a list of modules. */
2513 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2515 mutex_lock(&module_mutex
);
2516 return seq_list_start(&modules
, *pos
);
2519 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2521 return seq_list_next(p
, &modules
, pos
);
2524 static void m_stop(struct seq_file
*m
, void *p
)
2526 mutex_unlock(&module_mutex
);
2529 static char *module_flags(struct module
*mod
, char *buf
)
2534 mod
->state
== MODULE_STATE_GOING
||
2535 mod
->state
== MODULE_STATE_COMING
) {
2537 if (mod
->taints
& TAINT_PROPRIETARY_MODULE
)
2539 if (mod
->taints
& TAINT_FORCED_MODULE
)
2542 * TAINT_FORCED_RMMOD: could be added.
2543 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2547 /* Show a - for module-is-being-unloaded */
2548 if (mod
->state
== MODULE_STATE_GOING
)
2550 /* Show a + for module-is-being-loaded */
2551 if (mod
->state
== MODULE_STATE_COMING
)
2560 static int m_show(struct seq_file
*m
, void *p
)
2562 struct module
*mod
= list_entry(p
, struct module
, list
);
2565 seq_printf(m
, "%s %u",
2566 mod
->name
, mod
->init_size
+ mod
->core_size
);
2567 print_unload_info(m
, mod
);
2569 /* Informative for users. */
2570 seq_printf(m
, " %s",
2571 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2572 mod
->state
== MODULE_STATE_COMING
? "Loading":
2574 /* Used by oprofile and other similar tools. */
2575 seq_printf(m
, " 0x%p", mod
->module_core
);
2579 seq_printf(m
, " %s", module_flags(mod
, buf
));
2581 seq_printf(m
, "\n");
2585 /* Format: modulename size refcount deps address
2587 Where refcount is a number or -, and deps is a comma-separated list
2590 const struct seq_operations modules_op
= {
2597 /* Given an address, look for it in the module exception tables. */
2598 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2600 const struct exception_table_entry
*e
= NULL
;
2604 list_for_each_entry(mod
, &modules
, list
) {
2605 if (mod
->num_exentries
== 0)
2608 e
= search_extable(mod
->extable
,
2609 mod
->extable
+ mod
->num_exentries
- 1,
2616 /* Now, if we found one, we are running inside it now, hence
2617 we cannot unload the module, hence no refcnt needed. */
2622 * Is this a valid module address?
2624 int is_module_address(unsigned long addr
)
2630 list_for_each_entry(mod
, &modules
, list
) {
2631 if (within(addr
, mod
->module_core
, mod
->core_size
)) {
2643 /* Is this a valid kernel address? */
2644 struct module
*__module_text_address(unsigned long addr
)
2648 list_for_each_entry(mod
, &modules
, list
)
2649 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
2650 || within(addr
, mod
->module_core
, mod
->core_text_size
))
2655 struct module
*module_text_address(unsigned long addr
)
2660 mod
= __module_text_address(addr
);
2666 /* Don't grab lock, we're oopsing. */
2667 void print_modules(void)
2672 printk("Modules linked in:");
2673 list_for_each_entry(mod
, &modules
, list
)
2674 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
2675 if (last_unloaded_module
[0])
2676 printk(" [last unloaded: %s]", last_unloaded_module
);
2680 #ifdef CONFIG_MODVERSIONS
2681 /* Generate the signature for struct module here, too, for modversions. */
2682 void struct_module(struct module
*mod
) { return; }
2683 EXPORT_SYMBOL(struct_module
);
2686 #ifdef CONFIG_MARKERS
2687 void module_update_markers(void)
2691 mutex_lock(&module_mutex
);
2692 list_for_each_entry(mod
, &modules
, list
)
2694 marker_update_probe_range(mod
->markers
,
2695 mod
->markers
+ mod
->num_markers
);
2696 mutex_unlock(&module_mutex
);