2 * Copyright (c) 1997-2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include "opt_hwpmc_hooks.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/sysproto.h>
39 #include <sys/sysent.h>
43 #include <sys/mutex.h>
45 #include <sys/module.h>
46 #include <sys/mount.h>
47 #include <sys/linker.h>
48 #include <sys/eventhandler.h>
49 #include <sys/fcntl.h>
51 #include <sys/libkern.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysctl.h>
59 #include <security/mac/mac_framework.h>
61 #include "linker_if.h"
64 #include <sys/pmckern.h>
69 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RW | CTLFLAG_TUN,
70 &kld_debug, 0, "Set various levels of KLD debug");
71 TUNABLE_INT("debug.kld_debug", &kld_debug);
75 * static char *linker_search_path(const char *name, struct mod_depend
78 static const char *linker_basename(const char *path);
81 * Find a currently loaded file given its filename.
83 static linker_file_t linker_find_file_by_name(const char* _filename);
86 * Find a currently loaded file given its file id.
88 static linker_file_t linker_find_file_by_id(int _fileid);
90 /* Metadata from the static kernel */
91 SET_DECLARE(modmetadata_set, struct mod_metadata);
93 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
95 linker_file_t linker_kernel_file;
97 static struct sx kld_sx; /* kernel linker lock */
100 * Load counter used by clients to determine if a linker file has been
101 * re-loaded. This counter is incremented for each file load.
105 static linker_class_list_t classes;
106 static linker_file_list_t linker_files;
107 static int next_file_id = 1;
108 static int linker_no_more_classes = 0;
110 #define LINKER_GET_NEXT_FILE_ID(a) do { \
111 linker_file_t lftmp; \
114 sx_assert(&kld_sx, SA_XLOCKED); \
116 TAILQ_FOREACH(lftmp, &linker_files, link) { \
117 if (next_file_id == lftmp->id) { \
122 (a) = next_file_id; \
126 /* XXX wrong name; we're looking at version provision tags here, not modules */
127 typedef TAILQ_HEAD(, modlist) modlisthead_t;
129 TAILQ_ENTRY(modlist) link; /* chain together all modules */
130 linker_file_t container;
134 typedef struct modlist *modlist_t;
135 static modlisthead_t found_modules;
137 static int linker_file_add_dependency(linker_file_t file,
139 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
140 const char* name, int deps);
141 static int linker_load_module(const char *kldname,
142 const char *modname, struct linker_file *parent,
143 struct mod_depend *verinfo, struct linker_file **lfpp);
144 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
147 linker_init(void *arg)
150 sx_init(&kld_sx, "kernel linker");
151 TAILQ_INIT(&classes);
152 TAILQ_INIT(&linker_files);
155 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
158 linker_stop_class_add(void *arg)
161 linker_no_more_classes = 1;
164 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
167 linker_add_class(linker_class_t lc)
171 * We disallow any class registration past SI_ORDER_ANY
172 * of SI_SUB_KLD. We bump the reference count to keep the
173 * ops from being freed.
175 if (linker_no_more_classes == 1)
177 kobj_class_compile((kobj_class_t) lc);
178 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
179 TAILQ_INSERT_TAIL(&classes, lc, link);
184 linker_file_sysinit(linker_file_t lf)
186 struct sysinit **start, **stop, **sipp, **xipp, *save;
188 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
191 sx_assert(&kld_sx, SA_XLOCKED);
193 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
196 * Perform a bubble sort of the system initialization objects by
197 * their subsystem (primary key) and order (secondary key).
199 * Since some things care about execution order, this is the operation
200 * which ensures continued function.
202 for (sipp = start; sipp < stop; sipp++) {
203 for (xipp = sipp + 1; xipp < stop; xipp++) {
204 if ((*sipp)->subsystem < (*xipp)->subsystem ||
205 ((*sipp)->subsystem == (*xipp)->subsystem &&
206 (*sipp)->order <= (*xipp)->order))
215 * Traverse the (now) ordered list of system initialization tasks.
216 * Perform each task, and continue on to the next task.
220 for (sipp = start; sipp < stop; sipp++) {
221 if ((*sipp)->subsystem == SI_SUB_DUMMY)
222 continue; /* skip dummy task(s) */
225 (*((*sipp)->func)) ((*sipp)->udata);
232 linker_file_sysuninit(linker_file_t lf)
234 struct sysinit **start, **stop, **sipp, **xipp, *save;
236 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
239 sx_assert(&kld_sx, SA_XLOCKED);
241 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
246 * Perform a reverse bubble sort of the system initialization objects
247 * by their subsystem (primary key) and order (secondary key).
249 * Since some things care about execution order, this is the operation
250 * which ensures continued function.
252 for (sipp = start; sipp < stop; sipp++) {
253 for (xipp = sipp + 1; xipp < stop; xipp++) {
254 if ((*sipp)->subsystem > (*xipp)->subsystem ||
255 ((*sipp)->subsystem == (*xipp)->subsystem &&
256 (*sipp)->order >= (*xipp)->order))
265 * Traverse the (now) ordered list of system initialization tasks.
266 * Perform each task, and continue on to the next task.
270 for (sipp = start; sipp < stop; sipp++) {
271 if ((*sipp)->subsystem == SI_SUB_DUMMY)
272 continue; /* skip dummy task(s) */
275 (*((*sipp)->func)) ((*sipp)->udata);
282 linker_file_register_sysctls(linker_file_t lf)
284 struct sysctl_oid **start, **stop, **oidp;
287 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
290 sx_assert(&kld_sx, SA_XLOCKED);
292 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
297 for (oidp = start; oidp < stop; oidp++)
298 sysctl_register_oid(*oidp);
304 linker_file_unregister_sysctls(linker_file_t lf)
306 struct sysctl_oid **start, **stop, **oidp;
308 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
309 " for %s\n", lf->filename));
311 sx_assert(&kld_sx, SA_XLOCKED);
313 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
318 for (oidp = start; oidp < stop; oidp++)
319 sysctl_unregister_oid(*oidp);
325 linker_file_register_modules(linker_file_t lf)
327 struct mod_metadata **start, **stop, **mdp;
328 const moduledata_t *moddata;
329 int first_error, error;
331 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
332 " in %s\n", lf->filename));
334 sx_assert(&kld_sx, SA_XLOCKED);
336 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
339 * This fallback should be unnecessary, but if we get booted
340 * from boot2 instead of loader and we are missing our
341 * metadata then we have to try the best we can.
343 if (lf == linker_kernel_file) {
344 start = SET_BEGIN(modmetadata_set);
345 stop = SET_LIMIT(modmetadata_set);
350 for (mdp = start; mdp < stop; mdp++) {
351 if ((*mdp)->md_type != MDT_MODULE)
353 moddata = (*mdp)->md_data;
354 KLD_DPF(FILE, ("Registering module %s in %s\n",
355 moddata->name, lf->filename));
356 error = module_register(moddata, lf);
358 printf("Module %s failed to register: %d\n",
359 moddata->name, error);
360 if (first_error == 0)
364 return (first_error);
368 linker_init_kernel_modules(void)
372 linker_file_register_modules(linker_kernel_file);
376 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
380 linker_load_file(const char *filename, linker_file_t *result)
384 int foundfile, error, modules;
386 /* Refuse to load modules if securelevel raised */
387 if (prison0.pr_securelevel > 0)
390 sx_assert(&kld_sx, SA_XLOCKED);
391 lf = linker_find_file_by_name(filename);
393 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
394 " incrementing refs\n", filename));
403 * We do not need to protect (lock) classes here because there is
404 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
405 * and there is no class deregistration mechanism at this time.
407 TAILQ_FOREACH(lc, &classes, link) {
408 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
410 error = LINKER_LOAD_FILE(lc, filename, &lf);
412 * If we got something other than ENOENT, then it exists but
413 * we cannot load it for some other reason.
418 error = linker_file_register_modules(lf);
419 if (error == EEXIST) {
420 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
423 modules = !TAILQ_EMPTY(&lf->modules);
424 linker_file_register_sysctls(lf);
425 linker_file_sysinit(lf);
426 lf->flags |= LINKER_FILE_LINKED;
429 * If all of the modules in this file failed
430 * to load, unload the file and return an
433 if (modules && TAILQ_EMPTY(&lf->modules)) {
434 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
437 EVENTHANDLER_INVOKE(kld_load, lf);
443 * Less than ideal, but tells the user whether it failed to load or
444 * the module was not found.
449 * If the file type has not been recognized by the last try
450 * printout a message before to fail.
453 printf("linker_load_file: Unsupported file type\n");
456 * Format not recognized or otherwise unloadable.
457 * When loading a module that is statically built into
458 * the kernel EEXIST percolates back up as the return
459 * value. Preserve this so that apps like sysinstall
460 * can recognize this special case and not post bogus
466 error = ENOENT; /* Nothing found */
471 linker_reference_module(const char *modname, struct mod_depend *verinfo,
472 linker_file_t *result)
478 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
479 *result = mod->container;
485 error = linker_load_module(NULL, modname, NULL, verinfo, result);
491 linker_release_module(const char *modname, struct mod_depend *verinfo,
499 KASSERT(modname != NULL,
500 ("linker_release_module: no file or name"));
501 mod = modlist_lookup2(modname, verinfo);
508 KASSERT(modname == NULL && verinfo == NULL,
509 ("linker_release_module: both file and name"));
510 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
516 linker_find_file_by_name(const char *filename)
521 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
522 sprintf(koname, "%s.ko", filename);
524 sx_assert(&kld_sx, SA_XLOCKED);
525 TAILQ_FOREACH(lf, &linker_files, link) {
526 if (strcmp(lf->filename, koname) == 0)
528 if (strcmp(lf->filename, filename) == 0)
531 free(koname, M_LINKER);
536 linker_find_file_by_id(int fileid)
540 sx_assert(&kld_sx, SA_XLOCKED);
541 TAILQ_FOREACH(lf, &linker_files, link)
542 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
548 linker_file_foreach(linker_predicate_t *predicate, void *context)
554 TAILQ_FOREACH(lf, &linker_files, link) {
555 retval = predicate(lf, context);
564 linker_make_file(const char *pathname, linker_class_t lc)
567 const char *filename;
570 sx_assert(&kld_sx, SA_XLOCKED);
571 filename = linker_basename(pathname);
573 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
574 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
580 lf->filename = strdup(filename, M_LINKER);
581 lf->pathname = strdup(pathname, M_LINKER);
582 LINKER_GET_NEXT_FILE_ID(lf->id);
585 lf->loadcnt = ++loadcnt;
586 STAILQ_INIT(&lf->common);
587 TAILQ_INIT(&lf->modules);
588 TAILQ_INSERT_TAIL(&linker_files, lf, link);
593 linker_file_unload(linker_file_t file, int flags)
596 modlist_t ml, nextml;
597 struct common_symbol *cp;
600 /* Refuse to unload modules if securelevel raised. */
601 if (prison0.pr_securelevel > 0)
604 sx_assert(&kld_sx, SA_XLOCKED);
605 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
607 /* Easy case of just dropping a reference. */
608 if (file->refs > 1) {
613 /* Give eventhandlers a chance to prevent the unload. */
615 EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
619 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
620 " informing modules\n"));
623 * Quiesce all the modules to give them a chance to veto the unload.
626 for (mod = TAILQ_FIRST(&file->modules); mod;
627 mod = module_getfnext(mod)) {
629 error = module_quiesce(mod);
630 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
631 KLD_DPF(FILE, ("linker_file_unload: module %s"
632 " vetoed unload\n", module_getname(mod)));
634 * XXX: Do we need to tell all the quiesced modules
635 * that they can resume work now via a new module
645 * Inform any modules associated with this file that they are
649 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
650 next = module_getfnext(mod);
654 * Give the module a chance to veto the unload.
656 if ((error = module_unload(mod)) != 0) {
659 KLD_DPF(FILE, ("linker_file_unload: module %s"
660 " failed unload\n", module_getname(mod)));
670 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
671 if (ml->container == file) {
672 TAILQ_REMOVE(&found_modules, ml, link);
678 * Don't try to run SYSUNINITs if we are unloaded due to a
681 if (file->flags & LINKER_FILE_LINKED) {
682 file->flags &= ~LINKER_FILE_LINKED;
683 linker_file_sysuninit(file);
684 linker_file_unregister_sysctls(file);
686 TAILQ_REMOVE(&linker_files, file, link);
689 for (i = 0; i < file->ndeps; i++)
690 linker_file_unload(file->deps[i], flags);
691 free(file->deps, M_LINKER);
694 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
695 STAILQ_REMOVE_HEAD(&file->common, link);
701 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
704 if (file->filename) {
705 free(file->filename, M_LINKER);
706 file->filename = NULL;
708 if (file->pathname) {
709 free(file->pathname, M_LINKER);
710 file->pathname = NULL;
712 kobj_delete((kobj_t) file, M_LINKER);
717 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
719 return (LINKER_CTF_GET(file, lc));
723 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
725 linker_file_t *newdeps;
727 sx_assert(&kld_sx, SA_XLOCKED);
728 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
729 M_LINKER, M_WAITOK | M_ZERO);
730 file->deps[file->ndeps] = dep;
732 KLD_DPF(FILE, ("linker_file_add_dependency:"
733 " adding %s as dependency for %s\n",
734 dep->filename, file->filename));
739 * Locate a linker set and its contents. This is a helper function to avoid
740 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
741 * This function is used in this file so we can avoid having lots of (void **)
745 linker_file_lookup_set(linker_file_t file, const char *name,
746 void *firstp, void *lastp, int *countp)
749 sx_assert(&kld_sx, SA_LOCKED);
750 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
754 * List all functions in a file.
757 linker_file_function_listall(linker_file_t lf,
758 linker_function_nameval_callback_t callback_func, void *arg)
760 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
764 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
769 locked = sx_xlocked(&kld_sx);
772 sym = linker_file_lookup_symbol_internal(file, name, deps);
779 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
783 linker_symval_t symval;
785 size_t common_size = 0;
788 sx_assert(&kld_sx, SA_XLOCKED);
789 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
792 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
793 LINKER_SYMBOL_VALUES(file, sym, &symval);
794 if (symval.value == 0)
796 * For commons, first look them up in the
797 * dependencies and only allocate space if not found
800 common_size = symval.size;
802 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
803 ".value=%p\n", symval.value));
804 return (symval.value);
808 for (i = 0; i < file->ndeps; i++) {
809 address = linker_file_lookup_symbol_internal(
810 file->deps[i], name, 0);
812 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
813 " deps value=%p\n", address));
818 if (common_size > 0) {
820 * This is a common symbol which was not found in the
821 * dependencies. We maintain a simple common symbol table in
824 struct common_symbol *cp;
826 STAILQ_FOREACH(cp, &file->common, link) {
827 if (strcmp(cp->name, name) == 0) {
828 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
829 " old common value=%p\n", cp->address));
830 return (cp->address);
834 * Round the symbol size up to align.
836 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
837 cp = malloc(sizeof(struct common_symbol)
838 + common_size + strlen(name) + 1, M_LINKER,
840 cp->address = (caddr_t)(cp + 1);
841 cp->name = cp->address + common_size;
842 strcpy(cp->name, name);
843 bzero(cp->address, common_size);
844 STAILQ_INSERT_TAIL(&file->common, cp, link);
846 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
847 " value=%p\n", cp->address));
848 return (cp->address);
850 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
855 * Both DDB and stack(9) rely on the kernel linker to provide forward and
856 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
857 * do this in a lockfree manner. We provide a set of internal helper
858 * routines to perform these operations without locks, and then wrappers that
861 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
865 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
869 TAILQ_FOREACH(lf, &linker_files, link) {
870 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
878 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
881 c_linker_sym_t best, es;
882 u_long diff, bestdiff, off;
885 off = (uintptr_t)value;
887 TAILQ_FOREACH(lf, &linker_files, link) {
888 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
890 if (es != 0 && diff < bestdiff) {
909 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
913 TAILQ_FOREACH(lf, &linker_files, link) {
914 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
921 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
924 linker_symval_t symval;
929 error = linker_debug_search_symbol(value, &sym, offset);
932 error = linker_debug_symbol_values(sym, &symval);
935 strlcpy(buf, symval.name, buflen);
940 * DDB Helpers. DDB has to look across multiple files with their own symbol
941 * tables and string tables.
943 * Note that we do not obey list locking protocols here. We really don't need
944 * DDB to hang because somebody's got the lock held. We'll take the chance
945 * that the files list is inconsistant instead.
949 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
952 return (linker_debug_lookup(symstr, sym));
957 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
960 return (linker_debug_search_symbol(value, sym, diffp));
964 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
967 return (linker_debug_symbol_values(sym, symval));
971 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
975 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
979 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
980 * obey locking protocols, and offer a significantly less complex interface.
983 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
989 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
998 kern_kldload(struct thread *td, const char *file, int *fileid)
1000 const char *kldname, *modname;
1004 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1007 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1011 * It is possible that kldloaded module will attach a new ifnet,
1012 * so vnet context must be set when this ocurs.
1014 CURVNET_SET(TD_TO_VNET(td));
1017 * If file does not contain a qualified name or any dot in it
1018 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1021 if (strchr(file, '/') || strchr(file, '.')) {
1030 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1032 sx_xunlock(&kld_sx);
1038 sx_xunlock(&kld_sx);
1046 sys_kldload(struct thread *td, struct kldload_args *uap)
1048 char *pathname = NULL;
1051 td->td_retval[0] = -1;
1053 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1054 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1056 error = kern_kldload(td, pathname, &fileid);
1058 td->td_retval[0] = fileid;
1060 free(pathname, M_TEMP);
1065 kern_kldunload(struct thread *td, int fileid, int flags)
1070 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1073 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1076 CURVNET_SET(TD_TO_VNET(td));
1078 lf = linker_find_file_by_id(fileid);
1080 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1082 if (lf->userrefs == 0) {
1084 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1086 printf("kldunload: attempt to unload file that was"
1087 " loaded by the kernel\n");
1091 error = linker_file_unload(lf, flags);
1097 sx_xunlock(&kld_sx);
1104 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1107 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1111 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1114 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1115 uap->flags != LINKER_UNLOAD_FORCE)
1117 return (kern_kldunload(td, uap->fileid, uap->flags));
1121 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1124 const char *filename;
1129 error = mac_kld_check_stat(td->td_ucred);
1134 td->td_retval[0] = -1;
1136 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1137 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1140 filename = linker_basename(pathname);
1142 lf = linker_find_file_by_name(filename);
1144 td->td_retval[0] = lf->id;
1147 sx_xunlock(&kld_sx);
1149 free(pathname, M_TEMP);
1154 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1160 error = mac_kld_check_stat(td->td_ucred);
1166 if (uap->fileid == 0)
1167 lf = TAILQ_FIRST(&linker_files);
1169 lf = linker_find_file_by_id(uap->fileid);
1174 lf = TAILQ_NEXT(lf, link);
1177 /* Skip partially loaded files. */
1178 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1179 lf = TAILQ_NEXT(lf, link);
1182 td->td_retval[0] = lf->id;
1184 td->td_retval[0] = 0;
1186 sx_xunlock(&kld_sx);
1191 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1193 struct kld_file_stat stat;
1197 * Check the version of the user's structure.
1199 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1202 if (version != sizeof(struct kld_file_stat_1) &&
1203 version != sizeof(struct kld_file_stat))
1206 error = kern_kldstat(td, uap->fileid, &stat);
1209 return (copyout(&stat, uap->stat, version));
1213 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1220 error = mac_kld_check_stat(td->td_ucred);
1226 lf = linker_find_file_by_id(fileid);
1228 sx_xunlock(&kld_sx);
1232 /* Version 1 fields: */
1233 namelen = strlen(lf->filename) + 1;
1234 if (namelen > MAXPATHLEN)
1235 namelen = MAXPATHLEN;
1236 bcopy(lf->filename, &stat->name[0], namelen);
1237 stat->refs = lf->refs;
1239 stat->address = lf->address;
1240 stat->size = lf->size;
1241 /* Version 2 fields: */
1242 namelen = strlen(lf->pathname) + 1;
1243 if (namelen > MAXPATHLEN)
1244 namelen = MAXPATHLEN;
1245 bcopy(lf->pathname, &stat->pathname[0], namelen);
1246 sx_xunlock(&kld_sx);
1248 td->td_retval[0] = 0;
1253 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1260 error = mac_kld_check_stat(td->td_ucred);
1266 lf = linker_find_file_by_id(uap->fileid);
1269 mp = TAILQ_FIRST(&lf->modules);
1271 td->td_retval[0] = module_getid(mp);
1273 td->td_retval[0] = 0;
1277 sx_xunlock(&kld_sx);
1282 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1284 char *symstr = NULL;
1286 linker_symval_t symval;
1288 struct kld_sym_lookup lookup;
1292 error = mac_kld_check_stat(td->td_ucred);
1297 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1299 if (lookup.version != sizeof(lookup) ||
1300 uap->cmd != KLDSYM_LOOKUP)
1302 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1303 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1306 if (uap->fileid != 0) {
1307 lf = linker_find_file_by_id(uap->fileid);
1310 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1311 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1312 lookup.symvalue = (uintptr_t) symval.value;
1313 lookup.symsize = symval.size;
1314 error = copyout(&lookup, uap->data, sizeof(lookup));
1318 TAILQ_FOREACH(lf, &linker_files, link) {
1319 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1320 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1321 lookup.symvalue = (uintptr_t)symval.value;
1322 lookup.symsize = symval.size;
1323 error = copyout(&lookup, uap->data,
1331 sx_xunlock(&kld_sx);
1333 free(symstr, M_TEMP);
1338 * Preloaded module support
1342 modlist_lookup(const char *name, int ver)
1346 TAILQ_FOREACH(mod, &found_modules, link) {
1347 if (strcmp(mod->name, name) == 0 &&
1348 (ver == 0 || mod->version == ver))
1355 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1357 modlist_t mod, bestmod;
1360 if (verinfo == NULL)
1361 return (modlist_lookup(name, 0));
1363 TAILQ_FOREACH(mod, &found_modules, link) {
1364 if (strcmp(mod->name, name) != 0)
1367 if (ver == verinfo->md_ver_preferred)
1369 if (ver >= verinfo->md_ver_minimum &&
1370 ver <= verinfo->md_ver_maximum &&
1371 (bestmod == NULL || ver > bestmod->version))
1378 modlist_newmodule(const char *modname, int version, linker_file_t container)
1382 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1384 panic("no memory for module list");
1385 mod->container = container;
1386 mod->name = modname;
1387 mod->version = version;
1388 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1393 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1394 struct mod_metadata **stop, int preload)
1396 struct mod_metadata *mp, **mdp;
1397 const char *modname;
1400 for (mdp = start; mdp < stop; mdp++) {
1402 if (mp->md_type != MDT_VERSION)
1404 modname = mp->md_cval;
1405 ver = ((struct mod_version *)mp->md_data)->mv_version;
1406 if (modlist_lookup(modname, ver) != NULL) {
1407 printf("module %s already present!\n", modname);
1408 /* XXX what can we do? this is a build error. :-( */
1411 modlist_newmodule(modname, ver, lf);
1416 linker_preload(void *arg)
1419 const char *modname, *nmodname;
1421 linker_file_t lf, nlf;
1424 linker_file_list_t loaded_files;
1425 linker_file_list_t depended_files;
1426 struct mod_metadata *mp, *nmp;
1427 struct mod_metadata **start, **stop, **mdp, **nmdp;
1428 struct mod_depend *verinfo;
1432 struct sysinit **si_start, **si_stop;
1434 TAILQ_INIT(&loaded_files);
1435 TAILQ_INIT(&depended_files);
1436 TAILQ_INIT(&found_modules);
1441 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1442 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1443 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1444 if (modname == NULL) {
1445 printf("Preloaded module at %p does not have a"
1446 " name!\n", modptr);
1449 if (modtype == NULL) {
1450 printf("Preloaded module at %p does not have a type!\n",
1455 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1458 TAILQ_FOREACH(lc, &classes, link) {
1459 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1465 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1469 * First get a list of stuff in the kernel.
1471 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1473 linker_addmodules(linker_kernel_file, start, stop, 1);
1476 * This is a once-off kinky bubble sort to resolve relocation
1477 * dependency requirements.
1480 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1481 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1484 * First, look to see if we would successfully link with this
1487 resolves = 1; /* unless we know otherwise */
1489 for (mdp = start; mdp < stop; mdp++) {
1491 if (mp->md_type != MDT_DEPEND)
1493 modname = mp->md_cval;
1494 verinfo = mp->md_data;
1495 for (nmdp = start; nmdp < stop; nmdp++) {
1497 if (nmp->md_type != MDT_VERSION)
1499 nmodname = nmp->md_cval;
1500 if (strcmp(modname, nmodname) == 0)
1503 if (nmdp < stop) /* it's a self reference */
1507 * ok, the module isn't here yet, we
1510 if (modlist_lookup2(modname, verinfo) == NULL)
1515 * OK, if we found our modules, we can link. So, "provide"
1516 * the modules inside and add it to the end of the link order
1521 for (mdp = start; mdp < stop; mdp++) {
1523 if (mp->md_type != MDT_VERSION)
1525 modname = mp->md_cval;
1526 nver = ((struct mod_version *)
1527 mp->md_data)->mv_version;
1528 if (modlist_lookup(modname,
1530 printf("module %s already"
1531 " present!\n", modname);
1532 TAILQ_REMOVE(&loaded_files,
1534 linker_file_unload(lf,
1535 LINKER_UNLOAD_FORCE);
1536 /* we changed tailq next ptr */
1539 modlist_newmodule(modname, nver, lf);
1542 TAILQ_REMOVE(&loaded_files, lf, loaded);
1543 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1545 * Since we provided modules, we need to restart the
1546 * sort so that the previous files that depend on us
1547 * have a chance. Also, we've busted the tailq next
1548 * pointer with the REMOVE.
1555 * At this point, we check to see what could not be resolved..
1557 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1558 TAILQ_REMOVE(&loaded_files, lf, loaded);
1559 printf("KLD file %s is missing dependencies\n", lf->filename);
1560 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1564 * We made it. Finish off the linking in the order we determined.
1566 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1567 if (linker_kernel_file) {
1568 linker_kernel_file->refs++;
1569 error = linker_file_add_dependency(lf,
1570 linker_kernel_file);
1572 panic("cannot add dependency");
1574 lf->userrefs++; /* so we can (try to) kldunload it */
1575 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1578 for (mdp = start; mdp < stop; mdp++) {
1580 if (mp->md_type != MDT_DEPEND)
1582 modname = mp->md_cval;
1583 verinfo = mp->md_data;
1584 mod = modlist_lookup2(modname, verinfo);
1586 printf("KLD file %s - cannot find "
1587 "dependency \"%s\"\n",
1588 lf->filename, modname);
1591 /* Don't count self-dependencies */
1592 if (lf == mod->container)
1594 mod->container->refs++;
1595 error = linker_file_add_dependency(lf,
1598 panic("cannot add dependency");
1602 * Now do relocation etc using the symbol search paths
1603 * established by the dependencies
1605 error = LINKER_LINK_PRELOAD_FINISH(lf);
1607 printf("KLD file %s - could not finalize loading\n",
1611 linker_file_register_modules(lf);
1612 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1613 &si_stop, NULL) == 0)
1614 sysinit_add(si_start, si_stop);
1615 linker_file_register_sysctls(lf);
1616 lf->flags |= LINKER_FILE_LINKED;
1619 TAILQ_REMOVE(&depended_files, lf, loaded);
1620 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1622 sx_xunlock(&kld_sx);
1623 /* woohoo! we made it! */
1626 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1629 * Search for a not-loaded module by name.
1631 * Modules may be found in the following locations:
1633 * - preloaded (result is just the module name) - on disk (result is full path
1636 * If the module name is qualified in any way (contains path, etc.) the we
1637 * simply return a copy of it.
1639 * The search path can be manipulated via sysctl. Note that we use the ';'
1640 * character as a separator to be consistent with the bootloader.
1643 static char linker_hintfile[] = "linker.hints";
1644 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1646 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1647 sizeof(linker_path), "module load search path");
1649 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1651 static char *linker_ext_list[] = {
1658 * Check if file actually exists either with or without extension listed in
1659 * the linker_ext_list. (probably should be generic for the rest of the
1663 linker_lookup_file(const char *path, int pathlen, const char *name,
1664 int namelen, struct vattr *vap)
1666 struct nameidata nd;
1667 struct thread *td = curthread; /* XXX */
1668 char *result, **cpp, *sep;
1669 int error, len, extlen, reclen, flags;
1673 for (cpp = linker_ext_list; *cpp; cpp++) {
1678 extlen++; /* trailing '\0' */
1679 sep = (path[pathlen - 1] != '/') ? "/" : "";
1681 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1682 result = malloc(reclen, M_LINKER, M_WAITOK);
1683 for (cpp = linker_ext_list; *cpp; cpp++) {
1684 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1685 namelen, name, *cpp);
1687 * Attempt to open the file, and return the path if
1688 * we succeed and it's a regular file.
1690 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1692 error = vn_open(&nd, &flags, 0, NULL);
1694 NDFREE(&nd, NDF_ONLY_PNBUF);
1695 type = nd.ni_vp->v_type;
1697 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1698 VOP_UNLOCK(nd.ni_vp, 0);
1699 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1704 free(result, M_LINKER);
1708 #define INT_ALIGN(base, ptr) ptr = \
1709 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1712 * Lookup KLD which contains requested module in the "linker.hints" file. If
1713 * version specification is available, then try to find the best KLD.
1714 * Otherwise just find the latest one.
1717 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1718 int modnamelen, struct mod_depend *verinfo)
1720 struct thread *td = curthread; /* XXX */
1721 struct ucred *cred = td ? td->td_ucred : NULL;
1722 struct nameidata nd;
1723 struct vattr vattr, mattr;
1724 u_char *hints = NULL;
1725 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1726 int error, ival, bestver, *intp, found, flags, clen, blen;
1730 bestver = found = 0;
1732 sep = (path[pathlen - 1] != '/') ? "/" : "";
1733 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1735 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1736 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1739 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1741 error = vn_open(&nd, &flags, 0, NULL);
1744 NDFREE(&nd, NDF_ONLY_PNBUF);
1745 if (nd.ni_vp->v_type != VREG)
1748 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1752 * XXX: we need to limit this number to some reasonable value
1754 if (vattr.va_size > 100 * 1024) {
1755 printf("hints file too large %ld\n", (long)vattr.va_size);
1758 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1761 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1762 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1765 VOP_UNLOCK(nd.ni_vp, 0);
1766 vn_close(nd.ni_vp, FREAD, cred, td);
1769 printf("can't read %zd\n", reclen);
1772 intp = (int *)hints;
1774 if (ival != LINKER_HINTS_VERSION) {
1775 printf("hints file version mismatch %d\n", ival);
1778 bufend = hints + vattr.va_size;
1779 recptr = (u_char *)intp;
1781 while (recptr < bufend && !found) {
1782 intp = (int *)recptr;
1789 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1792 INT_ALIGN(hints, cp);
1796 if (verinfo == NULL ||
1797 ival == verinfo->md_ver_preferred) {
1801 if (ival >= verinfo->md_ver_minimum &&
1802 ival <= verinfo->md_ver_maximum &&
1812 recptr += reclen + sizeof(int);
1815 * Finally check if KLD is in the place
1818 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1820 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1823 * KLD is newer than hints file. What we should do now?
1825 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1826 printf("warning: KLD '%s' is newer than the linker.hints"
1829 free(pathbuf, M_LINKER);
1831 free(hints, M_TEMP);
1832 if (nd.ni_vp != NULL) {
1833 VOP_UNLOCK(nd.ni_vp, 0);
1834 vn_close(nd.ni_vp, FREAD, cred, td);
1837 * If nothing found or hints is absent - fallback to the old
1838 * way by using "kldname[.ko]" as module name.
1840 if (!found && !bestver && result == NULL)
1841 result = linker_lookup_file(path, pathlen, modname,
1847 * Lookup KLD which contains requested module in the all directories.
1850 linker_search_module(const char *modname, int modnamelen,
1851 struct mod_depend *verinfo)
1853 char *cp, *ep, *result;
1856 * traverse the linker path
1858 for (cp = linker_path; *cp; cp = ep + 1) {
1859 /* find the end of this component */
1860 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1861 result = linker_hints_lookup(cp, ep - cp, modname,
1862 modnamelen, verinfo);
1872 * Search for module in all directories listed in the linker_path.
1875 linker_search_kld(const char *name)
1877 char *cp, *ep, *result;
1880 /* qualified at all? */
1881 if (strchr(name, '/'))
1882 return (strdup(name, M_LINKER));
1884 /* traverse the linker path */
1886 for (ep = linker_path; *ep; ep++) {
1888 /* find the end of this component */
1889 for (; *ep != 0 && *ep != ';'; ep++);
1890 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1898 linker_basename(const char *path)
1900 const char *filename;
1902 filename = strrchr(path, '/');
1903 if (filename == NULL)
1912 * Inform hwpmc about the set of kernel modules currently loaded.
1915 linker_hwpmc_list_objects(void)
1918 struct pmckern_map_in *kobase;
1923 TAILQ_FOREACH(lf, &linker_files, link)
1926 /* Allocate nmappings + 1 entries. */
1927 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1928 M_LINKER, M_WAITOK | M_ZERO);
1930 TAILQ_FOREACH(lf, &linker_files, link) {
1932 /* Save the info for this linker file. */
1933 kobase[i].pm_file = lf->filename;
1934 kobase[i].pm_address = (uintptr_t)lf->address;
1937 sx_sunlock(&kld_sx);
1939 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
1941 /* The last entry of the malloced area comprises of all zeros. */
1942 KASSERT(kobase[i].pm_file == NULL,
1943 ("linker_hwpmc_list_objects: last object not NULL"));
1945 return ((void *)kobase);
1950 * Find a file which contains given module and load it, if "parent" is not
1951 * NULL, register a reference to it.
1954 linker_load_module(const char *kldname, const char *modname,
1955 struct linker_file *parent, struct mod_depend *verinfo,
1956 struct linker_file **lfpp)
1958 linker_file_t lfdep;
1959 const char *filename;
1963 sx_assert(&kld_sx, SA_XLOCKED);
1964 if (modname == NULL) {
1966 * We have to load KLD
1968 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1970 pathname = linker_search_kld(kldname);
1972 if (modlist_lookup2(modname, verinfo) != NULL)
1974 if (kldname != NULL)
1975 pathname = strdup(kldname, M_LINKER);
1976 else if (rootvnode == NULL)
1980 * Need to find a KLD with required module
1982 pathname = linker_search_module(modname,
1983 strlen(modname), verinfo);
1985 if (pathname == NULL)
1989 * Can't load more than one file with the same basename XXX:
1990 * Actually it should be possible to have multiple KLDs with
1991 * the same basename but different path because they can
1992 * provide different versions of the same modules.
1994 filename = linker_basename(pathname);
1995 if (linker_find_file_by_name(filename))
1998 error = linker_load_file(pathname, &lfdep);
2001 if (modname && verinfo &&
2002 modlist_lookup2(modname, verinfo) == NULL) {
2003 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2008 error = linker_file_add_dependency(parent, lfdep);
2015 free(pathname, M_LINKER);
2020 * This routine is responsible for finding dependencies of userland initiated
2021 * kldload(2)'s of files.
2024 linker_load_dependencies(linker_file_t lf)
2026 linker_file_t lfdep;
2027 struct mod_metadata **start, **stop, **mdp, **nmdp;
2028 struct mod_metadata *mp, *nmp;
2029 struct mod_depend *verinfo;
2031 const char *modname, *nmodname;
2032 int ver, error = 0, count;
2035 * All files are dependant on /kernel.
2037 sx_assert(&kld_sx, SA_XLOCKED);
2038 if (linker_kernel_file) {
2039 linker_kernel_file->refs++;
2040 error = linker_file_add_dependency(lf, linker_kernel_file);
2044 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2047 for (mdp = start; mdp < stop; mdp++) {
2049 if (mp->md_type != MDT_VERSION)
2051 modname = mp->md_cval;
2052 ver = ((struct mod_version *)mp->md_data)->mv_version;
2053 mod = modlist_lookup(modname, ver);
2055 printf("interface %s.%d already present in the KLD"
2056 " '%s'!\n", modname, ver,
2057 mod->container->filename);
2062 for (mdp = start; mdp < stop; mdp++) {
2064 if (mp->md_type != MDT_DEPEND)
2066 modname = mp->md_cval;
2067 verinfo = mp->md_data;
2069 for (nmdp = start; nmdp < stop; nmdp++) {
2071 if (nmp->md_type != MDT_VERSION)
2073 nmodname = nmp->md_cval;
2074 if (strcmp(modname, nmodname) == 0)
2077 if (nmdp < stop)/* early exit, it's a self reference */
2079 mod = modlist_lookup2(modname, verinfo);
2080 if (mod) { /* woohoo, it's loaded already */
2081 lfdep = mod->container;
2083 error = linker_file_add_dependency(lf, lfdep);
2088 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2090 printf("KLD %s: depends on %s - not available or"
2091 " version mismatch\n", lf->filename, modname);
2098 linker_addmodules(lf, start, stop, 0);
2103 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2105 struct sysctl_req *req;
2108 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2112 * Export a nul-separated, double-nul-terminated list of all function names
2116 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2122 error = mac_kld_check_stat(req->td->td_ucred);
2126 error = sysctl_wire_old_buffer(req, 0);
2130 TAILQ_FOREACH(lf, &linker_files, link) {
2131 error = LINKER_EACH_FUNCTION_NAME(lf,
2132 sysctl_kern_function_list_iterate, req);
2134 sx_xunlock(&kld_sx);
2138 sx_xunlock(&kld_sx);
2139 return (SYSCTL_OUT(req, "", 1));
2142 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD,
2143 NULL, 0, sysctl_kern_function_list, "", "kernel function list");