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/eventhandler.h>
48 #include <sys/fcntl.h>
50 #include <sys/libkern.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysctl.h>
58 #include <security/mac/mac_framework.h>
60 #include "linker_if.h"
63 #include <sys/pmckern.h>
68 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RW,
69 &kld_debug, 0, "Set various levels of KLD debug");
72 #define KLD_LOCK() sx_xlock(&kld_sx)
73 #define KLD_UNLOCK() sx_xunlock(&kld_sx)
74 #define KLD_DOWNGRADE() sx_downgrade(&kld_sx)
75 #define KLD_LOCK_READ() sx_slock(&kld_sx)
76 #define KLD_UNLOCK_READ() sx_sunlock(&kld_sx)
77 #define KLD_LOCKED() sx_xlocked(&kld_sx)
78 #define KLD_LOCK_ASSERT() do { \
80 sx_assert(&kld_sx, SX_XLOCKED); \
84 * static char *linker_search_path(const char *name, struct mod_depend
87 static const char *linker_basename(const char *path);
90 * Find a currently loaded file given its filename.
92 static linker_file_t linker_find_file_by_name(const char* _filename);
95 * Find a currently loaded file given its file id.
97 static linker_file_t linker_find_file_by_id(int _fileid);
99 /* Metadata from the static kernel */
100 SET_DECLARE(modmetadata_set, struct mod_metadata);
102 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
104 linker_file_t linker_kernel_file;
106 static struct sx kld_sx; /* kernel linker lock */
109 * Load counter used by clients to determine if a linker file has been
110 * re-loaded. This counter is incremented for each file load.
114 static linker_class_list_t classes;
115 static linker_file_list_t linker_files;
116 static int next_file_id = 1;
117 static int linker_no_more_classes = 0;
119 #define LINKER_GET_NEXT_FILE_ID(a) do { \
120 linker_file_t lftmp; \
124 TAILQ_FOREACH(lftmp, &linker_files, link) { \
125 if (next_file_id == lftmp->id) { \
130 (a) = next_file_id; \
134 /* XXX wrong name; we're looking at version provision tags here, not modules */
135 typedef TAILQ_HEAD(, modlist) modlisthead_t;
137 TAILQ_ENTRY(modlist) link; /* chain together all modules */
138 linker_file_t container;
142 typedef struct modlist *modlist_t;
143 static modlisthead_t found_modules;
145 static int linker_file_add_dependency(linker_file_t file,
147 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
148 const char* name, int deps);
149 static int linker_load_module(const char *kldname,
150 const char *modname, struct linker_file *parent,
151 struct mod_depend *verinfo, struct linker_file **lfpp);
152 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
155 linker_strdup(const char *str)
159 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
165 linker_init(void *arg)
168 sx_init(&kld_sx, "kernel linker");
169 TAILQ_INIT(&classes);
170 TAILQ_INIT(&linker_files);
173 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
176 linker_stop_class_add(void *arg)
179 linker_no_more_classes = 1;
182 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
185 linker_add_class(linker_class_t lc)
189 * We disallow any class registration past SI_ORDER_ANY
190 * of SI_SUB_KLD. We bump the reference count to keep the
191 * ops from being freed.
193 if (linker_no_more_classes == 1)
195 kobj_class_compile((kobj_class_t) lc);
196 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
197 TAILQ_INSERT_TAIL(&classes, lc, link);
202 linker_file_sysinit(linker_file_t lf)
204 struct sysinit **start, **stop, **sipp, **xipp, *save;
206 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
209 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
212 * Perform a bubble sort of the system initialization objects by
213 * their subsystem (primary key) and order (secondary key).
215 * Since some things care about execution order, this is the operation
216 * which ensures continued function.
218 for (sipp = start; sipp < stop; sipp++) {
219 for (xipp = sipp + 1; xipp < stop; xipp++) {
220 if ((*sipp)->subsystem < (*xipp)->subsystem ||
221 ((*sipp)->subsystem == (*xipp)->subsystem &&
222 (*sipp)->order <= (*xipp)->order))
231 * Traverse the (now) ordered list of system initialization tasks.
232 * Perform each task, and continue on to the next task.
235 for (sipp = start; sipp < stop; sipp++) {
236 if ((*sipp)->subsystem == SI_SUB_DUMMY)
237 continue; /* skip dummy task(s) */
240 (*((*sipp)->func)) ((*sipp)->udata);
246 linker_file_sysuninit(linker_file_t lf)
248 struct sysinit **start, **stop, **sipp, **xipp, *save;
250 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
253 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
258 * Perform a reverse bubble sort of the system initialization objects
259 * by their subsystem (primary key) and order (secondary key).
261 * Since some things care about execution order, this is the operation
262 * which ensures continued function.
264 for (sipp = start; sipp < stop; sipp++) {
265 for (xipp = sipp + 1; xipp < stop; xipp++) {
266 if ((*sipp)->subsystem > (*xipp)->subsystem ||
267 ((*sipp)->subsystem == (*xipp)->subsystem &&
268 (*sipp)->order >= (*xipp)->order))
277 * Traverse the (now) ordered list of system initialization tasks.
278 * Perform each task, and continue on to the next task.
281 for (sipp = start; sipp < stop; sipp++) {
282 if ((*sipp)->subsystem == SI_SUB_DUMMY)
283 continue; /* skip dummy task(s) */
286 (*((*sipp)->func)) ((*sipp)->udata);
292 linker_file_register_sysctls(linker_file_t lf)
294 struct sysctl_oid **start, **stop, **oidp;
297 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
300 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
304 for (oidp = start; oidp < stop; oidp++)
305 sysctl_register_oid(*oidp);
310 linker_file_unregister_sysctls(linker_file_t lf)
312 struct sysctl_oid **start, **stop, **oidp;
314 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
315 " for %s\n", lf->filename));
317 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
321 for (oidp = start; oidp < stop; oidp++)
322 sysctl_unregister_oid(*oidp);
327 linker_file_register_modules(linker_file_t lf)
329 struct mod_metadata **start, **stop, **mdp;
330 const moduledata_t *moddata;
331 int first_error, error;
333 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
334 " in %s\n", lf->filename));
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)
371 linker_file_register_modules(linker_kernel_file);
374 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
378 linker_load_file(const char *filename, linker_file_t *result)
382 int foundfile, error, modules;
384 /* Refuse to load modules if securelevel raised */
385 if (prison0.pr_securelevel > 0)
389 lf = linker_find_file_by_name(filename);
391 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
392 " incrementing refs\n", filename));
401 * We do not need to protect (lock) classes here because there is
402 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
403 * and there is no class deregistration mechanism at this time.
405 TAILQ_FOREACH(lc, &classes, link) {
406 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
408 error = LINKER_LOAD_FILE(lc, filename, &lf);
410 * If we got something other than ENOENT, then it exists but
411 * we cannot load it for some other reason.
416 error = linker_file_register_modules(lf);
417 if (error == EEXIST) {
418 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
421 modules = !TAILQ_EMPTY(&lf->modules);
423 linker_file_register_sysctls(lf);
424 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);
442 * Less than ideal, but tells the user whether it failed to load or
443 * the module was not found.
448 * If the file type has not been recognized by the last try
449 * printout a message before to fail.
452 printf("linker_load_file: Unsupported file type\n");
455 * Format not recognized or otherwise unloadable.
456 * When loading a module that is statically built into
457 * the kernel EEXIST percolates back up as the return
458 * value. Preserve this so that apps like sysinstall
459 * can recognize this special case and not post bogus
465 error = ENOENT; /* Nothing found */
470 linker_reference_module(const char *modname, struct mod_depend *verinfo,
471 linker_file_t *result)
477 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
478 *result = mod->container;
484 error = linker_load_module(NULL, modname, NULL, verinfo, result);
490 linker_release_module(const char *modname, struct mod_depend *verinfo,
498 KASSERT(modname != NULL,
499 ("linker_release_module: no file or name"));
500 mod = modlist_lookup2(modname, verinfo);
507 KASSERT(modname == NULL && verinfo == NULL,
508 ("linker_release_module: both file and name"));
509 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
515 linker_find_file_by_name(const char *filename)
520 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
521 sprintf(koname, "%s.ko", filename);
524 TAILQ_FOREACH(lf, &linker_files, link) {
525 if (strcmp(lf->filename, koname) == 0)
527 if (strcmp(lf->filename, filename) == 0)
530 free(koname, M_LINKER);
535 linker_find_file_by_id(int fileid)
540 TAILQ_FOREACH(lf, &linker_files, link)
541 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
547 linker_file_foreach(linker_predicate_t *predicate, void *context)
553 TAILQ_FOREACH(lf, &linker_files, link) {
554 retval = predicate(lf, context);
563 linker_make_file(const char *pathname, linker_class_t lc)
566 const char *filename;
569 filename = linker_basename(pathname);
571 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
572 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
578 lf->filename = linker_strdup(filename);
579 lf->pathname = linker_strdup(pathname);
580 LINKER_GET_NEXT_FILE_ID(lf->id);
583 lf->loadcnt = ++loadcnt;
584 STAILQ_INIT(&lf->common);
585 TAILQ_INIT(&lf->modules);
586 TAILQ_INSERT_TAIL(&linker_files, lf, link);
591 linker_file_unload(linker_file_t file, int flags)
594 modlist_t ml, nextml;
595 struct common_symbol *cp;
598 /* Refuse to unload modules if securelevel raised. */
599 if (prison0.pr_securelevel > 0)
603 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
605 /* Easy case of just dropping a reference. */
606 if (file->refs > 1) {
611 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
612 " informing modules\n"));
615 * Quiesce all the modules to give them a chance to veto the unload.
618 for (mod = TAILQ_FIRST(&file->modules); mod;
619 mod = module_getfnext(mod)) {
621 error = module_quiesce(mod);
622 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
623 KLD_DPF(FILE, ("linker_file_unload: module %s"
624 " vetoed unload\n", module_getname(mod)));
626 * XXX: Do we need to tell all the quiesced modules
627 * that they can resume work now via a new module
637 * Inform any modules associated with this file that they are
641 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
642 next = module_getfnext(mod);
646 * Give the module a chance to veto the unload.
648 if ((error = module_unload(mod)) != 0) {
651 KLD_DPF(FILE, ("linker_file_unload: module %s"
652 " failed unload\n", module_getname(mod)));
662 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
663 if (ml->container == file) {
664 TAILQ_REMOVE(&found_modules, ml, link);
670 * Don't try to run SYSUNINITs if we are unloaded due to a
673 if (file->flags & LINKER_FILE_LINKED) {
674 file->flags &= ~LINKER_FILE_LINKED;
676 linker_file_sysuninit(file);
677 linker_file_unregister_sysctls(file);
680 TAILQ_REMOVE(&linker_files, file, link);
683 for (i = 0; i < file->ndeps; i++)
684 linker_file_unload(file->deps[i], flags);
685 free(file->deps, M_LINKER);
688 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
689 STAILQ_REMOVE_HEAD(&file->common, link);
694 if (file->filename) {
695 free(file->filename, M_LINKER);
696 file->filename = NULL;
698 if (file->pathname) {
699 free(file->pathname, M_LINKER);
700 file->pathname = NULL;
702 kobj_delete((kobj_t) file, M_LINKER);
707 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
709 return (LINKER_CTF_GET(file, lc));
713 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
715 linker_file_t *newdeps;
718 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
719 M_LINKER, M_WAITOK | M_ZERO);
724 bcopy(file->deps, newdeps,
725 file->ndeps * sizeof(linker_file_t *));
726 free(file->deps, M_LINKER);
728 file->deps = newdeps;
729 file->deps[file->ndeps] = dep;
731 KLD_DPF(FILE, ("linker_file_add_dependency:"
732 " adding %s as dependency for %s\n",
733 dep->filename, file->filename));
738 * Locate a linker set and its contents. This is a helper function to avoid
739 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
740 * This function is used in this file so we can avoid having lots of (void **)
744 linker_file_lookup_set(linker_file_t file, const char *name,
745 void *firstp, void *lastp, int *countp)
749 locked = KLD_LOCKED();
752 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
759 * List all functions in a file.
762 linker_file_function_listall(linker_file_t lf,
763 linker_function_nameval_callback_t callback_func, void *arg)
765 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
769 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
774 locked = KLD_LOCKED();
777 sym = linker_file_lookup_symbol_internal(file, name, deps);
784 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
788 linker_symval_t symval;
790 size_t common_size = 0;
794 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
797 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
798 LINKER_SYMBOL_VALUES(file, sym, &symval);
799 if (symval.value == 0)
801 * For commons, first look them up in the
802 * dependencies and only allocate space if not found
805 common_size = symval.size;
807 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
808 ".value=%p\n", symval.value));
809 return (symval.value);
813 for (i = 0; i < file->ndeps; i++) {
814 address = linker_file_lookup_symbol_internal(
815 file->deps[i], name, 0);
817 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
818 " deps value=%p\n", address));
823 if (common_size > 0) {
825 * This is a common symbol which was not found in the
826 * dependencies. We maintain a simple common symbol table in
829 struct common_symbol *cp;
831 STAILQ_FOREACH(cp, &file->common, link) {
832 if (strcmp(cp->name, name) == 0) {
833 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
834 " old common value=%p\n", cp->address));
835 return (cp->address);
839 * Round the symbol size up to align.
841 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
842 cp = malloc(sizeof(struct common_symbol)
843 + common_size + strlen(name) + 1, M_LINKER,
845 cp->address = (caddr_t)(cp + 1);
846 cp->name = cp->address + common_size;
847 strcpy(cp->name, name);
848 bzero(cp->address, common_size);
849 STAILQ_INSERT_TAIL(&file->common, cp, link);
851 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
852 " value=%p\n", cp->address));
853 return (cp->address);
855 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
860 * Both DDB and stack(9) rely on the kernel linker to provide forward and
861 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
862 * do this in a lockfree manner. We provide a set of internal helper
863 * routines to perform these operations without locks, and then wrappers that
866 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
870 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
874 TAILQ_FOREACH(lf, &linker_files, link) {
875 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
883 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
886 c_linker_sym_t best, es;
887 u_long diff, bestdiff, off;
890 off = (uintptr_t)value;
892 TAILQ_FOREACH(lf, &linker_files, link) {
893 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
895 if (es != 0 && diff < bestdiff) {
914 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
918 TAILQ_FOREACH(lf, &linker_files, link) {
919 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
926 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
929 linker_symval_t symval;
934 error = linker_debug_search_symbol(value, &sym, offset);
937 error = linker_debug_symbol_values(sym, &symval);
940 strlcpy(buf, symval.name, buflen);
945 * DDB Helpers. DDB has to look across multiple files with their own symbol
946 * tables and string tables.
948 * Note that we do not obey list locking protocols here. We really don't need
949 * DDB to hang because somebody's got the lock held. We'll take the chance
950 * that the files list is inconsistant instead.
954 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
957 return (linker_debug_lookup(symstr, sym));
962 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
965 return (linker_debug_search_symbol(value, sym, diffp));
969 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
972 return (linker_debug_symbol_values(sym, symval));
976 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
980 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
984 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
985 * obey locking protocols, and offer a significantly less complex interface.
988 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
994 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
1003 kern_kldload(struct thread *td, const char *file, int *fileid)
1006 struct pmckern_map_in pkm;
1008 const char *kldname, *modname;
1012 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1015 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1019 * It is possible that kldloaded module will attach a new ifnet,
1020 * so vnet context must be set when this ocurs.
1022 CURVNET_SET(TD_TO_VNET(td));
1025 * If file does not contain a qualified name or any dot in it
1026 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1029 if (index(file, '/') || index(file, '.')) {
1038 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1047 EVENTHANDLER_INVOKE(kld_load, lf);
1051 pkm.pm_file = lf->filename;
1052 pkm.pm_address = (uintptr_t) lf->address;
1053 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
1065 sys_kldload(struct thread *td, struct kldload_args *uap)
1067 char *pathname = NULL;
1070 td->td_retval[0] = -1;
1072 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1073 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1075 error = kern_kldload(td, pathname, &fileid);
1077 td->td_retval[0] = fileid;
1079 free(pathname, M_TEMP);
1084 kern_kldunload(struct thread *td, int fileid, int flags)
1087 struct pmckern_map_out pkm;
1092 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1095 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1098 CURVNET_SET(TD_TO_VNET(td));
1100 lf = linker_find_file_by_id(fileid);
1102 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1104 EVENTHANDLER_INVOKE(kld_unload, lf, &error);
1107 else if (lf->userrefs == 0) {
1109 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1111 printf("kldunload: attempt to unload file that was"
1112 " loaded by the kernel\n");
1116 /* Save data needed by hwpmc(4) before unloading. */
1117 pkm.pm_address = (uintptr_t) lf->address;
1118 pkm.pm_size = lf->size;
1121 error = linker_file_unload(lf, flags);
1131 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
1143 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1146 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1150 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1153 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1154 uap->flags != LINKER_UNLOAD_FORCE)
1156 return (kern_kldunload(td, uap->fileid, uap->flags));
1160 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1163 const char *filename;
1168 error = mac_kld_check_stat(td->td_ucred);
1173 td->td_retval[0] = -1;
1175 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1176 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1179 filename = linker_basename(pathname);
1181 lf = linker_find_file_by_name(filename);
1183 td->td_retval[0] = lf->id;
1188 free(pathname, M_TEMP);
1193 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1199 error = mac_kld_check_stat(td->td_ucred);
1205 if (uap->fileid == 0)
1206 lf = TAILQ_FIRST(&linker_files);
1208 lf = linker_find_file_by_id(uap->fileid);
1213 lf = TAILQ_NEXT(lf, link);
1216 /* Skip partially loaded files. */
1217 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1218 lf = TAILQ_NEXT(lf, link);
1221 td->td_retval[0] = lf->id;
1223 td->td_retval[0] = 0;
1230 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1232 struct kld_file_stat stat;
1236 * Check the version of the user's structure.
1238 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1241 if (version != sizeof(struct kld_file_stat_1) &&
1242 version != sizeof(struct kld_file_stat))
1245 error = kern_kldstat(td, uap->fileid, &stat);
1248 return (copyout(&stat, uap->stat, version));
1252 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1259 error = mac_kld_check_stat(td->td_ucred);
1265 lf = linker_find_file_by_id(fileid);
1271 /* Version 1 fields: */
1272 namelen = strlen(lf->filename) + 1;
1273 if (namelen > MAXPATHLEN)
1274 namelen = MAXPATHLEN;
1275 bcopy(lf->filename, &stat->name[0], namelen);
1276 stat->refs = lf->refs;
1278 stat->address = lf->address;
1279 stat->size = lf->size;
1280 /* Version 2 fields: */
1281 namelen = strlen(lf->pathname) + 1;
1282 if (namelen > MAXPATHLEN)
1283 namelen = MAXPATHLEN;
1284 bcopy(lf->pathname, &stat->pathname[0], namelen);
1287 td->td_retval[0] = 0;
1292 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1299 error = mac_kld_check_stat(td->td_ucred);
1305 lf = linker_find_file_by_id(uap->fileid);
1308 mp = TAILQ_FIRST(&lf->modules);
1310 td->td_retval[0] = module_getid(mp);
1312 td->td_retval[0] = 0;
1321 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1323 char *symstr = NULL;
1325 linker_symval_t symval;
1327 struct kld_sym_lookup lookup;
1331 error = mac_kld_check_stat(td->td_ucred);
1336 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1338 if (lookup.version != sizeof(lookup) ||
1339 uap->cmd != KLDSYM_LOOKUP)
1341 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1342 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1345 if (uap->fileid != 0) {
1346 lf = linker_find_file_by_id(uap->fileid);
1349 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1350 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1351 lookup.symvalue = (uintptr_t) symval.value;
1352 lookup.symsize = symval.size;
1353 error = copyout(&lookup, uap->data, sizeof(lookup));
1357 TAILQ_FOREACH(lf, &linker_files, link) {
1358 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1359 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1360 lookup.symvalue = (uintptr_t)symval.value;
1361 lookup.symsize = symval.size;
1362 error = copyout(&lookup, uap->data,
1372 free(symstr, M_TEMP);
1377 * Preloaded module support
1381 modlist_lookup(const char *name, int ver)
1385 TAILQ_FOREACH(mod, &found_modules, link) {
1386 if (strcmp(mod->name, name) == 0 &&
1387 (ver == 0 || mod->version == ver))
1394 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1396 modlist_t mod, bestmod;
1399 if (verinfo == NULL)
1400 return (modlist_lookup(name, 0));
1402 TAILQ_FOREACH(mod, &found_modules, link) {
1403 if (strcmp(mod->name, name) != 0)
1406 if (ver == verinfo->md_ver_preferred)
1408 if (ver >= verinfo->md_ver_minimum &&
1409 ver <= verinfo->md_ver_maximum &&
1410 (bestmod == NULL || ver > bestmod->version))
1417 modlist_newmodule(const char *modname, int version, linker_file_t container)
1421 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1423 panic("no memory for module list");
1424 mod->container = container;
1425 mod->name = modname;
1426 mod->version = version;
1427 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1432 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1433 struct mod_metadata **stop, int preload)
1435 struct mod_metadata *mp, **mdp;
1436 const char *modname;
1439 for (mdp = start; mdp < stop; mdp++) {
1441 if (mp->md_type != MDT_VERSION)
1443 modname = mp->md_cval;
1444 ver = ((struct mod_version *)mp->md_data)->mv_version;
1445 if (modlist_lookup(modname, ver) != NULL) {
1446 printf("module %s already present!\n", modname);
1447 /* XXX what can we do? this is a build error. :-( */
1450 modlist_newmodule(modname, ver, lf);
1455 linker_preload(void *arg)
1458 const char *modname, *nmodname;
1460 linker_file_t lf, nlf;
1463 linker_file_list_t loaded_files;
1464 linker_file_list_t depended_files;
1465 struct mod_metadata *mp, *nmp;
1466 struct mod_metadata **start, **stop, **mdp, **nmdp;
1467 struct mod_depend *verinfo;
1471 struct sysinit **si_start, **si_stop;
1473 TAILQ_INIT(&loaded_files);
1474 TAILQ_INIT(&depended_files);
1475 TAILQ_INIT(&found_modules);
1479 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1480 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1481 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1482 if (modname == NULL) {
1483 printf("Preloaded module at %p does not have a"
1484 " name!\n", modptr);
1487 if (modtype == NULL) {
1488 printf("Preloaded module at %p does not have a type!\n",
1493 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1496 TAILQ_FOREACH(lc, &classes, link) {
1497 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1503 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1507 * First get a list of stuff in the kernel.
1509 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1511 linker_addmodules(linker_kernel_file, start, stop, 1);
1514 * This is a once-off kinky bubble sort to resolve relocation
1515 * dependency requirements.
1518 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1519 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1522 * First, look to see if we would successfully link with this
1525 resolves = 1; /* unless we know otherwise */
1527 for (mdp = start; mdp < stop; mdp++) {
1529 if (mp->md_type != MDT_DEPEND)
1531 modname = mp->md_cval;
1532 verinfo = mp->md_data;
1533 for (nmdp = start; nmdp < stop; nmdp++) {
1535 if (nmp->md_type != MDT_VERSION)
1537 nmodname = nmp->md_cval;
1538 if (strcmp(modname, nmodname) == 0)
1541 if (nmdp < stop) /* it's a self reference */
1545 * ok, the module isn't here yet, we
1548 if (modlist_lookup2(modname, verinfo) == NULL)
1553 * OK, if we found our modules, we can link. So, "provide"
1554 * the modules inside and add it to the end of the link order
1559 for (mdp = start; mdp < stop; mdp++) {
1561 if (mp->md_type != MDT_VERSION)
1563 modname = mp->md_cval;
1564 nver = ((struct mod_version *)
1565 mp->md_data)->mv_version;
1566 if (modlist_lookup(modname,
1568 printf("module %s already"
1569 " present!\n", modname);
1570 TAILQ_REMOVE(&loaded_files,
1572 linker_file_unload(lf,
1573 LINKER_UNLOAD_FORCE);
1574 /* we changed tailq next ptr */
1577 modlist_newmodule(modname, nver, lf);
1580 TAILQ_REMOVE(&loaded_files, lf, loaded);
1581 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1583 * Since we provided modules, we need to restart the
1584 * sort so that the previous files that depend on us
1585 * have a chance. Also, we've busted the tailq next
1586 * pointer with the REMOVE.
1593 * At this point, we check to see what could not be resolved..
1595 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1596 TAILQ_REMOVE(&loaded_files, lf, loaded);
1597 printf("KLD file %s is missing dependencies\n", lf->filename);
1598 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1602 * We made it. Finish off the linking in the order we determined.
1604 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1605 if (linker_kernel_file) {
1606 linker_kernel_file->refs++;
1607 error = linker_file_add_dependency(lf,
1608 linker_kernel_file);
1610 panic("cannot add dependency");
1612 lf->userrefs++; /* so we can (try to) kldunload it */
1613 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1616 for (mdp = start; mdp < stop; mdp++) {
1618 if (mp->md_type != MDT_DEPEND)
1620 modname = mp->md_cval;
1621 verinfo = mp->md_data;
1622 mod = modlist_lookup2(modname, verinfo);
1624 printf("KLD file %s - cannot find "
1625 "dependency \"%s\"\n",
1626 lf->filename, modname);
1629 /* Don't count self-dependencies */
1630 if (lf == mod->container)
1632 mod->container->refs++;
1633 error = linker_file_add_dependency(lf,
1636 panic("cannot add dependency");
1640 * Now do relocation etc using the symbol search paths
1641 * established by the dependencies
1643 error = LINKER_LINK_PRELOAD_FINISH(lf);
1645 printf("KLD file %s - could not finalize loading\n",
1649 linker_file_register_modules(lf);
1650 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1651 &si_stop, NULL) == 0)
1652 sysinit_add(si_start, si_stop);
1653 linker_file_register_sysctls(lf);
1654 lf->flags |= LINKER_FILE_LINKED;
1657 TAILQ_REMOVE(&depended_files, lf, loaded);
1658 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1660 /* woohoo! we made it! */
1663 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1666 * Search for a not-loaded module by name.
1668 * Modules may be found in the following locations:
1670 * - preloaded (result is just the module name) - on disk (result is full path
1673 * If the module name is qualified in any way (contains path, etc.) the we
1674 * simply return a copy of it.
1676 * The search path can be manipulated via sysctl. Note that we use the ';'
1677 * character as a separator to be consistent with the bootloader.
1680 static char linker_hintfile[] = "linker.hints";
1681 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1683 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1684 sizeof(linker_path), "module load search path");
1686 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1688 static char *linker_ext_list[] = {
1695 * Check if file actually exists either with or without extension listed in
1696 * the linker_ext_list. (probably should be generic for the rest of the
1700 linker_lookup_file(const char *path, int pathlen, const char *name,
1701 int namelen, struct vattr *vap)
1703 struct nameidata nd;
1704 struct thread *td = curthread; /* XXX */
1705 char *result, **cpp, *sep;
1706 int error, len, extlen, reclen, flags, vfslocked;
1710 for (cpp = linker_ext_list; *cpp; cpp++) {
1715 extlen++; /* trailing '\0' */
1716 sep = (path[pathlen - 1] != '/') ? "/" : "";
1718 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1719 result = malloc(reclen, M_LINKER, M_WAITOK);
1720 for (cpp = linker_ext_list; *cpp; cpp++) {
1721 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1722 namelen, name, *cpp);
1724 * Attempt to open the file, and return the path if
1725 * we succeed and it's a regular file.
1727 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
1729 error = vn_open(&nd, &flags, 0, NULL);
1731 vfslocked = NDHASGIANT(&nd);
1732 NDFREE(&nd, NDF_ONLY_PNBUF);
1733 type = nd.ni_vp->v_type;
1735 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1736 VOP_UNLOCK(nd.ni_vp, 0);
1737 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1738 VFS_UNLOCK_GIANT(vfslocked);
1743 free(result, M_LINKER);
1747 #define INT_ALIGN(base, ptr) ptr = \
1748 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1751 * Lookup KLD which contains requested module in the "linker.hints" file. If
1752 * version specification is available, then try to find the best KLD.
1753 * Otherwise just find the latest one.
1756 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1757 int modnamelen, struct mod_depend *verinfo)
1759 struct thread *td = curthread; /* XXX */
1760 struct ucred *cred = td ? td->td_ucred : NULL;
1761 struct nameidata nd;
1762 struct vattr vattr, mattr;
1763 u_char *hints = NULL;
1764 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1765 int error, ival, bestver, *intp, found, flags, clen, blen;
1770 bestver = found = 0;
1772 sep = (path[pathlen - 1] != '/') ? "/" : "";
1773 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1775 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1776 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1779 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
1781 error = vn_open(&nd, &flags, 0, NULL);
1784 vfslocked = NDHASGIANT(&nd);
1785 NDFREE(&nd, NDF_ONLY_PNBUF);
1786 if (nd.ni_vp->v_type != VREG)
1789 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1793 * XXX: we need to limit this number to some reasonable value
1795 if (vattr.va_size > 100 * 1024) {
1796 printf("hints file too large %ld\n", (long)vattr.va_size);
1799 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1802 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1803 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1806 VOP_UNLOCK(nd.ni_vp, 0);
1807 vn_close(nd.ni_vp, FREAD, cred, td);
1808 VFS_UNLOCK_GIANT(vfslocked);
1811 printf("can't read %zd\n", reclen);
1814 intp = (int *)hints;
1816 if (ival != LINKER_HINTS_VERSION) {
1817 printf("hints file version mismatch %d\n", ival);
1820 bufend = hints + vattr.va_size;
1821 recptr = (u_char *)intp;
1823 while (recptr < bufend && !found) {
1824 intp = (int *)recptr;
1831 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1834 INT_ALIGN(hints, cp);
1838 if (verinfo == NULL ||
1839 ival == verinfo->md_ver_preferred) {
1843 if (ival >= verinfo->md_ver_minimum &&
1844 ival <= verinfo->md_ver_maximum &&
1854 recptr += reclen + sizeof(int);
1857 * Finally check if KLD is in the place
1860 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1862 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1865 * KLD is newer than hints file. What we should do now?
1867 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1868 printf("warning: KLD '%s' is newer than the linker.hints"
1871 free(pathbuf, M_LINKER);
1873 free(hints, M_TEMP);
1874 if (nd.ni_vp != NULL) {
1875 VOP_UNLOCK(nd.ni_vp, 0);
1876 vn_close(nd.ni_vp, FREAD, cred, td);
1877 VFS_UNLOCK_GIANT(vfslocked);
1880 * If nothing found or hints is absent - fallback to the old
1881 * way by using "kldname[.ko]" as module name.
1883 if (!found && !bestver && result == NULL)
1884 result = linker_lookup_file(path, pathlen, modname,
1890 * Lookup KLD which contains requested module in the all directories.
1893 linker_search_module(const char *modname, int modnamelen,
1894 struct mod_depend *verinfo)
1896 char *cp, *ep, *result;
1899 * traverse the linker path
1901 for (cp = linker_path; *cp; cp = ep + 1) {
1902 /* find the end of this component */
1903 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1904 result = linker_hints_lookup(cp, ep - cp, modname,
1905 modnamelen, verinfo);
1915 * Search for module in all directories listed in the linker_path.
1918 linker_search_kld(const char *name)
1920 char *cp, *ep, *result;
1923 /* qualified at all? */
1924 if (index(name, '/'))
1925 return (linker_strdup(name));
1927 /* traverse the linker path */
1929 for (ep = linker_path; *ep; ep++) {
1931 /* find the end of this component */
1932 for (; *ep != 0 && *ep != ';'; ep++);
1933 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1941 linker_basename(const char *path)
1943 const char *filename;
1945 filename = rindex(path, '/');
1946 if (filename == NULL)
1955 * Inform hwpmc about the set of kernel modules currently loaded.
1958 linker_hwpmc_list_objects(void)
1961 struct pmckern_map_in *kobase;
1966 TAILQ_FOREACH(lf, &linker_files, link)
1969 /* Allocate nmappings + 1 entries. */
1970 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1971 M_LINKER, M_WAITOK | M_ZERO);
1973 TAILQ_FOREACH(lf, &linker_files, link) {
1975 /* Save the info for this linker file. */
1976 kobase[i].pm_file = lf->filename;
1977 kobase[i].pm_address = (uintptr_t)lf->address;
1982 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
1984 /* The last entry of the malloced area comprises of all zeros. */
1985 KASSERT(kobase[i].pm_file == NULL,
1986 ("linker_hwpmc_list_objects: last object not NULL"));
1988 return ((void *)kobase);
1993 * Find a file which contains given module and load it, if "parent" is not
1994 * NULL, register a reference to it.
1997 linker_load_module(const char *kldname, const char *modname,
1998 struct linker_file *parent, struct mod_depend *verinfo,
1999 struct linker_file **lfpp)
2001 linker_file_t lfdep;
2002 const char *filename;
2007 if (modname == NULL) {
2009 * We have to load KLD
2011 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2013 pathname = linker_search_kld(kldname);
2015 if (modlist_lookup2(modname, verinfo) != NULL)
2017 if (kldname != NULL)
2018 pathname = linker_strdup(kldname);
2019 else if (rootvnode == NULL)
2023 * Need to find a KLD with required module
2025 pathname = linker_search_module(modname,
2026 strlen(modname), verinfo);
2028 if (pathname == NULL)
2032 * Can't load more than one file with the same basename XXX:
2033 * Actually it should be possible to have multiple KLDs with
2034 * the same basename but different path because they can
2035 * provide different versions of the same modules.
2037 filename = linker_basename(pathname);
2038 if (linker_find_file_by_name(filename))
2041 error = linker_load_file(pathname, &lfdep);
2044 if (modname && verinfo &&
2045 modlist_lookup2(modname, verinfo) == NULL) {
2046 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2051 error = linker_file_add_dependency(parent, lfdep);
2058 free(pathname, M_LINKER);
2063 * This routine is responsible for finding dependencies of userland initiated
2064 * kldload(2)'s of files.
2067 linker_load_dependencies(linker_file_t lf)
2069 linker_file_t lfdep;
2070 struct mod_metadata **start, **stop, **mdp, **nmdp;
2071 struct mod_metadata *mp, *nmp;
2072 struct mod_depend *verinfo;
2074 const char *modname, *nmodname;
2075 int ver, error = 0, count;
2078 * All files are dependant on /kernel.
2081 if (linker_kernel_file) {
2082 linker_kernel_file->refs++;
2083 error = linker_file_add_dependency(lf, linker_kernel_file);
2087 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2090 for (mdp = start; mdp < stop; mdp++) {
2092 if (mp->md_type != MDT_VERSION)
2094 modname = mp->md_cval;
2095 ver = ((struct mod_version *)mp->md_data)->mv_version;
2096 mod = modlist_lookup(modname, ver);
2098 printf("interface %s.%d already present in the KLD"
2099 " '%s'!\n", modname, ver,
2100 mod->container->filename);
2105 for (mdp = start; mdp < stop; mdp++) {
2107 if (mp->md_type != MDT_DEPEND)
2109 modname = mp->md_cval;
2110 verinfo = mp->md_data;
2112 for (nmdp = start; nmdp < stop; nmdp++) {
2114 if (nmp->md_type != MDT_VERSION)
2116 nmodname = nmp->md_cval;
2117 if (strcmp(modname, nmodname) == 0)
2120 if (nmdp < stop)/* early exit, it's a self reference */
2122 mod = modlist_lookup2(modname, verinfo);
2123 if (mod) { /* woohoo, it's loaded already */
2124 lfdep = mod->container;
2126 error = linker_file_add_dependency(lf, lfdep);
2131 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2133 printf("KLD %s: depends on %s - not available or"
2134 " version mismatch\n", lf->filename, modname);
2141 linker_addmodules(lf, start, stop, 0);
2146 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2148 struct sysctl_req *req;
2151 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2155 * Export a nul-separated, double-nul-terminated list of all function names
2159 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2165 error = mac_kld_check_stat(req->td->td_ucred);
2169 error = sysctl_wire_old_buffer(req, 0);
2173 TAILQ_FOREACH(lf, &linker_files, link) {
2174 error = LINKER_EACH_FUNCTION_NAME(lf,
2175 sysctl_kern_function_list_iterate, req);
2182 return (SYSCTL_OUT(req, "", 1));
2185 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD,
2186 NULL, 0, sysctl_kern_function_list, "", "kernel function list");