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/ftrace_event.h>
22 #include <linux/init.h>
23 #include <linux/kallsyms.h>
25 #include <linux/sysfs.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/elf.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/syscalls.h>
33 #include <linux/fcntl.h>
34 #include <linux/rcupdate.h>
35 #include <linux/capability.h>
36 #include <linux/cpu.h>
37 #include <linux/moduleparam.h>
38 #include <linux/errno.h>
39 #include <linux/err.h>
40 #include <linux/vermagic.h>
41 #include <linux/notifier.h>
42 #include <linux/sched.h>
43 #include <linux/stop_machine.h>
44 #include <linux/device.h>
45 #include <linux/string.h>
46 #include <linux/mutex.h>
47 #include <linux/rculist.h>
48 #include <asm/uaccess.h>
49 #include <asm/cacheflush.h>
50 #include <asm/mmu_context.h>
51 #include <linux/license.h>
52 #include <asm/sections.h>
53 #include <linux/tracepoint.h>
54 #include <linux/ftrace.h>
55 #include <linux/async.h>
56 #include <linux/percpu.h>
57 #include <linux/kmemleak.h>
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/module.h>
65 #define DEBUGP(fmt , a...)
68 #ifndef ARCH_SHF_SMALL
69 #define ARCH_SHF_SMALL 0
72 /* If this is set, the section belongs in the init part of the module */
73 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
75 /* List of modules, protected by module_mutex or preempt_disable
76 * (delete uses stop_machine/add uses RCU list operations). */
77 DEFINE_MUTEX(module_mutex);
78 EXPORT_SYMBOL_GPL(module_mutex);
79 static LIST_HEAD(modules);
81 /* Block module loading/unloading? */
82 int modules_disabled = 0;
84 /* Waiting for a module to finish initializing? */
85 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
87 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
89 /* Bounds of module allocation, for speeding __module_address */
90 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
92 int register_module_notifier(struct notifier_block * nb)
94 return blocking_notifier_chain_register(&module_notify_list, nb);
96 EXPORT_SYMBOL(register_module_notifier);
98 int unregister_module_notifier(struct notifier_block * nb)
100 return blocking_notifier_chain_unregister(&module_notify_list, nb);
102 EXPORT_SYMBOL(unregister_module_notifier);
104 /* We require a truly strong try_module_get(): 0 means failure due to
105 ongoing or failed initialization etc. */
106 static inline int strong_try_module_get(struct module *mod)
108 if (mod && mod->state == MODULE_STATE_COMING)
110 if (try_module_get(mod))
116 static inline void add_taint_module(struct module *mod, unsigned flag)
119 mod->taints |= (1U << flag);
123 * A thread that wants to hold a reference to a module only while it
124 * is running can call this to safely exit. nfsd and lockd use this.
126 void __module_put_and_exit(struct module *mod, long code)
131 EXPORT_SYMBOL(__module_put_and_exit);
133 /* Find a module section: 0 means not found. */
134 static unsigned int find_sec(Elf_Ehdr *hdr,
136 const char *secstrings,
141 for (i = 1; i < hdr->e_shnum; i++)
142 /* Alloc bit cleared means "ignore it." */
143 if ((sechdrs[i].sh_flags & SHF_ALLOC)
144 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
149 /* Find a module section, or NULL. */
150 static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
151 const char *secstrings, const char *name)
153 /* Section 0 has sh_addr 0. */
154 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
157 /* Find a module section, or NULL. Fill in number of "objects" in section. */
158 static void *section_objs(Elf_Ehdr *hdr,
160 const char *secstrings,
165 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
167 /* Section 0 has sh_addr 0 and sh_size 0. */
168 *num = sechdrs[sec].sh_size / object_size;
169 return (void *)sechdrs[sec].sh_addr;
172 /* Provided by the linker */
173 extern const struct kernel_symbol __start___ksymtab[];
174 extern const struct kernel_symbol __stop___ksymtab[];
175 extern const struct kernel_symbol __start___ksymtab_gpl[];
176 extern const struct kernel_symbol __stop___ksymtab_gpl[];
177 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
178 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
179 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
180 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
181 extern const unsigned long __start___kcrctab[];
182 extern const unsigned long __start___kcrctab_gpl[];
183 extern const unsigned long __start___kcrctab_gpl_future[];
184 #ifdef CONFIG_UNUSED_SYMBOLS
185 extern const struct kernel_symbol __start___ksymtab_unused[];
186 extern const struct kernel_symbol __stop___ksymtab_unused[];
187 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
188 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
189 extern const unsigned long __start___kcrctab_unused[];
190 extern const unsigned long __start___kcrctab_unused_gpl[];
193 #ifndef CONFIG_MODVERSIONS
194 #define symversion(base, idx) NULL
196 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
199 static bool each_symbol_in_section(const struct symsearch *arr,
200 unsigned int arrsize,
201 struct module *owner,
202 bool (*fn)(const struct symsearch *syms,
203 struct module *owner,
204 unsigned int symnum, void *data),
209 for (j = 0; j < arrsize; j++) {
210 for (i = 0; i < arr[j].stop - arr[j].start; i++)
211 if (fn(&arr[j], owner, i, data))
218 /* Returns true as soon as fn returns true, otherwise false. */
219 bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
220 unsigned int symnum, void *data), void *data)
223 const struct symsearch arr[] = {
224 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
225 NOT_GPL_ONLY, false },
226 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
227 __start___kcrctab_gpl,
229 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
230 __start___kcrctab_gpl_future,
231 WILL_BE_GPL_ONLY, false },
232 #ifdef CONFIG_UNUSED_SYMBOLS
233 { __start___ksymtab_unused, __stop___ksymtab_unused,
234 __start___kcrctab_unused,
235 NOT_GPL_ONLY, true },
236 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
237 __start___kcrctab_unused_gpl,
242 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
245 list_for_each_entry_rcu(mod, &modules, list) {
246 struct symsearch arr[] = {
247 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
248 NOT_GPL_ONLY, false },
249 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
252 { mod->gpl_future_syms,
253 mod->gpl_future_syms + mod->num_gpl_future_syms,
254 mod->gpl_future_crcs,
255 WILL_BE_GPL_ONLY, false },
256 #ifdef CONFIG_UNUSED_SYMBOLS
258 mod->unused_syms + mod->num_unused_syms,
260 NOT_GPL_ONLY, true },
261 { mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
263 mod->unused_gpl_crcs,
268 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
273 EXPORT_SYMBOL_GPL(each_symbol);
275 struct find_symbol_arg {
282 struct module *owner;
283 const unsigned long *crc;
284 const struct kernel_symbol *sym;
287 static bool find_symbol_in_section(const struct symsearch *syms,
288 struct module *owner,
289 unsigned int symnum, void *data)
291 struct find_symbol_arg *fsa = data;
293 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
297 if (syms->licence == GPL_ONLY)
299 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
300 printk(KERN_WARNING "Symbol %s is being used "
301 "by a non-GPL module, which will not "
302 "be allowed in the future\n", fsa->name);
303 printk(KERN_WARNING "Please see the file "
304 "Documentation/feature-removal-schedule.txt "
305 "in the kernel source tree for more details.\n");
309 #ifdef CONFIG_UNUSED_SYMBOLS
310 if (syms->unused && fsa->warn) {
311 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
312 "however this module is using it.\n", fsa->name);
314 "This symbol will go away in the future.\n");
316 "Please evalute if this is the right api to use and if "
317 "it really is, submit a report the linux kernel "
318 "mailinglist together with submitting your code for "
324 fsa->crc = symversion(syms->crcs, symnum);
325 fsa->sym = &syms->start[symnum];
329 /* Find a symbol and return it, along with, (optional) crc and
330 * (optional) module which owns it */
331 const struct kernel_symbol *find_symbol(const char *name,
332 struct module **owner,
333 const unsigned long **crc,
337 struct find_symbol_arg fsa;
343 if (each_symbol(find_symbol_in_section, &fsa)) {
351 DEBUGP("Failed to find symbol %s\n", name);
354 EXPORT_SYMBOL_GPL(find_symbol);
356 /* Search for module by name: must hold module_mutex. */
357 struct module *find_module(const char *name)
361 list_for_each_entry(mod, &modules, list) {
362 if (strcmp(mod->name, name) == 0)
367 EXPORT_SYMBOL_GPL(find_module);
371 static inline void __percpu *mod_percpu(struct module *mod)
376 static int percpu_modalloc(struct module *mod,
377 unsigned long size, unsigned long align)
379 if (align > PAGE_SIZE) {
380 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
381 mod->name, align, PAGE_SIZE);
385 mod->percpu = __alloc_reserved_percpu(size, align);
388 "Could not allocate %lu bytes percpu data\n", size);
391 mod->percpu_size = size;
395 static void percpu_modfree(struct module *mod)
397 free_percpu(mod->percpu);
400 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
402 const char *secstrings)
404 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
407 static void percpu_modcopy(struct module *mod,
408 const void *from, unsigned long size)
412 for_each_possible_cpu(cpu)
413 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
417 * is_module_percpu_address - test whether address is from module static percpu
418 * @addr: address to test
420 * Test whether @addr belongs to module static percpu area.
423 * %true if @addr is from module static percpu area
425 bool is_module_percpu_address(unsigned long addr)
432 list_for_each_entry_rcu(mod, &modules, list) {
433 if (!mod->percpu_size)
435 for_each_possible_cpu(cpu) {
436 void *start = per_cpu_ptr(mod->percpu, cpu);
438 if ((void *)addr >= start &&
439 (void *)addr < start + mod->percpu_size) {
450 #else /* ... !CONFIG_SMP */
452 static inline void __percpu *mod_percpu(struct module *mod)
456 static inline int percpu_modalloc(struct module *mod,
457 unsigned long size, unsigned long align)
461 static inline void percpu_modfree(struct module *mod)
464 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
466 const char *secstrings)
470 static inline void percpu_modcopy(struct module *mod,
471 const void *from, unsigned long size)
473 /* pcpusec should be 0, and size of that section should be 0. */
476 bool is_module_percpu_address(unsigned long addr)
481 #endif /* CONFIG_SMP */
483 #define MODINFO_ATTR(field) \
484 static void setup_modinfo_##field(struct module *mod, const char *s) \
486 mod->field = kstrdup(s, GFP_KERNEL); \
488 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
489 struct module *mod, char *buffer) \
491 return sprintf(buffer, "%s\n", mod->field); \
493 static int modinfo_##field##_exists(struct module *mod) \
495 return mod->field != NULL; \
497 static void free_modinfo_##field(struct module *mod) \
502 static struct module_attribute modinfo_##field = { \
503 .attr = { .name = __stringify(field), .mode = 0444 }, \
504 .show = show_modinfo_##field, \
505 .setup = setup_modinfo_##field, \
506 .test = modinfo_##field##_exists, \
507 .free = free_modinfo_##field, \
510 MODINFO_ATTR(version);
511 MODINFO_ATTR(srcversion);
513 static char last_unloaded_module[MODULE_NAME_LEN+1];
515 #ifdef CONFIG_MODULE_UNLOAD
517 EXPORT_TRACEPOINT_SYMBOL(module_get);
519 /* Init the unload section of the module. */
520 static void module_unload_init(struct module *mod)
524 INIT_LIST_HEAD(&mod->modules_which_use_me);
525 for_each_possible_cpu(cpu) {
526 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
527 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
530 /* Hold reference count during initialization. */
531 __this_cpu_write(mod->refptr->incs, 1);
532 /* Backwards compatibility macros put refcount during init. */
533 mod->waiter = current;
536 /* modules using other modules */
539 struct list_head list;
540 struct module *module_which_uses;
543 /* Does a already use b? */
544 static int already_uses(struct module *a, struct module *b)
546 struct module_use *use;
548 list_for_each_entry(use, &b->modules_which_use_me, list) {
549 if (use->module_which_uses == a) {
550 DEBUGP("%s uses %s!\n", a->name, b->name);
554 DEBUGP("%s does not use %s!\n", a->name, b->name);
558 /* Module a uses b */
559 int use_module(struct module *a, struct module *b)
561 struct module_use *use;
564 if (b == NULL || already_uses(a, b))
567 /* If we're interrupted or time out, we fail. */
568 err = strong_try_module_get(b);
572 DEBUGP("Allocating new usage for %s.\n", a->name);
573 use = kmalloc(sizeof(*use), GFP_ATOMIC);
575 printk("%s: out of memory loading\n", a->name);
580 use->module_which_uses = a;
581 list_add(&use->list, &b->modules_which_use_me);
582 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
585 EXPORT_SYMBOL_GPL(use_module);
587 /* Clear the unload stuff of the module. */
588 static void module_unload_free(struct module *mod)
592 list_for_each_entry(i, &modules, list) {
593 struct module_use *use;
595 list_for_each_entry(use, &i->modules_which_use_me, list) {
596 if (use->module_which_uses == mod) {
597 DEBUGP("%s unusing %s\n", mod->name, i->name);
599 list_del(&use->list);
601 sysfs_remove_link(i->holders_dir, mod->name);
602 /* There can be at most one match. */
609 #ifdef CONFIG_MODULE_FORCE_UNLOAD
610 static inline int try_force_unload(unsigned int flags)
612 int ret = (flags & O_TRUNC);
614 add_taint(TAINT_FORCED_RMMOD);
618 static inline int try_force_unload(unsigned int flags)
622 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
631 /* Whole machine is stopped with interrupts off when this runs. */
632 static int __try_stop_module(void *_sref)
634 struct stopref *sref = _sref;
636 /* If it's not unused, quit unless we're forcing. */
637 if (module_refcount(sref->mod) != 0) {
638 if (!(*sref->forced = try_force_unload(sref->flags)))
642 /* Mark it as dying. */
643 sref->mod->state = MODULE_STATE_GOING;
647 static int try_stop_module(struct module *mod, int flags, int *forced)
649 if (flags & O_NONBLOCK) {
650 struct stopref sref = { mod, flags, forced };
652 return stop_machine(__try_stop_module, &sref, NULL);
654 /* We don't need to stop the machine for this. */
655 mod->state = MODULE_STATE_GOING;
661 unsigned int module_refcount(struct module *mod)
663 unsigned int incs = 0, decs = 0;
666 for_each_possible_cpu(cpu)
667 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
669 * ensure the incs are added up after the decs.
670 * module_put ensures incs are visible before decs with smp_wmb.
672 * This 2-count scheme avoids the situation where the refcount
673 * for CPU0 is read, then CPU0 increments the module refcount,
674 * then CPU1 drops that refcount, then the refcount for CPU1 is
675 * read. We would record a decrement but not its corresponding
676 * increment so we would see a low count (disaster).
678 * Rare situation? But module_refcount can be preempted, and we
679 * might be tallying up 4096+ CPUs. So it is not impossible.
682 for_each_possible_cpu(cpu)
683 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
686 EXPORT_SYMBOL(module_refcount);
688 /* This exists whether we can unload or not */
689 static void free_module(struct module *mod);
691 static void wait_for_zero_refcount(struct module *mod)
693 /* Since we might sleep for some time, release the mutex first */
694 mutex_unlock(&module_mutex);
696 DEBUGP("Looking at refcount...\n");
697 set_current_state(TASK_UNINTERRUPTIBLE);
698 if (module_refcount(mod) == 0)
702 current->state = TASK_RUNNING;
703 mutex_lock(&module_mutex);
706 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
710 char name[MODULE_NAME_LEN];
713 if (!capable(CAP_SYS_MODULE) || modules_disabled)
716 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
718 name[MODULE_NAME_LEN-1] = '\0';
720 if (mutex_lock_interruptible(&module_mutex) != 0)
723 mod = find_module(name);
729 if (!list_empty(&mod->modules_which_use_me)) {
730 /* Other modules depend on us: get rid of them first. */
735 /* Doing init or already dying? */
736 if (mod->state != MODULE_STATE_LIVE) {
737 /* FIXME: if (force), slam module count and wake up
739 DEBUGP("%s already dying\n", mod->name);
744 /* If it has an init func, it must have an exit func to unload */
745 if (mod->init && !mod->exit) {
746 forced = try_force_unload(flags);
748 /* This module can't be removed */
754 /* Set this up before setting mod->state */
755 mod->waiter = current;
757 /* Stop the machine so refcounts can't move and disable module. */
758 ret = try_stop_module(mod, flags, &forced);
762 /* Never wait if forced. */
763 if (!forced && module_refcount(mod) != 0)
764 wait_for_zero_refcount(mod);
766 mutex_unlock(&module_mutex);
767 /* Final destruction now noone is using it. */
768 if (mod->exit != NULL)
770 blocking_notifier_call_chain(&module_notify_list,
771 MODULE_STATE_GOING, mod);
772 async_synchronize_full();
773 mutex_lock(&module_mutex);
774 /* Store the name of the last unloaded module for diagnostic purposes */
775 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
776 ddebug_remove_module(mod->name);
780 mutex_unlock(&module_mutex);
784 static inline void print_unload_info(struct seq_file *m, struct module *mod)
786 struct module_use *use;
787 int printed_something = 0;
789 seq_printf(m, " %u ", module_refcount(mod));
791 /* Always include a trailing , so userspace can differentiate
792 between this and the old multi-field proc format. */
793 list_for_each_entry(use, &mod->modules_which_use_me, list) {
794 printed_something = 1;
795 seq_printf(m, "%s,", use->module_which_uses->name);
798 if (mod->init != NULL && mod->exit == NULL) {
799 printed_something = 1;
800 seq_printf(m, "[permanent],");
803 if (!printed_something)
807 void __symbol_put(const char *symbol)
809 struct module *owner;
812 if (!find_symbol(symbol, &owner, NULL, true, false))
817 EXPORT_SYMBOL(__symbol_put);
819 /* Note this assumes addr is a function, which it currently always is. */
820 void symbol_put_addr(void *addr)
822 struct module *modaddr;
823 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
825 if (core_kernel_text(a))
828 /* module_text_address is safe here: we're supposed to have reference
829 * to module from symbol_get, so it can't go away. */
830 modaddr = __module_text_address(a);
834 EXPORT_SYMBOL_GPL(symbol_put_addr);
836 static ssize_t show_refcnt(struct module_attribute *mattr,
837 struct module *mod, char *buffer)
839 return sprintf(buffer, "%u\n", module_refcount(mod));
842 static struct module_attribute refcnt = {
843 .attr = { .name = "refcnt", .mode = 0444 },
847 void module_put(struct module *module)
851 smp_wmb(); /* see comment in module_refcount */
852 __this_cpu_inc(module->refptr->decs);
854 trace_module_put(module, _RET_IP_);
855 /* Maybe they're waiting for us to drop reference? */
856 if (unlikely(!module_is_live(module)))
857 wake_up_process(module->waiter);
861 EXPORT_SYMBOL(module_put);
863 #else /* !CONFIG_MODULE_UNLOAD */
864 static inline void print_unload_info(struct seq_file *m, struct module *mod)
866 /* We don't know the usage count, or what modules are using. */
867 seq_printf(m, " - -");
870 static inline void module_unload_free(struct module *mod)
874 int use_module(struct module *a, struct module *b)
876 return strong_try_module_get(b);
878 EXPORT_SYMBOL_GPL(use_module);
880 static inline void module_unload_init(struct module *mod)
883 #endif /* CONFIG_MODULE_UNLOAD */
885 static ssize_t show_initstate(struct module_attribute *mattr,
886 struct module *mod, char *buffer)
888 const char *state = "unknown";
890 switch (mod->state) {
891 case MODULE_STATE_LIVE:
894 case MODULE_STATE_COMING:
897 case MODULE_STATE_GOING:
901 return sprintf(buffer, "%s\n", state);
904 static struct module_attribute initstate = {
905 .attr = { .name = "initstate", .mode = 0444 },
906 .show = show_initstate,
909 static struct module_attribute *modinfo_attrs[] = {
913 #ifdef CONFIG_MODULE_UNLOAD
919 static const char vermagic[] = VERMAGIC_STRING;
921 static int try_to_force_load(struct module *mod, const char *reason)
923 #ifdef CONFIG_MODULE_FORCE_LOAD
924 if (!test_taint(TAINT_FORCED_MODULE))
925 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
927 add_taint_module(mod, TAINT_FORCED_MODULE);
934 #ifdef CONFIG_MODVERSIONS
935 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
936 static unsigned long maybe_relocated(unsigned long crc,
937 const struct module *crc_owner)
939 #ifdef ARCH_RELOCATES_KCRCTAB
940 if (crc_owner == NULL)
941 return crc - (unsigned long)reloc_start;
946 static int check_version(Elf_Shdr *sechdrs,
947 unsigned int versindex,
950 const unsigned long *crc,
951 const struct module *crc_owner)
953 unsigned int i, num_versions;
954 struct modversion_info *versions;
956 /* Exporting module didn't supply crcs? OK, we're already tainted. */
960 /* No versions at all? modprobe --force does this. */
962 return try_to_force_load(mod, symname) == 0;
964 versions = (void *) sechdrs[versindex].sh_addr;
965 num_versions = sechdrs[versindex].sh_size
966 / sizeof(struct modversion_info);
968 for (i = 0; i < num_versions; i++) {
969 if (strcmp(versions[i].name, symname) != 0)
972 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
974 DEBUGP("Found checksum %lX vs module %lX\n",
975 maybe_relocated(*crc, crc_owner), versions[i].crc);
979 printk(KERN_WARNING "%s: no symbol version for %s\n",
984 printk("%s: disagrees about version of symbol %s\n",
989 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
990 unsigned int versindex,
993 const unsigned long *crc;
995 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
998 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1002 /* First part is kernel version, which we ignore if module has crcs. */
1003 static inline int same_magic(const char *amagic, const char *bmagic,
1007 amagic += strcspn(amagic, " ");
1008 bmagic += strcspn(bmagic, " ");
1010 return strcmp(amagic, bmagic) == 0;
1013 static inline int check_version(Elf_Shdr *sechdrs,
1014 unsigned int versindex,
1015 const char *symname,
1017 const unsigned long *crc,
1018 const struct module *crc_owner)
1023 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1024 unsigned int versindex,
1030 static inline int same_magic(const char *amagic, const char *bmagic,
1033 return strcmp(amagic, bmagic) == 0;
1035 #endif /* CONFIG_MODVERSIONS */
1037 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1038 Must be holding module_mutex. */
1039 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1040 unsigned int versindex,
1044 struct module *owner;
1045 const struct kernel_symbol *sym;
1046 const unsigned long *crc;
1049 long timeleft = 30 * HZ;
1052 sym = find_symbol(name, &owner, &crc,
1053 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1057 if (!check_version(sechdrs, versindex, name, mod, crc, owner))
1060 prepare_to_wait(&module_wq, &wait, TASK_INTERRUPTIBLE);
1061 err = use_module(mod, owner);
1062 if (likely(!err) || err != -EBUSY || signal_pending(current)) {
1063 finish_wait(&module_wq, &wait);
1064 return err ? NULL : sym;
1067 /* Module is still loading. Drop lock and wait. */
1068 mutex_unlock(&module_mutex);
1069 timeleft = schedule_timeout(timeleft);
1070 mutex_lock(&module_mutex);
1071 finish_wait(&module_wq, &wait);
1073 /* Module might be gone entirely, or replaced. Re-lookup. */
1077 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1078 mod->name, owner->name);
1083 * /sys/module/foo/sections stuff
1084 * J. Corbet <corbet@lwn.net>
1086 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1088 static inline bool sect_empty(const Elf_Shdr *sect)
1090 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1093 struct module_sect_attr
1095 struct module_attribute mattr;
1097 unsigned long address;
1100 struct module_sect_attrs
1102 struct attribute_group grp;
1103 unsigned int nsections;
1104 struct module_sect_attr attrs[0];
1107 static ssize_t module_sect_show(struct module_attribute *mattr,
1108 struct module *mod, char *buf)
1110 struct module_sect_attr *sattr =
1111 container_of(mattr, struct module_sect_attr, mattr);
1112 return sprintf(buf, "0x%lx\n", sattr->address);
1115 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1117 unsigned int section;
1119 for (section = 0; section < sect_attrs->nsections; section++)
1120 kfree(sect_attrs->attrs[section].name);
1124 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1125 char *secstrings, Elf_Shdr *sechdrs)
1127 unsigned int nloaded = 0, i, size[2];
1128 struct module_sect_attrs *sect_attrs;
1129 struct module_sect_attr *sattr;
1130 struct attribute **gattr;
1132 /* Count loaded sections and allocate structures */
1133 for (i = 0; i < nsect; i++)
1134 if (!sect_empty(&sechdrs[i]))
1136 size[0] = ALIGN(sizeof(*sect_attrs)
1137 + nloaded * sizeof(sect_attrs->attrs[0]),
1138 sizeof(sect_attrs->grp.attrs[0]));
1139 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1140 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1141 if (sect_attrs == NULL)
1144 /* Setup section attributes. */
1145 sect_attrs->grp.name = "sections";
1146 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1148 sect_attrs->nsections = 0;
1149 sattr = §_attrs->attrs[0];
1150 gattr = §_attrs->grp.attrs[0];
1151 for (i = 0; i < nsect; i++) {
1152 if (sect_empty(&sechdrs[i]))
1154 sattr->address = sechdrs[i].sh_addr;
1155 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1157 if (sattr->name == NULL)
1159 sect_attrs->nsections++;
1160 sysfs_attr_init(&sattr->mattr.attr);
1161 sattr->mattr.show = module_sect_show;
1162 sattr->mattr.store = NULL;
1163 sattr->mattr.attr.name = sattr->name;
1164 sattr->mattr.attr.mode = S_IRUGO;
1165 *(gattr++) = &(sattr++)->mattr.attr;
1169 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1172 mod->sect_attrs = sect_attrs;
1175 free_sect_attrs(sect_attrs);
1178 static void remove_sect_attrs(struct module *mod)
1180 if (mod->sect_attrs) {
1181 sysfs_remove_group(&mod->mkobj.kobj,
1182 &mod->sect_attrs->grp);
1183 /* We are positive that no one is using any sect attrs
1184 * at this point. Deallocate immediately. */
1185 free_sect_attrs(mod->sect_attrs);
1186 mod->sect_attrs = NULL;
1191 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1194 struct module_notes_attrs {
1195 struct kobject *dir;
1197 struct bin_attribute attrs[0];
1200 static ssize_t module_notes_read(struct kobject *kobj,
1201 struct bin_attribute *bin_attr,
1202 char *buf, loff_t pos, size_t count)
1205 * The caller checked the pos and count against our size.
1207 memcpy(buf, bin_attr->private + pos, count);
1211 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1214 if (notes_attrs->dir) {
1216 sysfs_remove_bin_file(notes_attrs->dir,
1217 ¬es_attrs->attrs[i]);
1218 kobject_put(notes_attrs->dir);
1223 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1224 char *secstrings, Elf_Shdr *sechdrs)
1226 unsigned int notes, loaded, i;
1227 struct module_notes_attrs *notes_attrs;
1228 struct bin_attribute *nattr;
1230 /* failed to create section attributes, so can't create notes */
1231 if (!mod->sect_attrs)
1234 /* Count notes sections and allocate structures. */
1236 for (i = 0; i < nsect; i++)
1237 if (!sect_empty(&sechdrs[i]) &&
1238 (sechdrs[i].sh_type == SHT_NOTE))
1244 notes_attrs = kzalloc(sizeof(*notes_attrs)
1245 + notes * sizeof(notes_attrs->attrs[0]),
1247 if (notes_attrs == NULL)
1250 notes_attrs->notes = notes;
1251 nattr = ¬es_attrs->attrs[0];
1252 for (loaded = i = 0; i < nsect; ++i) {
1253 if (sect_empty(&sechdrs[i]))
1255 if (sechdrs[i].sh_type == SHT_NOTE) {
1256 sysfs_bin_attr_init(nattr);
1257 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1258 nattr->attr.mode = S_IRUGO;
1259 nattr->size = sechdrs[i].sh_size;
1260 nattr->private = (void *) sechdrs[i].sh_addr;
1261 nattr->read = module_notes_read;
1267 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1268 if (!notes_attrs->dir)
1271 for (i = 0; i < notes; ++i)
1272 if (sysfs_create_bin_file(notes_attrs->dir,
1273 ¬es_attrs->attrs[i]))
1276 mod->notes_attrs = notes_attrs;
1280 free_notes_attrs(notes_attrs, i);
1283 static void remove_notes_attrs(struct module *mod)
1285 if (mod->notes_attrs)
1286 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1291 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1292 char *sectstrings, Elf_Shdr *sechdrs)
1296 static inline void remove_sect_attrs(struct module *mod)
1300 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1301 char *sectstrings, Elf_Shdr *sechdrs)
1305 static inline void remove_notes_attrs(struct module *mod)
1311 int module_add_modinfo_attrs(struct module *mod)
1313 struct module_attribute *attr;
1314 struct module_attribute *temp_attr;
1318 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1319 (ARRAY_SIZE(modinfo_attrs) + 1)),
1321 if (!mod->modinfo_attrs)
1324 temp_attr = mod->modinfo_attrs;
1325 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1327 (attr->test && attr->test(mod))) {
1328 memcpy(temp_attr, attr, sizeof(*temp_attr));
1329 sysfs_attr_init(&temp_attr->attr);
1330 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1337 void module_remove_modinfo_attrs(struct module *mod)
1339 struct module_attribute *attr;
1342 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1343 /* pick a field to test for end of list */
1344 if (!attr->attr.name)
1346 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1350 kfree(mod->modinfo_attrs);
1353 int mod_sysfs_init(struct module *mod)
1356 struct kobject *kobj;
1358 if (!module_sysfs_initialized) {
1359 printk(KERN_ERR "%s: module sysfs not initialized\n",
1365 kobj = kset_find_obj(module_kset, mod->name);
1367 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1373 mod->mkobj.mod = mod;
1375 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1376 mod->mkobj.kobj.kset = module_kset;
1377 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1380 kobject_put(&mod->mkobj.kobj);
1382 /* delay uevent until full sysfs population */
1387 int mod_sysfs_setup(struct module *mod,
1388 struct kernel_param *kparam,
1389 unsigned int num_params)
1393 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1394 if (!mod->holders_dir) {
1399 err = module_param_sysfs_setup(mod, kparam, num_params);
1401 goto out_unreg_holders;
1403 err = module_add_modinfo_attrs(mod);
1405 goto out_unreg_param;
1407 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1411 module_param_sysfs_remove(mod);
1413 kobject_put(mod->holders_dir);
1415 kobject_put(&mod->mkobj.kobj);
1419 static void mod_sysfs_fini(struct module *mod)
1421 kobject_put(&mod->mkobj.kobj);
1424 #else /* CONFIG_SYSFS */
1426 static void mod_sysfs_fini(struct module *mod)
1430 #endif /* CONFIG_SYSFS */
1432 static void mod_kobject_remove(struct module *mod)
1434 module_remove_modinfo_attrs(mod);
1435 module_param_sysfs_remove(mod);
1436 kobject_put(mod->mkobj.drivers_dir);
1437 kobject_put(mod->holders_dir);
1438 mod_sysfs_fini(mod);
1442 * unlink the module with the whole machine is stopped with interrupts off
1443 * - this defends against kallsyms not taking locks
1445 static int __unlink_module(void *_mod)
1447 struct module *mod = _mod;
1448 list_del(&mod->list);
1452 /* Free a module, remove from lists, etc (must hold module_mutex). */
1453 static void free_module(struct module *mod)
1455 trace_module_free(mod);
1457 /* Delete from various lists */
1458 stop_machine(__unlink_module, mod, NULL);
1459 remove_notes_attrs(mod);
1460 remove_sect_attrs(mod);
1461 mod_kobject_remove(mod);
1463 /* Arch-specific cleanup. */
1464 module_arch_cleanup(mod);
1466 /* Module unload stuff */
1467 module_unload_free(mod);
1469 /* Free any allocated parameters. */
1470 destroy_params(mod->kp, mod->num_kp);
1472 /* This may be NULL, but that's OK */
1473 module_free(mod, mod->module_init);
1475 percpu_modfree(mod);
1476 #if defined(CONFIG_MODULE_UNLOAD)
1478 free_percpu(mod->refptr);
1480 /* Free lock-classes: */
1481 lockdep_free_key_range(mod->module_core, mod->core_size);
1483 /* Finally, free the core (containing the module structure) */
1484 module_free(mod, mod->module_core);
1487 update_protections(current->mm);
1491 void *__symbol_get(const char *symbol)
1493 struct module *owner;
1494 const struct kernel_symbol *sym;
1497 sym = find_symbol(symbol, &owner, NULL, true, true);
1498 if (sym && strong_try_module_get(owner))
1502 return sym ? (void *)sym->value : NULL;
1504 EXPORT_SYMBOL_GPL(__symbol_get);
1507 * Ensure that an exported symbol [global namespace] does not already exist
1508 * in the kernel or in some other module's exported symbol table.
1510 static int verify_export_symbols(struct module *mod)
1513 struct module *owner;
1514 const struct kernel_symbol *s;
1516 const struct kernel_symbol *sym;
1519 { mod->syms, mod->num_syms },
1520 { mod->gpl_syms, mod->num_gpl_syms },
1521 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1522 #ifdef CONFIG_UNUSED_SYMBOLS
1523 { mod->unused_syms, mod->num_unused_syms },
1524 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1528 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1529 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1530 if (find_symbol(s->name, &owner, NULL, true, false)) {
1532 "%s: exports duplicate symbol %s"
1534 mod->name, s->name, module_name(owner));
1542 /* Change all symbols so that st_value encodes the pointer directly. */
1543 static int simplify_symbols(Elf_Shdr *sechdrs,
1544 unsigned int symindex,
1546 unsigned int versindex,
1547 unsigned int pcpuindex,
1550 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1551 unsigned long secbase;
1552 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1554 const struct kernel_symbol *ksym;
1556 for (i = 1; i < n; i++) {
1557 switch (sym[i].st_shndx) {
1559 /* We compiled with -fno-common. These are not
1560 supposed to happen. */
1561 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1562 printk("%s: please compile with -fno-common\n",
1568 /* Don't need to do anything */
1569 DEBUGP("Absolute symbol: 0x%08lx\n",
1570 (long)sym[i].st_value);
1574 ksym = resolve_symbol(sechdrs, versindex,
1575 strtab + sym[i].st_name, mod);
1576 /* Ok if resolved. */
1578 sym[i].st_value = ksym->value;
1583 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1586 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1587 mod->name, strtab + sym[i].st_name);
1592 /* Divert to percpu allocation if a percpu var. */
1593 if (sym[i].st_shndx == pcpuindex)
1594 secbase = (unsigned long)mod_percpu(mod);
1596 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1597 sym[i].st_value += secbase;
1605 /* Additional bytes needed by arch in front of individual sections */
1606 unsigned int __weak arch_mod_section_prepend(struct module *mod,
1607 unsigned int section)
1609 /* default implementation just returns zero */
1613 /* Update size with this section: return offset. */
1614 static long get_offset(struct module *mod, unsigned int *size,
1615 Elf_Shdr *sechdr, unsigned int section)
1619 *size += arch_mod_section_prepend(mod, section);
1620 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1621 *size = ret + sechdr->sh_size;
1625 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1626 might -- code, read-only data, read-write data, small data. Tally
1627 sizes, and place the offsets into sh_entsize fields: high bit means it
1629 static void layout_sections(struct module *mod,
1630 const Elf_Ehdr *hdr,
1632 const char *secstrings)
1634 static unsigned long const masks[][2] = {
1635 /* NOTE: all executable code must be the first section
1636 * in this array; otherwise modify the text_size
1637 * finder in the two loops below */
1638 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1639 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1640 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1641 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1645 for (i = 0; i < hdr->e_shnum; i++)
1646 sechdrs[i].sh_entsize = ~0UL;
1648 DEBUGP("Core section allocation order:\n");
1649 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1650 for (i = 0; i < hdr->e_shnum; ++i) {
1651 Elf_Shdr *s = &sechdrs[i];
1653 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1654 || (s->sh_flags & masks[m][1])
1655 || s->sh_entsize != ~0UL
1656 || strstarts(secstrings + s->sh_name, ".init"))
1658 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1659 DEBUGP("\t%s\n", secstrings + s->sh_name);
1662 mod->core_text_size = mod->core_size;
1665 DEBUGP("Init section allocation order:\n");
1666 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1667 for (i = 0; i < hdr->e_shnum; ++i) {
1668 Elf_Shdr *s = &sechdrs[i];
1670 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1671 || (s->sh_flags & masks[m][1])
1672 || s->sh_entsize != ~0UL
1673 || !strstarts(secstrings + s->sh_name, ".init"))
1675 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1676 | INIT_OFFSET_MASK);
1677 DEBUGP("\t%s\n", secstrings + s->sh_name);
1680 mod->init_text_size = mod->init_size;
1684 static void set_license(struct module *mod, const char *license)
1687 license = "unspecified";
1689 if (!license_is_gpl_compatible(license)) {
1690 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1691 printk(KERN_WARNING "%s: module license '%s' taints "
1692 "kernel.\n", mod->name, license);
1693 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1697 /* Parse tag=value strings from .modinfo section */
1698 static char *next_string(char *string, unsigned long *secsize)
1700 /* Skip non-zero chars */
1703 if ((*secsize)-- <= 1)
1707 /* Skip any zero padding. */
1708 while (!string[0]) {
1710 if ((*secsize)-- <= 1)
1716 static char *get_modinfo(Elf_Shdr *sechdrs,
1721 unsigned int taglen = strlen(tag);
1722 unsigned long size = sechdrs[info].sh_size;
1724 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1725 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1726 return p + taglen + 1;
1731 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1732 unsigned int infoindex)
1734 struct module_attribute *attr;
1737 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1740 get_modinfo(sechdrs,
1746 static void free_modinfo(struct module *mod)
1748 struct module_attribute *attr;
1751 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1757 #ifdef CONFIG_KALLSYMS
1759 /* lookup symbol in given range of kernel_symbols */
1760 static const struct kernel_symbol *lookup_symbol(const char *name,
1761 const struct kernel_symbol *start,
1762 const struct kernel_symbol *stop)
1764 const struct kernel_symbol *ks = start;
1765 for (; ks < stop; ks++)
1766 if (strcmp(ks->name, name) == 0)
1771 static int is_exported(const char *name, unsigned long value,
1772 const struct module *mod)
1774 const struct kernel_symbol *ks;
1776 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
1778 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1779 return ks != NULL && ks->value == value;
1783 static char elf_type(const Elf_Sym *sym,
1785 const char *secstrings,
1788 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1789 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1794 if (sym->st_shndx == SHN_UNDEF)
1796 if (sym->st_shndx == SHN_ABS)
1798 if (sym->st_shndx >= SHN_LORESERVE)
1800 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1802 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1803 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1804 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1806 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1811 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1812 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1817 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
1822 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1825 const Elf_Shdr *sec;
1827 if (src->st_shndx == SHN_UNDEF
1828 || src->st_shndx >= shnum
1832 sec = sechdrs + src->st_shndx;
1833 if (!(sec->sh_flags & SHF_ALLOC)
1834 #ifndef CONFIG_KALLSYMS_ALL
1835 || !(sec->sh_flags & SHF_EXECINSTR)
1837 || (sec->sh_entsize & INIT_OFFSET_MASK))
1843 static unsigned long layout_symtab(struct module *mod,
1845 unsigned int symindex,
1846 unsigned int strindex,
1847 const Elf_Ehdr *hdr,
1848 const char *secstrings,
1849 unsigned long *pstroffs,
1850 unsigned long *strmap)
1852 unsigned long symoffs;
1853 Elf_Shdr *symsect = sechdrs + symindex;
1854 Elf_Shdr *strsect = sechdrs + strindex;
1857 unsigned int i, nsrc, ndst;
1859 /* Put symbol section at end of init part of module. */
1860 symsect->sh_flags |= SHF_ALLOC;
1861 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
1862 symindex) | INIT_OFFSET_MASK;
1863 DEBUGP("\t%s\n", secstrings + symsect->sh_name);
1865 src = (void *)hdr + symsect->sh_offset;
1866 nsrc = symsect->sh_size / sizeof(*src);
1867 strtab = (void *)hdr + strsect->sh_offset;
1868 for (ndst = i = 1; i < nsrc; ++i, ++src)
1869 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
1870 unsigned int j = src->st_name;
1872 while(!__test_and_set_bit(j, strmap) && strtab[j])
1877 /* Append room for core symbols at end of core part. */
1878 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1879 mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
1881 /* Put string table section at end of init part of module. */
1882 strsect->sh_flags |= SHF_ALLOC;
1883 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1884 strindex) | INIT_OFFSET_MASK;
1885 DEBUGP("\t%s\n", secstrings + strsect->sh_name);
1887 /* Append room for core symbols' strings at end of core part. */
1888 *pstroffs = mod->core_size;
1889 __set_bit(0, strmap);
1890 mod->core_size += bitmap_weight(strmap, strsect->sh_size);
1895 static void add_kallsyms(struct module *mod,
1898 unsigned int symindex,
1899 unsigned int strindex,
1900 unsigned long symoffs,
1901 unsigned long stroffs,
1902 const char *secstrings,
1903 unsigned long *strmap)
1905 unsigned int i, ndst;
1910 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1911 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1912 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1914 /* Set types up while we still have access to sections. */
1915 for (i = 0; i < mod->num_symtab; i++)
1916 mod->symtab[i].st_info
1917 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1919 mod->core_symtab = dst = mod->module_core + symoffs;
1922 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
1923 if (!is_core_symbol(src, sechdrs, shnum))
1926 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
1929 mod->core_num_syms = ndst;
1931 mod->core_strtab = s = mod->module_core + stroffs;
1932 for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
1933 if (test_bit(i, strmap))
1934 *++s = mod->strtab[i];
1937 static inline unsigned long layout_symtab(struct module *mod,
1939 unsigned int symindex,
1940 unsigned int strindex,
1941 const Elf_Ehdr *hdr,
1942 const char *secstrings,
1943 unsigned long *pstroffs,
1944 unsigned long *strmap)
1949 static inline void add_kallsyms(struct module *mod,
1952 unsigned int symindex,
1953 unsigned int strindex,
1954 unsigned long symoffs,
1955 unsigned long stroffs,
1956 const char *secstrings,
1957 const unsigned long *strmap)
1960 #endif /* CONFIG_KALLSYMS */
1962 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1964 #ifdef CONFIG_DYNAMIC_DEBUG
1965 if (ddebug_add_module(debug, num, debug->modname))
1966 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1971 static void *module_alloc_update_bounds(unsigned long size)
1973 void *ret = module_alloc(size);
1976 /* Update module bounds. */
1977 if ((unsigned long)ret < module_addr_min)
1978 module_addr_min = (unsigned long)ret;
1979 if ((unsigned long)ret + size > module_addr_max)
1980 module_addr_max = (unsigned long)ret + size;
1985 #ifdef CONFIG_DEBUG_KMEMLEAK
1986 static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1987 Elf_Shdr *sechdrs, char *secstrings)
1991 /* only scan the sections containing data */
1992 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
1994 for (i = 1; i < hdr->e_shnum; i++) {
1995 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1997 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1998 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
2001 kmemleak_scan_area((void *)sechdrs[i].sh_addr,
2002 sechdrs[i].sh_size, GFP_KERNEL);
2006 static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
2007 Elf_Shdr *sechdrs, char *secstrings)
2012 /* Allocate and load the module: note that size of section 0 is always
2013 zero, and we rely on this for optional sections. */
2014 static noinline struct module *load_module(void __user *umod,
2016 const char __user *uargs)
2020 char *secstrings, *args, *modmagic, *strtab = NULL;
2023 unsigned int symindex = 0;
2024 unsigned int strindex = 0;
2025 unsigned int modindex, versindex, infoindex, pcpuindex;
2028 void *ptr = NULL; /* Stops spurious gcc warning */
2029 unsigned long symoffs, stroffs, *strmap;
2031 mm_segment_t old_fs;
2033 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2035 if (len < sizeof(*hdr))
2036 return ERR_PTR(-ENOEXEC);
2038 /* Suck in entire file: we'll want most of it. */
2039 /* vmalloc barfs on "unusual" numbers. Check here */
2040 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
2041 return ERR_PTR(-ENOMEM);
2043 if (copy_from_user(hdr, umod, len) != 0) {
2048 /* Sanity checks against insmoding binaries or wrong arch,
2049 weird elf version */
2050 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2051 || hdr->e_type != ET_REL
2052 || !elf_check_arch(hdr)
2053 || hdr->e_shentsize != sizeof(*sechdrs)) {
2058 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
2061 /* Convenience variables */
2062 sechdrs = (void *)hdr + hdr->e_shoff;
2063 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
2064 sechdrs[0].sh_addr = 0;
2066 for (i = 1; i < hdr->e_shnum; i++) {
2067 if (sechdrs[i].sh_type != SHT_NOBITS
2068 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
2071 /* Mark all sections sh_addr with their address in the
2073 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
2075 /* Internal symbols and strings. */
2076 if (sechdrs[i].sh_type == SHT_SYMTAB) {
2078 strindex = sechdrs[i].sh_link;
2079 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2081 #ifndef CONFIG_MODULE_UNLOAD
2082 /* Don't load .exit sections */
2083 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
2084 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
2088 modindex = find_sec(hdr, sechdrs, secstrings,
2089 ".gnu.linkonce.this_module");
2091 printk(KERN_WARNING "No module found in object\n");
2095 /* This is temporary: point mod into copy of data. */
2096 mod = (void *)sechdrs[modindex].sh_addr;
2098 if (symindex == 0) {
2099 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2105 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2106 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2107 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2109 /* Don't keep modinfo and version sections. */
2110 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2111 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2113 /* Check module struct version now, before we try to use module. */
2114 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2119 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2120 /* This is allowed: modprobe --force will invalidate it. */
2122 err = try_to_force_load(mod, "bad vermagic");
2125 } else if (!same_magic(modmagic, vermagic, versindex)) {
2126 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2127 mod->name, modmagic, vermagic);
2132 staging = get_modinfo(sechdrs, infoindex, "staging");
2134 add_taint_module(mod, TAINT_CRAP);
2135 printk(KERN_WARNING "%s: module is from the staging directory,"
2136 " the quality is unknown, you have been warned.\n",
2140 /* Now copy in args */
2141 args = strndup_user(uargs, ~0UL >> 1);
2143 err = PTR_ERR(args);
2147 strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
2148 * sizeof(long), GFP_KERNEL);
2154 if (find_module(mod->name)) {
2159 mod->state = MODULE_STATE_COMING;
2161 /* Allow arches to frob section contents and sizes. */
2162 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2167 /* We have a special allocation for this section. */
2168 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2169 sechdrs[pcpuindex].sh_addralign);
2172 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2175 /* Determine total sizes, and put offsets in sh_entsize. For now
2176 this is done generically; there doesn't appear to be any
2177 special cases for the architectures. */
2178 layout_sections(mod, hdr, sechdrs, secstrings);
2179 symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
2180 secstrings, &stroffs, strmap);
2182 /* Do the allocs. */
2183 ptr = module_alloc_update_bounds(mod->core_size);
2185 * The pointer to this block is stored in the module structure
2186 * which is inside the block. Just mark it as not being a
2189 kmemleak_not_leak(ptr);
2194 memset(ptr, 0, mod->core_size);
2195 mod->module_core = ptr;
2197 ptr = module_alloc_update_bounds(mod->init_size);
2199 * The pointer to this block is stored in the module structure
2200 * which is inside the block. This block doesn't need to be
2201 * scanned as it contains data and code that will be freed
2202 * after the module is initialized.
2204 kmemleak_ignore(ptr);
2205 if (!ptr && mod->init_size) {
2209 memset(ptr, 0, mod->init_size);
2210 mod->module_init = ptr;
2212 /* Transfer each section which specifies SHF_ALLOC */
2213 DEBUGP("final section addresses:\n");
2214 for (i = 0; i < hdr->e_shnum; i++) {
2217 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2220 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2221 dest = mod->module_init
2222 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2224 dest = mod->module_core + sechdrs[i].sh_entsize;
2226 if (sechdrs[i].sh_type != SHT_NOBITS)
2227 memcpy(dest, (void *)sechdrs[i].sh_addr,
2228 sechdrs[i].sh_size);
2229 /* Update sh_addr to point to copy in image. */
2230 sechdrs[i].sh_addr = (unsigned long)dest;
2231 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2233 /* Module has been moved. */
2234 mod = (void *)sechdrs[modindex].sh_addr;
2235 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2237 #if defined(CONFIG_MODULE_UNLOAD)
2238 mod->refptr = alloc_percpu(struct module_ref);
2244 /* Now we've moved module, initialize linked lists, etc. */
2245 module_unload_init(mod);
2247 /* add kobject, so we can reference it. */
2248 err = mod_sysfs_init(mod);
2252 /* Set up license info based on the info section */
2253 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2256 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2257 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2258 * using GPL-only symbols it needs.
2260 if (strcmp(mod->name, "ndiswrapper") == 0)
2261 add_taint(TAINT_PROPRIETARY_MODULE);
2263 /* driverloader was caught wrongly pretending to be under GPL */
2264 if (strcmp(mod->name, "driverloader") == 0)
2265 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2267 /* Set up MODINFO_ATTR fields */
2268 setup_modinfo(mod, sechdrs, infoindex);
2270 /* Fix up syms, so that st_value is a pointer to location. */
2271 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2276 /* Now we've got everything in the final locations, we can
2277 * find optional sections. */
2278 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2279 sizeof(*mod->kp), &mod->num_kp);
2280 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2281 sizeof(*mod->syms), &mod->num_syms);
2282 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2283 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2284 sizeof(*mod->gpl_syms),
2285 &mod->num_gpl_syms);
2286 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2287 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2288 "__ksymtab_gpl_future",
2289 sizeof(*mod->gpl_future_syms),
2290 &mod->num_gpl_future_syms);
2291 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2292 "__kcrctab_gpl_future");
2294 #ifdef CONFIG_UNUSED_SYMBOLS
2295 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2297 sizeof(*mod->unused_syms),
2298 &mod->num_unused_syms);
2299 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2300 "__kcrctab_unused");
2301 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2302 "__ksymtab_unused_gpl",
2303 sizeof(*mod->unused_gpl_syms),
2304 &mod->num_unused_gpl_syms);
2305 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2306 "__kcrctab_unused_gpl");
2308 #ifdef CONFIG_CONSTRUCTORS
2309 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2310 sizeof(*mod->ctors), &mod->num_ctors);
2313 #ifdef CONFIG_TRACEPOINTS
2314 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2316 sizeof(*mod->tracepoints),
2317 &mod->num_tracepoints);
2319 #ifdef CONFIG_EVENT_TRACING
2320 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2322 sizeof(*mod->trace_events),
2323 &mod->num_trace_events);
2325 * This section contains pointers to allocated objects in the trace
2326 * code and not scanning it leads to false positives.
2328 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2329 mod->num_trace_events, GFP_KERNEL);
2331 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2332 /* sechdrs[0].sh_size is always zero */
2333 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2335 sizeof(*mod->ftrace_callsites),
2336 &mod->num_ftrace_callsites);
2338 #ifdef CONFIG_MODVERSIONS
2339 if ((mod->num_syms && !mod->crcs)
2340 || (mod->num_gpl_syms && !mod->gpl_crcs)
2341 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2342 #ifdef CONFIG_UNUSED_SYMBOLS
2343 || (mod->num_unused_syms && !mod->unused_crcs)
2344 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2347 err = try_to_force_load(mod,
2348 "no versions for exported symbols");
2354 /* Now do relocations. */
2355 for (i = 1; i < hdr->e_shnum; i++) {
2356 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2357 unsigned int info = sechdrs[i].sh_info;
2359 /* Not a valid relocation section? */
2360 if (info >= hdr->e_shnum)
2363 /* Don't bother with non-allocated sections */
2364 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2367 if (sechdrs[i].sh_type == SHT_REL)
2368 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2369 else if (sechdrs[i].sh_type == SHT_RELA)
2370 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2376 /* Find duplicate symbols */
2377 err = verify_export_symbols(mod);
2381 /* Set up and sort exception table */
2382 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2383 sizeof(*mod->extable), &mod->num_exentries);
2384 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2386 /* Finally, copy percpu area over. */
2387 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
2388 sechdrs[pcpuindex].sh_size);
2390 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
2391 symoffs, stroffs, secstrings, strmap);
2396 struct _ddebug *debug;
2397 unsigned int num_debug;
2399 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2400 sizeof(*debug), &num_debug);
2402 dynamic_debug_setup(debug, num_debug);
2405 err = module_finalize(hdr, sechdrs, mod);
2409 /* flush the icache in correct context */
2414 * Flush the instruction cache, since we've played with text.
2415 * Do it before processing of module parameters, so the module
2416 * can provide parameter accessor functions of its own.
2418 if (mod->module_init)
2419 flush_icache_range((unsigned long)mod->module_init,
2420 (unsigned long)mod->module_init
2422 flush_icache_range((unsigned long)mod->module_core,
2423 (unsigned long)mod->module_core + mod->core_size);
2428 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2429 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2432 /* Now sew it into the lists so we can get lockdep and oops
2433 * info during argument parsing. Noone should access us, since
2434 * strong_try_module_get() will fail.
2435 * lockdep/oops can run asynchronous, so use the RCU list insertion
2436 * function to insert in a way safe to concurrent readers.
2437 * The mutex protects against concurrent writers.
2439 list_add_rcu(&mod->list, &modules);
2441 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2445 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2448 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2449 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2451 /* Get rid of temporary copy */
2454 trace_module_load(mod);
2460 /* Unlink carefully: kallsyms could be walking list. */
2461 list_del_rcu(&mod->list);
2462 synchronize_sched();
2463 module_arch_cleanup(mod);
2466 kobject_del(&mod->mkobj.kobj);
2467 kobject_put(&mod->mkobj.kobj);
2469 module_unload_free(mod);
2470 #if defined(CONFIG_MODULE_UNLOAD)
2471 free_percpu(mod->refptr);
2474 module_free(mod, mod->module_init);
2476 module_free(mod, mod->module_core);
2477 /* mod will be freed with core. Don't access it beyond this line! */
2479 percpu_modfree(mod);
2485 return ERR_PTR(err);
2488 printk(KERN_ERR "Module len %lu truncated\n", len);
2493 /* Call module constructors. */
2494 static void do_mod_ctors(struct module *mod)
2496 #ifdef CONFIG_CONSTRUCTORS
2499 for (i = 0; i < mod->num_ctors; i++)
2504 /* This is where the real work happens */
2505 SYSCALL_DEFINE3(init_module, void __user *, umod,
2506 unsigned long, len, const char __user *, uargs)
2511 /* Must have permission */
2512 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2515 /* Only one module load at a time, please */
2516 if (mutex_lock_interruptible(&module_mutex) != 0)
2519 /* Do all the hard work */
2520 mod = load_module(umod, len, uargs);
2522 mutex_unlock(&module_mutex);
2523 return PTR_ERR(mod);
2526 /* Drop lock so they can recurse */
2527 mutex_unlock(&module_mutex);
2529 blocking_notifier_call_chain(&module_notify_list,
2530 MODULE_STATE_COMING, mod);
2533 /* Start the module */
2534 if (mod->init != NULL)
2535 ret = do_one_initcall(mod->init);
2537 /* Init routine failed: abort. Try to protect us from
2538 buggy refcounters. */
2539 mod->state = MODULE_STATE_GOING;
2540 synchronize_sched();
2542 blocking_notifier_call_chain(&module_notify_list,
2543 MODULE_STATE_GOING, mod);
2544 mutex_lock(&module_mutex);
2546 mutex_unlock(&module_mutex);
2547 wake_up(&module_wq);
2552 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2553 "%s: loading module anyway...\n",
2554 __func__, mod->name, ret,
2559 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2560 mod->state = MODULE_STATE_LIVE;
2561 wake_up(&module_wq);
2562 blocking_notifier_call_chain(&module_notify_list,
2563 MODULE_STATE_LIVE, mod);
2565 /* We need to finish all async code before the module init sequence is done */
2566 async_synchronize_full();
2568 mutex_lock(&module_mutex);
2569 /* Drop initial reference. */
2571 trim_init_extable(mod);
2572 #ifdef CONFIG_KALLSYMS
2573 mod->num_symtab = mod->core_num_syms;
2574 mod->symtab = mod->core_symtab;
2575 mod->strtab = mod->core_strtab;
2577 module_free(mod, mod->module_init);
2578 mod->module_init = NULL;
2580 mod->init_text_size = 0;
2581 mutex_unlock(&module_mutex);
2586 static inline int within(unsigned long addr, void *start, unsigned long size)
2588 return ((void *)addr >= start && (void *)addr < start + size);
2591 #ifdef CONFIG_KALLSYMS
2593 * This ignores the intensely annoying "mapping symbols" found
2594 * in ARM ELF files: $a, $t and $d.
2596 static inline int is_arm_mapping_symbol(const char *str)
2598 return str[0] == '$' && strchr("atd", str[1])
2599 && (str[2] == '\0' || str[2] == '.');
2602 static const char *get_ksymbol(struct module *mod,
2604 unsigned long *size,
2605 unsigned long *offset)
2607 unsigned int i, best = 0;
2608 unsigned long nextval;
2610 /* At worse, next value is at end of module */
2611 if (within_module_init(addr, mod))
2612 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2614 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2616 /* Scan for closest preceeding symbol, and next symbol. (ELF
2617 starts real symbols at 1). */
2618 for (i = 1; i < mod->num_symtab; i++) {
2619 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2622 /* We ignore unnamed symbols: they're uninformative
2623 * and inserted at a whim. */
2624 if (mod->symtab[i].st_value <= addr
2625 && mod->symtab[i].st_value > mod->symtab[best].st_value
2626 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2627 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2629 if (mod->symtab[i].st_value > addr
2630 && mod->symtab[i].st_value < nextval
2631 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2632 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2633 nextval = mod->symtab[i].st_value;
2640 *size = nextval - mod->symtab[best].st_value;
2642 *offset = addr - mod->symtab[best].st_value;
2643 return mod->strtab + mod->symtab[best].st_name;
2646 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2647 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2648 const char *module_address_lookup(unsigned long addr,
2649 unsigned long *size,
2650 unsigned long *offset,
2655 const char *ret = NULL;
2658 list_for_each_entry_rcu(mod, &modules, list) {
2659 if (within_module_init(addr, mod) ||
2660 within_module_core(addr, mod)) {
2662 *modname = mod->name;
2663 ret = get_ksymbol(mod, addr, size, offset);
2667 /* Make a copy in here where it's safe */
2669 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2676 int lookup_module_symbol_name(unsigned long addr, char *symname)
2681 list_for_each_entry_rcu(mod, &modules, list) {
2682 if (within_module_init(addr, mod) ||
2683 within_module_core(addr, mod)) {
2686 sym = get_ksymbol(mod, addr, NULL, NULL);
2689 strlcpy(symname, sym, KSYM_NAME_LEN);
2699 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2700 unsigned long *offset, char *modname, char *name)
2705 list_for_each_entry_rcu(mod, &modules, list) {
2706 if (within_module_init(addr, mod) ||
2707 within_module_core(addr, mod)) {
2710 sym = get_ksymbol(mod, addr, size, offset);
2714 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2716 strlcpy(name, sym, KSYM_NAME_LEN);
2726 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2727 char *name, char *module_name, int *exported)
2732 list_for_each_entry_rcu(mod, &modules, list) {
2733 if (symnum < mod->num_symtab) {
2734 *value = mod->symtab[symnum].st_value;
2735 *type = mod->symtab[symnum].st_info;
2736 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2738 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2739 *exported = is_exported(name, *value, mod);
2743 symnum -= mod->num_symtab;
2749 static unsigned long mod_find_symname(struct module *mod, const char *name)
2753 for (i = 0; i < mod->num_symtab; i++)
2754 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2755 mod->symtab[i].st_info != 'U')
2756 return mod->symtab[i].st_value;
2760 /* Look for this name: can be of form module:name. */
2761 unsigned long module_kallsyms_lookup_name(const char *name)
2765 unsigned long ret = 0;
2767 /* Don't lock: we're in enough trouble already. */
2769 if ((colon = strchr(name, ':')) != NULL) {
2771 if ((mod = find_module(name)) != NULL)
2772 ret = mod_find_symname(mod, colon+1);
2775 list_for_each_entry_rcu(mod, &modules, list)
2776 if ((ret = mod_find_symname(mod, name)) != 0)
2783 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2784 struct module *, unsigned long),
2791 list_for_each_entry(mod, &modules, list) {
2792 for (i = 0; i < mod->num_symtab; i++) {
2793 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2794 mod, mod->symtab[i].st_value);
2801 #endif /* CONFIG_KALLSYMS */
2803 static char *module_flags(struct module *mod, char *buf)
2808 mod->state == MODULE_STATE_GOING ||
2809 mod->state == MODULE_STATE_COMING) {
2811 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2813 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2815 if (mod->taints & (1 << TAINT_CRAP))
2818 * TAINT_FORCED_RMMOD: could be added.
2819 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2823 /* Show a - for module-is-being-unloaded */
2824 if (mod->state == MODULE_STATE_GOING)
2826 /* Show a + for module-is-being-loaded */
2827 if (mod->state == MODULE_STATE_COMING)
2836 #ifdef CONFIG_PROC_FS
2837 /* Called by the /proc file system to return a list of modules. */
2838 static void *m_start(struct seq_file *m, loff_t *pos)
2840 mutex_lock(&module_mutex);
2841 return seq_list_start(&modules, *pos);
2844 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2846 return seq_list_next(p, &modules, pos);
2849 static void m_stop(struct seq_file *m, void *p)
2851 mutex_unlock(&module_mutex);
2854 static int m_show(struct seq_file *m, void *p)
2856 struct module *mod = list_entry(p, struct module, list);
2859 seq_printf(m, "%s %u",
2860 mod->name, mod->init_size + mod->core_size);
2861 print_unload_info(m, mod);
2863 /* Informative for users. */
2864 seq_printf(m, " %s",
2865 mod->state == MODULE_STATE_GOING ? "Unloading":
2866 mod->state == MODULE_STATE_COMING ? "Loading":
2868 /* Used by oprofile and other similar tools. */
2869 seq_printf(m, " 0x%p", mod->module_core);
2873 seq_printf(m, " %s", module_flags(mod, buf));
2875 seq_printf(m, "\n");
2879 /* Format: modulename size refcount deps address
2881 Where refcount is a number or -, and deps is a comma-separated list
2884 static const struct seq_operations modules_op = {
2891 static int modules_open(struct inode *inode, struct file *file)
2893 return seq_open(file, &modules_op);
2896 static const struct file_operations proc_modules_operations = {
2897 .open = modules_open,
2899 .llseek = seq_lseek,
2900 .release = seq_release,
2903 static int __init proc_modules_init(void)
2905 proc_create("modules", 0, NULL, &proc_modules_operations);
2908 module_init(proc_modules_init);
2911 /* Given an address, look for it in the module exception tables. */
2912 const struct exception_table_entry *search_module_extables(unsigned long addr)
2914 const struct exception_table_entry *e = NULL;
2918 list_for_each_entry_rcu(mod, &modules, list) {
2919 if (mod->num_exentries == 0)
2922 e = search_extable(mod->extable,
2923 mod->extable + mod->num_exentries - 1,
2930 /* Now, if we found one, we are running inside it now, hence
2931 we cannot unload the module, hence no refcnt needed. */
2936 * is_module_address - is this address inside a module?
2937 * @addr: the address to check.
2939 * See is_module_text_address() if you simply want to see if the address
2940 * is code (not data).
2942 bool is_module_address(unsigned long addr)
2947 ret = __module_address(addr) != NULL;
2954 * __module_address - get the module which contains an address.
2955 * @addr: the address.
2957 * Must be called with preempt disabled or module mutex held so that
2958 * module doesn't get freed during this.
2960 struct module *__module_address(unsigned long addr)
2964 if (addr < module_addr_min || addr > module_addr_max)
2967 list_for_each_entry_rcu(mod, &modules, list)
2968 if (within_module_core(addr, mod)
2969 || within_module_init(addr, mod))
2973 EXPORT_SYMBOL_GPL(__module_address);
2976 * is_module_text_address - is this address inside module code?
2977 * @addr: the address to check.
2979 * See is_module_address() if you simply want to see if the address is
2980 * anywhere in a module. See kernel_text_address() for testing if an
2981 * address corresponds to kernel or module code.
2983 bool is_module_text_address(unsigned long addr)
2988 ret = __module_text_address(addr) != NULL;
2995 * __module_text_address - get the module whose code contains an address.
2996 * @addr: the address.
2998 * Must be called with preempt disabled or module mutex held so that
2999 * module doesn't get freed during this.
3001 struct module *__module_text_address(unsigned long addr)
3003 struct module *mod = __module_address(addr);
3005 /* Make sure it's within the text section. */
3006 if (!within(addr, mod->module_init, mod->init_text_size)
3007 && !within(addr, mod->module_core, mod->core_text_size))
3012 EXPORT_SYMBOL_GPL(__module_text_address);
3014 /* Don't grab lock, we're oopsing. */
3015 void print_modules(void)
3020 printk(KERN_DEFAULT "Modules linked in:");
3021 /* Most callers should already have preempt disabled, but make sure */
3023 list_for_each_entry_rcu(mod, &modules, list)
3024 printk(" %s%s", mod->name, module_flags(mod, buf));
3026 if (last_unloaded_module[0])
3027 printk(" [last unloaded: %s]", last_unloaded_module);
3031 #ifdef CONFIG_MODVERSIONS
3032 /* Generate the signature for all relevant module structures here.
3033 * If these change, we don't want to try to parse the module. */
3034 void module_layout(struct module *mod,
3035 struct modversion_info *ver,
3036 struct kernel_param *kp,
3037 struct kernel_symbol *ks,
3038 struct tracepoint *tp)
3041 EXPORT_SYMBOL(module_layout);
3044 #ifdef CONFIG_TRACEPOINTS
3045 void module_update_tracepoints(void)
3049 mutex_lock(&module_mutex);
3050 list_for_each_entry(mod, &modules, list)
3052 tracepoint_update_probe_range(mod->tracepoints,
3053 mod->tracepoints + mod->num_tracepoints);
3054 mutex_unlock(&module_mutex);
3058 * Returns 0 if current not found.
3059 * Returns 1 if current found.
3061 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3063 struct module *iter_mod;
3066 mutex_lock(&module_mutex);
3067 list_for_each_entry(iter_mod, &modules, list) {
3068 if (!iter_mod->taints) {
3070 * Sorted module list
3072 if (iter_mod < iter->module)
3074 else if (iter_mod > iter->module)
3075 iter->tracepoint = NULL;
3076 found = tracepoint_get_iter_range(&iter->tracepoint,
3077 iter_mod->tracepoints,
3078 iter_mod->tracepoints
3079 + iter_mod->num_tracepoints);
3081 iter->module = iter_mod;
3086 mutex_unlock(&module_mutex);