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$");
31 #include "opt_hwpmc_hooks.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/sysproto.h>
38 #include <sys/sysent.h>
42 #include <sys/mutex.h>
44 #include <sys/module.h>
45 #include <sys/mount.h>
46 #include <sys/linker.h>
47 #include <sys/fcntl.h>
49 #include <sys/libkern.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysctl.h>
57 #include <security/mac/mac_framework.h>
59 #include "linker_if.h"
62 #include <sys/pmckern.h>
67 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RW,
68 &kld_debug, 0, "Set various levels of KLD debug");
71 #define KLD_LOCK() sx_xlock(&kld_sx)
72 #define KLD_UNLOCK() sx_xunlock(&kld_sx)
73 #define KLD_LOCKED() sx_xlocked(&kld_sx)
74 #define KLD_LOCK_ASSERT() do { \
76 sx_assert(&kld_sx, SX_XLOCKED); \
80 * static char *linker_search_path(const char *name, struct mod_depend
83 static const char *linker_basename(const char *path);
86 * Find a currently loaded file given its filename.
88 static linker_file_t linker_find_file_by_name(const char* _filename);
91 * Find a currently loaded file given its file id.
93 static linker_file_t linker_find_file_by_id(int _fileid);
95 /* Metadata from the static kernel */
96 SET_DECLARE(modmetadata_set, struct mod_metadata);
98 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
100 linker_file_t linker_kernel_file;
102 static struct sx kld_sx; /* kernel linker lock */
105 * Load counter used by clients to determine if a linker file has been
106 * re-loaded. This counter is incremented for each file load.
110 static linker_class_list_t classes;
111 static linker_file_list_t linker_files;
112 static int next_file_id = 1;
113 static int linker_no_more_classes = 0;
115 #define LINKER_GET_NEXT_FILE_ID(a) do { \
116 linker_file_t lftmp; \
120 TAILQ_FOREACH(lftmp, &linker_files, link) { \
121 if (next_file_id == lftmp->id) { \
126 (a) = next_file_id; \
130 /* XXX wrong name; we're looking at version provision tags here, not modules */
131 typedef TAILQ_HEAD(, modlist) modlisthead_t;
133 TAILQ_ENTRY(modlist) link; /* chain together all modules */
134 linker_file_t container;
138 typedef struct modlist *modlist_t;
139 static modlisthead_t found_modules;
141 static int linker_file_add_dependency(linker_file_t file,
143 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
144 const char* name, int deps);
145 static int linker_load_module(const char *kldname,
146 const char *modname, struct linker_file *parent,
147 struct mod_depend *verinfo, struct linker_file **lfpp);
148 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
151 linker_strdup(const char *str)
155 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
161 linker_init(void *arg)
164 sx_init(&kld_sx, "kernel linker");
165 TAILQ_INIT(&classes);
166 TAILQ_INIT(&linker_files);
169 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
172 linker_stop_class_add(void *arg)
175 linker_no_more_classes = 1;
178 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
181 linker_add_class(linker_class_t lc)
185 * We disallow any class registration past SI_ORDER_ANY
186 * of SI_SUB_KLD. We bump the reference count to keep the
187 * ops from being freed.
189 if (linker_no_more_classes == 1)
191 kobj_class_compile((kobj_class_t) lc);
192 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
193 TAILQ_INSERT_TAIL(&classes, lc, link);
198 linker_file_sysinit(linker_file_t lf)
200 struct sysinit **start, **stop, **sipp, **xipp, *save;
202 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
205 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
208 * Perform a bubble sort of the system initialization objects by
209 * their subsystem (primary key) and order (secondary key).
211 * Since some things care about execution order, this is the operation
212 * which ensures continued function.
214 for (sipp = start; sipp < stop; sipp++) {
215 for (xipp = sipp + 1; xipp < stop; xipp++) {
216 if ((*sipp)->subsystem < (*xipp)->subsystem ||
217 ((*sipp)->subsystem == (*xipp)->subsystem &&
218 (*sipp)->order <= (*xipp)->order))
227 * Traverse the (now) ordered list of system initialization tasks.
228 * Perform each task, and continue on to the next task.
231 for (sipp = start; sipp < stop; sipp++) {
232 if ((*sipp)->subsystem == SI_SUB_DUMMY)
233 continue; /* skip dummy task(s) */
236 (*((*sipp)->func)) ((*sipp)->udata);
242 linker_file_sysuninit(linker_file_t lf)
244 struct sysinit **start, **stop, **sipp, **xipp, *save;
246 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
249 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
254 * Perform a reverse bubble sort of the system initialization objects
255 * by their subsystem (primary key) and order (secondary key).
257 * Since some things care about execution order, this is the operation
258 * which ensures continued function.
260 for (sipp = start; sipp < stop; sipp++) {
261 for (xipp = sipp + 1; xipp < stop; xipp++) {
262 if ((*sipp)->subsystem > (*xipp)->subsystem ||
263 ((*sipp)->subsystem == (*xipp)->subsystem &&
264 (*sipp)->order >= (*xipp)->order))
273 * Traverse the (now) ordered list of system initialization tasks.
274 * Perform each task, and continue on to the next task.
277 for (sipp = start; sipp < stop; sipp++) {
278 if ((*sipp)->subsystem == SI_SUB_DUMMY)
279 continue; /* skip dummy task(s) */
282 (*((*sipp)->func)) ((*sipp)->udata);
288 linker_file_register_sysctls(linker_file_t lf)
290 struct sysctl_oid **start, **stop, **oidp;
293 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
296 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
300 for (oidp = start; oidp < stop; oidp++)
301 sysctl_register_oid(*oidp);
306 linker_file_unregister_sysctls(linker_file_t lf)
308 struct sysctl_oid **start, **stop, **oidp;
310 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
311 " for %s\n", lf->filename));
313 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
317 for (oidp = start; oidp < stop; oidp++)
318 sysctl_unregister_oid(*oidp);
323 linker_file_register_modules(linker_file_t lf)
325 struct mod_metadata **start, **stop, **mdp;
326 const moduledata_t *moddata;
327 int first_error, error;
329 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
330 " in %s\n", lf->filename));
332 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
335 * This fallback should be unnecessary, but if we get booted
336 * from boot2 instead of loader and we are missing our
337 * metadata then we have to try the best we can.
339 if (lf == linker_kernel_file) {
340 start = SET_BEGIN(modmetadata_set);
341 stop = SET_LIMIT(modmetadata_set);
346 for (mdp = start; mdp < stop; mdp++) {
347 if ((*mdp)->md_type != MDT_MODULE)
349 moddata = (*mdp)->md_data;
350 KLD_DPF(FILE, ("Registering module %s in %s\n",
351 moddata->name, lf->filename));
352 error = module_register(moddata, lf);
354 printf("Module %s failed to register: %d\n",
355 moddata->name, error);
356 if (first_error == 0)
360 return (first_error);
364 linker_init_kernel_modules(void)
367 linker_file_register_modules(linker_kernel_file);
370 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
374 linker_load_file(const char *filename, linker_file_t *result)
378 int foundfile, error;
380 /* Refuse to load modules if securelevel raised */
381 if (prison0.pr_securelevel > 0)
385 lf = linker_find_file_by_name(filename);
387 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
388 " incrementing refs\n", filename));
397 * We do not need to protect (lock) classes here because there is
398 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
399 * and there is no class deregistration mechanism at this time.
401 TAILQ_FOREACH(lc, &classes, link) {
402 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
404 error = LINKER_LOAD_FILE(lc, filename, &lf);
406 * If we got something other than ENOENT, then it exists but
407 * we cannot load it for some other reason.
412 error = linker_file_register_modules(lf);
413 if (error == EEXIST) {
414 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
418 linker_file_register_sysctls(lf);
419 linker_file_sysinit(lf);
421 lf->flags |= LINKER_FILE_LINKED;
427 * Less than ideal, but tells the user whether it failed to load or
428 * the module was not found.
433 * If the file type has not been recognized by the last try
434 * printout a message before to fail.
437 printf("linker_load_file: Unsupported file type\n");
440 * Format not recognized or otherwise unloadable.
441 * When loading a module that is statically built into
442 * the kernel EEXIST percolates back up as the return
443 * value. Preserve this so that apps like sysinstall
444 * can recognize this special case and not post bogus
450 error = ENOENT; /* Nothing found */
455 linker_reference_module(const char *modname, struct mod_depend *verinfo,
456 linker_file_t *result)
462 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
463 *result = mod->container;
469 error = linker_load_module(NULL, modname, NULL, verinfo, result);
475 linker_release_module(const char *modname, struct mod_depend *verinfo,
483 KASSERT(modname != NULL,
484 ("linker_release_module: no file or name"));
485 mod = modlist_lookup2(modname, verinfo);
492 KASSERT(modname == NULL && verinfo == NULL,
493 ("linker_release_module: both file and name"));
494 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
500 linker_find_file_by_name(const char *filename)
505 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
506 sprintf(koname, "%s.ko", filename);
509 TAILQ_FOREACH(lf, &linker_files, link) {
510 if (strcmp(lf->filename, koname) == 0)
512 if (strcmp(lf->filename, filename) == 0)
515 free(koname, M_LINKER);
520 linker_find_file_by_id(int fileid)
525 TAILQ_FOREACH(lf, &linker_files, link)
526 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
532 linker_file_foreach(linker_predicate_t *predicate, void *context)
538 TAILQ_FOREACH(lf, &linker_files, link) {
539 retval = predicate(lf, context);
548 linker_make_file(const char *pathname, linker_class_t lc)
551 const char *filename;
554 filename = linker_basename(pathname);
556 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
557 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
563 lf->filename = linker_strdup(filename);
564 lf->pathname = linker_strdup(pathname);
565 LINKER_GET_NEXT_FILE_ID(lf->id);
568 lf->loadcnt = ++loadcnt;
569 lf->sdt_probes = NULL;
571 STAILQ_INIT(&lf->common);
572 TAILQ_INIT(&lf->modules);
573 TAILQ_INSERT_TAIL(&linker_files, lf, link);
578 linker_file_unload(linker_file_t file, int flags)
581 modlist_t ml, nextml;
582 struct common_symbol *cp;
585 /* Refuse to unload modules if securelevel raised. */
586 if (prison0.pr_securelevel > 0)
590 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
592 /* Easy case of just dropping a reference. */
593 if (file->refs > 1) {
598 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
599 " informing modules\n"));
602 * Quiesce all the modules to give them a chance to veto the unload.
605 for (mod = TAILQ_FIRST(&file->modules); mod;
606 mod = module_getfnext(mod)) {
608 error = module_quiesce(mod);
609 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
610 KLD_DPF(FILE, ("linker_file_unload: module %s"
611 " vetoed unload\n", module_getname(mod)));
613 * XXX: Do we need to tell all the quiesced modules
614 * that they can resume work now via a new module
624 * Inform any modules associated with this file that they are
628 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
629 next = module_getfnext(mod);
633 * Give the module a chance to veto the unload.
635 if ((error = module_unload(mod)) != 0) {
636 KLD_DPF(FILE, ("linker_file_unload: module %s"
637 " failed unload\n", module_getname(mod)));
645 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
646 if (ml->container == file) {
647 TAILQ_REMOVE(&found_modules, ml, link);
653 * Don't try to run SYSUNINITs if we are unloaded due to a
656 if (file->flags & LINKER_FILE_LINKED) {
657 file->flags &= ~LINKER_FILE_LINKED;
659 linker_file_sysuninit(file);
660 linker_file_unregister_sysctls(file);
663 TAILQ_REMOVE(&linker_files, file, link);
666 for (i = 0; i < file->ndeps; i++)
667 linker_file_unload(file->deps[i], flags);
668 free(file->deps, M_LINKER);
671 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
672 STAILQ_REMOVE_HEAD(&file->common, link);
677 if (file->filename) {
678 free(file->filename, M_LINKER);
679 file->filename = NULL;
681 if (file->pathname) {
682 free(file->pathname, M_LINKER);
683 file->pathname = NULL;
685 kobj_delete((kobj_t) file, M_LINKER);
690 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
692 return (LINKER_CTF_GET(file, lc));
696 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
698 linker_file_t *newdeps;
701 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
702 M_LINKER, M_WAITOK | M_ZERO);
707 bcopy(file->deps, newdeps,
708 file->ndeps * sizeof(linker_file_t *));
709 free(file->deps, M_LINKER);
711 file->deps = newdeps;
712 file->deps[file->ndeps] = dep;
714 KLD_DPF(FILE, ("linker_file_add_dependency:"
715 " adding %s as dependency for %s\n",
716 dep->filename, file->filename));
721 * Locate a linker set and its contents. This is a helper function to avoid
722 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
723 * This function is used in this file so we can avoid having lots of (void **)
727 linker_file_lookup_set(linker_file_t file, const char *name,
728 void *firstp, void *lastp, int *countp)
732 locked = KLD_LOCKED();
735 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
742 * List all functions in a file.
745 linker_file_function_listall(linker_file_t lf,
746 linker_function_nameval_callback_t callback_func, void *arg)
748 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
752 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
757 locked = KLD_LOCKED();
760 sym = linker_file_lookup_symbol_internal(file, name, deps);
767 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
771 linker_symval_t symval;
773 size_t common_size = 0;
777 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
780 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
781 LINKER_SYMBOL_VALUES(file, sym, &symval);
782 if (symval.value == 0)
784 * For commons, first look them up in the
785 * dependencies and only allocate space if not found
788 common_size = symval.size;
790 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
791 ".value=%p\n", symval.value));
792 return (symval.value);
796 for (i = 0; i < file->ndeps; i++) {
797 address = linker_file_lookup_symbol_internal(
798 file->deps[i], name, 0);
800 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
801 " deps value=%p\n", address));
806 if (common_size > 0) {
808 * This is a common symbol which was not found in the
809 * dependencies. We maintain a simple common symbol table in
812 struct common_symbol *cp;
814 STAILQ_FOREACH(cp, &file->common, link) {
815 if (strcmp(cp->name, name) == 0) {
816 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
817 " old common value=%p\n", cp->address));
818 return (cp->address);
822 * Round the symbol size up to align.
824 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
825 cp = malloc(sizeof(struct common_symbol)
826 + common_size + strlen(name) + 1, M_LINKER,
828 cp->address = (caddr_t)(cp + 1);
829 cp->name = cp->address + common_size;
830 strcpy(cp->name, name);
831 bzero(cp->address, common_size);
832 STAILQ_INSERT_TAIL(&file->common, cp, link);
834 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
835 " value=%p\n", cp->address));
836 return (cp->address);
838 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
843 * Both DDB and stack(9) rely on the kernel linker to provide forward and
844 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
845 * do this in a lockfree manner. We provide a set of internal helper
846 * routines to perform these operations without locks, and then wrappers that
849 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
853 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
857 TAILQ_FOREACH(lf, &linker_files, link) {
858 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
866 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
869 c_linker_sym_t best, es;
870 u_long diff, bestdiff, off;
873 off = (uintptr_t)value;
875 TAILQ_FOREACH(lf, &linker_files, link) {
876 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
878 if (es != 0 && diff < bestdiff) {
897 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
901 TAILQ_FOREACH(lf, &linker_files, link) {
902 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
909 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
912 linker_symval_t symval;
917 error = linker_debug_search_symbol(value, &sym, offset);
920 error = linker_debug_symbol_values(sym, &symval);
923 strlcpy(buf, symval.name, buflen);
929 * DDB Helpers. DDB has to look across multiple files with their own symbol
930 * tables and string tables.
932 * Note that we do not obey list locking protocols here. We really don't need
933 * DDB to hang because somebody's got the lock held. We'll take the chance
934 * that the files list is inconsistant instead.
937 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
940 return (linker_debug_lookup(symstr, sym));
944 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
947 return (linker_debug_search_symbol(value, sym, diffp));
951 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
954 return (linker_debug_symbol_values(sym, symval));
958 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
962 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
967 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
968 * obey locking protocols, and offer a significantly less complex interface.
971 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
977 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
986 kern_kldload(struct thread *td, const char *file, int *fileid)
989 struct pmckern_map_in pkm;
991 const char *kldname, *modname;
995 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
998 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1002 * It is possible that kldloaded module will attach a new ifnet,
1003 * so vnet context must be set when this ocurs.
1005 CURVNET_SET(TD_TO_VNET(td));
1008 * If file does not contain a qualified name or any dot in it
1009 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1012 if (index(file, '/') || index(file, '.')) {
1021 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1025 pkm.pm_file = lf->filename;
1026 pkm.pm_address = (uintptr_t) lf->address;
1027 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
1039 kldload(struct thread *td, struct kldload_args *uap)
1041 char *pathname = NULL;
1044 td->td_retval[0] = -1;
1046 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1047 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1049 error = kern_kldload(td, pathname, &fileid);
1051 td->td_retval[0] = fileid;
1053 free(pathname, M_TEMP);
1058 kern_kldunload(struct thread *td, int fileid, int flags)
1061 struct pmckern_map_out pkm;
1066 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1069 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1072 CURVNET_SET(TD_TO_VNET(td));
1074 lf = linker_find_file_by_id(fileid);
1076 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1078 /* Check if there are DTrace probes enabled on this file. */
1079 if (lf->nenabled > 0) {
1080 printf("kldunload: attempt to unload file that has"
1081 " DTrace probes enabled\n");
1083 } else if (lf->userrefs == 0) {
1085 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1087 printf("kldunload: attempt to unload file that was"
1088 " loaded by the kernel\n");
1092 /* Save data needed by hwpmc(4) before unloading. */
1093 pkm.pm_address = (uintptr_t) lf->address;
1094 pkm.pm_size = lf->size;
1097 error = linker_file_unload(lf, flags);
1106 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
1114 kldunload(struct thread *td, struct kldunload_args *uap)
1117 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1121 kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1124 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1125 uap->flags != LINKER_UNLOAD_FORCE)
1127 return (kern_kldunload(td, uap->fileid, uap->flags));
1131 kldfind(struct thread *td, struct kldfind_args *uap)
1134 const char *filename;
1139 error = mac_kld_check_stat(td->td_ucred);
1144 td->td_retval[0] = -1;
1146 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1147 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1150 filename = linker_basename(pathname);
1152 lf = linker_find_file_by_name(filename);
1154 td->td_retval[0] = lf->id;
1159 free(pathname, M_TEMP);
1164 kldnext(struct thread *td, struct kldnext_args *uap)
1170 error = mac_kld_check_stat(td->td_ucred);
1176 if (uap->fileid == 0)
1177 lf = TAILQ_FIRST(&linker_files);
1179 lf = linker_find_file_by_id(uap->fileid);
1184 lf = TAILQ_NEXT(lf, link);
1187 /* Skip partially loaded files. */
1188 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1189 lf = TAILQ_NEXT(lf, link);
1192 td->td_retval[0] = lf->id;
1194 td->td_retval[0] = 0;
1201 kldstat(struct thread *td, struct kldstat_args *uap)
1203 struct kld_file_stat stat;
1205 int error, namelen, version, version_num;
1208 * Check the version of the user's structure.
1210 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) != 0)
1212 if (version == sizeof(struct kld_file_stat_1))
1214 else if (version == sizeof(struct kld_file_stat))
1220 error = mac_kld_check_stat(td->td_ucred);
1226 lf = linker_find_file_by_id(uap->fileid);
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 if (version_num > 1) {
1242 /* Version 2 fields: */
1243 namelen = strlen(lf->pathname) + 1;
1244 if (namelen > MAXPATHLEN)
1245 namelen = MAXPATHLEN;
1246 bcopy(lf->pathname, &stat.pathname[0], namelen);
1250 td->td_retval[0] = 0;
1252 return (copyout(&stat, uap->stat, version));
1256 kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1263 error = mac_kld_check_stat(td->td_ucred);
1269 lf = linker_find_file_by_id(uap->fileid);
1272 mp = TAILQ_FIRST(&lf->modules);
1274 td->td_retval[0] = module_getid(mp);
1276 td->td_retval[0] = 0;
1285 kldsym(struct thread *td, struct kldsym_args *uap)
1287 char *symstr = NULL;
1289 linker_symval_t symval;
1291 struct kld_sym_lookup lookup;
1295 error = mac_kld_check_stat(td->td_ucred);
1300 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1302 if (lookup.version != sizeof(lookup) ||
1303 uap->cmd != KLDSYM_LOOKUP)
1305 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1306 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1309 if (uap->fileid != 0) {
1310 lf = linker_find_file_by_id(uap->fileid);
1313 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1314 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1315 lookup.symvalue = (uintptr_t) symval.value;
1316 lookup.symsize = symval.size;
1317 error = copyout(&lookup, uap->data, sizeof(lookup));
1321 TAILQ_FOREACH(lf, &linker_files, link) {
1322 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1323 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1324 lookup.symvalue = (uintptr_t)symval.value;
1325 lookup.symsize = symval.size;
1326 error = copyout(&lookup, uap->data,
1336 free(symstr, M_TEMP);
1341 * Preloaded module support
1345 modlist_lookup(const char *name, int ver)
1349 TAILQ_FOREACH(mod, &found_modules, link) {
1350 if (strcmp(mod->name, name) == 0 &&
1351 (ver == 0 || mod->version == ver))
1358 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1360 modlist_t mod, bestmod;
1363 if (verinfo == NULL)
1364 return (modlist_lookup(name, 0));
1366 TAILQ_FOREACH(mod, &found_modules, link) {
1367 if (strcmp(mod->name, name) != 0)
1370 if (ver == verinfo->md_ver_preferred)
1372 if (ver >= verinfo->md_ver_minimum &&
1373 ver <= verinfo->md_ver_maximum &&
1374 (bestmod == NULL || ver > bestmod->version))
1381 modlist_newmodule(const char *modname, int version, linker_file_t container)
1385 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1387 panic("no memory for module list");
1388 mod->container = container;
1389 mod->name = modname;
1390 mod->version = version;
1391 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1396 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1397 struct mod_metadata **stop, int preload)
1399 struct mod_metadata *mp, **mdp;
1400 const char *modname;
1403 for (mdp = start; mdp < stop; mdp++) {
1405 if (mp->md_type != MDT_VERSION)
1407 modname = mp->md_cval;
1408 ver = ((struct mod_version *)mp->md_data)->mv_version;
1409 if (modlist_lookup(modname, ver) != NULL) {
1410 printf("module %s already present!\n", modname);
1411 /* XXX what can we do? this is a build error. :-( */
1414 modlist_newmodule(modname, ver, lf);
1419 linker_preload(void *arg)
1422 const char *modname, *nmodname;
1424 linker_file_t lf, nlf;
1427 linker_file_list_t loaded_files;
1428 linker_file_list_t depended_files;
1429 struct mod_metadata *mp, *nmp;
1430 struct mod_metadata **start, **stop, **mdp, **nmdp;
1431 struct mod_depend *verinfo;
1435 struct sysinit **si_start, **si_stop;
1437 TAILQ_INIT(&loaded_files);
1438 TAILQ_INIT(&depended_files);
1439 TAILQ_INIT(&found_modules);
1443 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1444 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1445 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1446 if (modname == NULL) {
1447 printf("Preloaded module at %p does not have a"
1448 " name!\n", modptr);
1451 if (modtype == NULL) {
1452 printf("Preloaded module at %p does not have a type!\n",
1457 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1460 TAILQ_FOREACH(lc, &classes, link) {
1461 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1467 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1471 * First get a list of stuff in the kernel.
1473 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1475 linker_addmodules(linker_kernel_file, start, stop, 1);
1478 * This is a once-off kinky bubble sort to resolve relocation
1479 * dependency requirements.
1482 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1483 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1486 * First, look to see if we would successfully link with this
1489 resolves = 1; /* unless we know otherwise */
1491 for (mdp = start; mdp < stop; mdp++) {
1493 if (mp->md_type != MDT_DEPEND)
1495 modname = mp->md_cval;
1496 verinfo = mp->md_data;
1497 for (nmdp = start; nmdp < stop; nmdp++) {
1499 if (nmp->md_type != MDT_VERSION)
1501 nmodname = nmp->md_cval;
1502 if (strcmp(modname, nmodname) == 0)
1505 if (nmdp < stop) /* it's a self reference */
1509 * ok, the module isn't here yet, we
1512 if (modlist_lookup2(modname, verinfo) == NULL)
1517 * OK, if we found our modules, we can link. So, "provide"
1518 * the modules inside and add it to the end of the link order
1523 for (mdp = start; mdp < stop; mdp++) {
1525 if (mp->md_type != MDT_VERSION)
1527 modname = mp->md_cval;
1528 nver = ((struct mod_version *)
1529 mp->md_data)->mv_version;
1530 if (modlist_lookup(modname,
1532 printf("module %s already"
1533 " present!\n", modname);
1534 TAILQ_REMOVE(&loaded_files,
1536 linker_file_unload(lf,
1537 LINKER_UNLOAD_FORCE);
1538 /* we changed tailq next ptr */
1541 modlist_newmodule(modname, nver, lf);
1544 TAILQ_REMOVE(&loaded_files, lf, loaded);
1545 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1547 * Since we provided modules, we need to restart the
1548 * sort so that the previous files that depend on us
1549 * have a chance. Also, we've busted the tailq next
1550 * pointer with the REMOVE.
1557 * At this point, we check to see what could not be resolved..
1559 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1560 TAILQ_REMOVE(&loaded_files, lf, loaded);
1561 printf("KLD file %s is missing dependencies\n", lf->filename);
1562 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1566 * We made it. Finish off the linking in the order we determined.
1568 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1569 if (linker_kernel_file) {
1570 linker_kernel_file->refs++;
1571 error = linker_file_add_dependency(lf,
1572 linker_kernel_file);
1574 panic("cannot add dependency");
1576 lf->userrefs++; /* so we can (try to) kldunload it */
1577 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1580 for (mdp = start; mdp < stop; mdp++) {
1582 if (mp->md_type != MDT_DEPEND)
1584 modname = mp->md_cval;
1585 verinfo = mp->md_data;
1586 mod = modlist_lookup2(modname, verinfo);
1587 /* Don't count self-dependencies */
1588 if (lf == mod->container)
1590 mod->container->refs++;
1591 error = linker_file_add_dependency(lf,
1594 panic("cannot add dependency");
1598 * Now do relocation etc using the symbol search paths
1599 * established by the dependencies
1601 error = LINKER_LINK_PRELOAD_FINISH(lf);
1603 TAILQ_REMOVE(&depended_files, lf, loaded);
1604 printf("KLD file %s - could not finalize loading\n",
1606 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1609 linker_file_register_modules(lf);
1610 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1611 &si_stop, NULL) == 0)
1612 sysinit_add(si_start, si_stop);
1613 linker_file_register_sysctls(lf);
1614 lf->flags |= LINKER_FILE_LINKED;
1616 /* woohoo! we made it! */
1619 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1622 * Search for a not-loaded module by name.
1624 * Modules may be found in the following locations:
1626 * - preloaded (result is just the module name) - on disk (result is full path
1629 * If the module name is qualified in any way (contains path, etc.) the we
1630 * simply return a copy of it.
1632 * The search path can be manipulated via sysctl. Note that we use the ';'
1633 * character as a separator to be consistent with the bootloader.
1636 static char linker_hintfile[] = "linker.hints";
1637 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1639 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1640 sizeof(linker_path), "module load search path");
1642 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1644 static char *linker_ext_list[] = {
1651 * Check if file actually exists either with or without extension listed in
1652 * the linker_ext_list. (probably should be generic for the rest of the
1656 linker_lookup_file(const char *path, int pathlen, const char *name,
1657 int namelen, struct vattr *vap)
1659 struct nameidata nd;
1660 struct thread *td = curthread; /* XXX */
1661 char *result, **cpp, *sep;
1662 int error, len, extlen, reclen, flags, vfslocked;
1666 for (cpp = linker_ext_list; *cpp; cpp++) {
1671 extlen++; /* trailing '\0' */
1672 sep = (path[pathlen - 1] != '/') ? "/" : "";
1674 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1675 result = malloc(reclen, M_LINKER, M_WAITOK);
1676 for (cpp = linker_ext_list; *cpp; cpp++) {
1677 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1678 namelen, name, *cpp);
1680 * Attempt to open the file, and return the path if
1681 * we succeed and it's a regular file.
1683 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
1685 error = vn_open(&nd, &flags, 0, NULL);
1687 vfslocked = NDHASGIANT(&nd);
1688 NDFREE(&nd, NDF_ONLY_PNBUF);
1689 type = nd.ni_vp->v_type;
1691 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1692 VOP_UNLOCK(nd.ni_vp, 0);
1693 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1694 VFS_UNLOCK_GIANT(vfslocked);
1699 free(result, M_LINKER);
1703 #define INT_ALIGN(base, ptr) ptr = \
1704 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1707 * Lookup KLD which contains requested module in the "linker.hints" file. If
1708 * version specification is available, then try to find the best KLD.
1709 * Otherwise just find the latest one.
1712 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1713 int modnamelen, struct mod_depend *verinfo)
1715 struct thread *td = curthread; /* XXX */
1716 struct ucred *cred = td ? td->td_ucred : NULL;
1717 struct nameidata nd;
1718 struct vattr vattr, mattr;
1719 u_char *hints = NULL;
1720 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1721 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1725 bestver = found = 0;
1727 sep = (path[pathlen - 1] != '/') ? "/" : "";
1728 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1730 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1731 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1734 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
1736 error = vn_open(&nd, &flags, 0, NULL);
1739 vfslocked = NDHASGIANT(&nd);
1740 NDFREE(&nd, NDF_ONLY_PNBUF);
1741 if (nd.ni_vp->v_type != VREG)
1744 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1748 * XXX: we need to limit this number to some reasonable value
1750 if (vattr.va_size > 100 * 1024) {
1751 printf("hints file too large %ld\n", (long)vattr.va_size);
1754 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1757 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1758 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1761 VOP_UNLOCK(nd.ni_vp, 0);
1762 vn_close(nd.ni_vp, FREAD, cred, td);
1763 VFS_UNLOCK_GIANT(vfslocked);
1766 printf("can't read %d\n", reclen);
1769 intp = (int *)hints;
1771 if (ival != LINKER_HINTS_VERSION) {
1772 printf("hints file version mismatch %d\n", ival);
1775 bufend = hints + vattr.va_size;
1776 recptr = (u_char *)intp;
1778 while (recptr < bufend && !found) {
1779 intp = (int *)recptr;
1786 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1789 INT_ALIGN(hints, cp);
1793 if (verinfo == NULL ||
1794 ival == verinfo->md_ver_preferred) {
1798 if (ival >= verinfo->md_ver_minimum &&
1799 ival <= verinfo->md_ver_maximum &&
1809 recptr += reclen + sizeof(int);
1812 * Finally check if KLD is in the place
1815 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1817 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1820 * KLD is newer than hints file. What we should do now?
1822 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1823 printf("warning: KLD '%s' is newer than the linker.hints"
1826 free(pathbuf, M_LINKER);
1828 free(hints, M_TEMP);
1829 if (nd.ni_vp != NULL) {
1830 VOP_UNLOCK(nd.ni_vp, 0);
1831 vn_close(nd.ni_vp, FREAD, cred, td);
1832 VFS_UNLOCK_GIANT(vfslocked);
1835 * If nothing found or hints is absent - fallback to the old
1836 * way by using "kldname[.ko]" as module name.
1838 if (!found && !bestver && result == NULL)
1839 result = linker_lookup_file(path, pathlen, modname,
1845 * Lookup KLD which contains requested module in the all directories.
1848 linker_search_module(const char *modname, int modnamelen,
1849 struct mod_depend *verinfo)
1851 char *cp, *ep, *result;
1854 * traverse the linker path
1856 for (cp = linker_path; *cp; cp = ep + 1) {
1857 /* find the end of this component */
1858 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1859 result = linker_hints_lookup(cp, ep - cp, modname,
1860 modnamelen, verinfo);
1870 * Search for module in all directories listed in the linker_path.
1873 linker_search_kld(const char *name)
1875 char *cp, *ep, *result;
1878 /* qualified at all? */
1879 if (index(name, '/'))
1880 return (linker_strdup(name));
1882 /* traverse the linker path */
1884 for (ep = linker_path; *ep; ep++) {
1886 /* find the end of this component */
1887 for (; *ep != 0 && *ep != ';'; ep++);
1888 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1896 linker_basename(const char *path)
1898 const char *filename;
1900 filename = rindex(path, '/');
1901 if (filename == NULL)
1910 * Inform hwpmc about the set of kernel modules currently loaded.
1913 linker_hwpmc_list_objects(void)
1916 struct pmckern_map_in *kobase;
1921 TAILQ_FOREACH(lf, &linker_files, link)
1924 /* Allocate nmappings + 1 entries. */
1925 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1926 M_LINKER, M_WAITOK | M_ZERO);
1928 TAILQ_FOREACH(lf, &linker_files, link) {
1930 /* Save the info for this linker file. */
1931 kobase[i].pm_file = lf->filename;
1932 kobase[i].pm_address = (uintptr_t)lf->address;
1937 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
1939 /* The last entry of the malloced area comprises of all zeros. */
1940 KASSERT(kobase[i].pm_file == NULL,
1941 ("linker_hwpmc_list_objects: last object not NULL"));
1943 return ((void *)kobase);
1948 * Find a file which contains given module and load it, if "parent" is not
1949 * NULL, register a reference to it.
1952 linker_load_module(const char *kldname, const char *modname,
1953 struct linker_file *parent, struct mod_depend *verinfo,
1954 struct linker_file **lfpp)
1956 linker_file_t lfdep;
1957 const char *filename;
1962 if (modname == NULL) {
1964 * We have to load KLD
1966 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1968 pathname = linker_search_kld(kldname);
1970 if (modlist_lookup2(modname, verinfo) != NULL)
1972 if (kldname != NULL)
1973 pathname = linker_strdup(kldname);
1974 else if (rootvnode == NULL)
1978 * Need to find a KLD with required module
1980 pathname = linker_search_module(modname,
1981 strlen(modname), verinfo);
1983 if (pathname == NULL)
1987 * Can't load more than one file with the same basename XXX:
1988 * Actually it should be possible to have multiple KLDs with
1989 * the same basename but different path because they can
1990 * provide different versions of the same modules.
1992 filename = linker_basename(pathname);
1993 if (linker_find_file_by_name(filename))
1996 error = linker_load_file(pathname, &lfdep);
1999 if (modname && verinfo &&
2000 modlist_lookup2(modname, verinfo) == NULL) {
2001 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2006 error = linker_file_add_dependency(parent, lfdep);
2013 free(pathname, M_LINKER);
2018 * This routine is responsible for finding dependencies of userland initiated
2019 * kldload(2)'s of files.
2022 linker_load_dependencies(linker_file_t lf)
2024 linker_file_t lfdep;
2025 struct mod_metadata **start, **stop, **mdp, **nmdp;
2026 struct mod_metadata *mp, *nmp;
2027 struct mod_depend *verinfo;
2029 const char *modname, *nmodname;
2030 int ver, error = 0, count;
2033 * All files are dependant on /kernel.
2036 if (linker_kernel_file) {
2037 linker_kernel_file->refs++;
2038 error = linker_file_add_dependency(lf, linker_kernel_file);
2042 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2045 for (mdp = start; mdp < stop; mdp++) {
2047 if (mp->md_type != MDT_VERSION)
2049 modname = mp->md_cval;
2050 ver = ((struct mod_version *)mp->md_data)->mv_version;
2051 mod = modlist_lookup(modname, ver);
2053 printf("interface %s.%d already present in the KLD"
2054 " '%s'!\n", modname, ver,
2055 mod->container->filename);
2060 for (mdp = start; mdp < stop; mdp++) {
2062 if (mp->md_type != MDT_DEPEND)
2064 modname = mp->md_cval;
2065 verinfo = mp->md_data;
2067 for (nmdp = start; nmdp < stop; nmdp++) {
2069 if (nmp->md_type != MDT_VERSION)
2071 nmodname = nmp->md_cval;
2072 if (strcmp(modname, nmodname) == 0)
2075 if (nmdp < stop)/* early exit, it's a self reference */
2077 mod = modlist_lookup2(modname, verinfo);
2078 if (mod) { /* woohoo, it's loaded already */
2079 lfdep = mod->container;
2081 error = linker_file_add_dependency(lf, lfdep);
2086 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2088 printf("KLD %s: depends on %s - not available or"
2089 " version mismatch\n", lf->filename, modname);
2096 linker_addmodules(lf, start, stop, 0);
2101 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2103 struct sysctl_req *req;
2106 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2110 * Export a nul-separated, double-nul-terminated list of all function names
2114 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2120 error = mac_kld_check_stat(req->td->td_ucred);
2124 error = sysctl_wire_old_buffer(req, 0);
2128 TAILQ_FOREACH(lf, &linker_files, link) {
2129 error = LINKER_EACH_FUNCTION_NAME(lf,
2130 sysctl_kern_function_list_iterate, req);
2137 return (SYSCTL_OUT(req, "", 1));
2140 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
2141 NULL, 0, sysctl_kern_function_list, "", "kernel function list");