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>
69 #define KLD_LOCK() sx_xlock(&kld_sx)
70 #define KLD_UNLOCK() sx_xunlock(&kld_sx)
71 #define KLD_DOWNGRADE() sx_downgrade(&kld_sx)
72 #define KLD_LOCK_READ() sx_slock(&kld_sx)
73 #define KLD_UNLOCK_READ() sx_sunlock(&kld_sx)
74 #define KLD_LOCKED() sx_xlocked(&kld_sx)
75 #define KLD_LOCK_ASSERT() do { \
77 sx_assert(&kld_sx, SX_XLOCKED); \
81 * static char *linker_search_path(const char *name, struct mod_depend
84 static const char *linker_basename(const char *path);
87 * Find a currently loaded file given its filename.
89 static linker_file_t linker_find_file_by_name(const char* _filename);
92 * Find a currently loaded file given its file id.
94 static linker_file_t linker_find_file_by_id(int _fileid);
96 /* Metadata from the static kernel */
97 SET_DECLARE(modmetadata_set, struct mod_metadata);
99 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
101 linker_file_t linker_kernel_file;
103 static struct sx kld_sx; /* kernel linker lock */
106 * Load counter used by clients to determine if a linker file has been
107 * re-loaded. This counter is incremented for each file load.
111 static linker_class_list_t classes;
112 static linker_file_list_t linker_files;
113 static int next_file_id = 1;
114 static int linker_no_more_classes = 0;
116 #define LINKER_GET_NEXT_FILE_ID(a) do { \
117 linker_file_t lftmp; \
121 TAILQ_FOREACH(lftmp, &linker_files, link) { \
122 if (next_file_id == lftmp->id) { \
127 (a) = next_file_id; \
131 /* XXX wrong name; we're looking at version provision tags here, not modules */
132 typedef TAILQ_HEAD(, modlist) modlisthead_t;
134 TAILQ_ENTRY(modlist) link; /* chain together all modules */
135 linker_file_t container;
139 typedef struct modlist *modlist_t;
140 static modlisthead_t found_modules;
142 static int linker_file_add_dependency(linker_file_t file,
144 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
145 const char* name, int deps);
146 static int linker_load_module(const char *kldname,
147 const char *modname, struct linker_file *parent,
148 struct mod_depend *verinfo, struct linker_file **lfpp);
149 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
152 linker_strdup(const char *str)
156 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
162 linker_init(void *arg)
165 sx_init(&kld_sx, "kernel linker");
166 TAILQ_INIT(&classes);
167 TAILQ_INIT(&linker_files);
170 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
173 linker_stop_class_add(void *arg)
176 linker_no_more_classes = 1;
179 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
182 linker_add_class(linker_class_t lc)
186 * We disallow any class registration past SI_ORDER_ANY
187 * of SI_SUB_KLD. We bump the reference count to keep the
188 * ops from being freed.
190 if (linker_no_more_classes == 1)
192 kobj_class_compile((kobj_class_t) lc);
193 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
194 TAILQ_INSERT_TAIL(&classes, lc, link);
199 linker_file_sysinit(linker_file_t lf)
201 struct sysinit **start, **stop, **sipp, **xipp, *save;
203 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
206 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
209 * Perform a bubble sort of the system initialization objects by
210 * their subsystem (primary key) and order (secondary key).
212 * Since some things care about execution order, this is the operation
213 * which ensures continued function.
215 for (sipp = start; sipp < stop; sipp++) {
216 for (xipp = sipp + 1; xipp < stop; xipp++) {
217 if ((*sipp)->subsystem < (*xipp)->subsystem ||
218 ((*sipp)->subsystem == (*xipp)->subsystem &&
219 (*sipp)->order <= (*xipp)->order))
228 * Traverse the (now) ordered list of system initialization tasks.
229 * Perform each task, and continue on to the next task.
232 for (sipp = start; sipp < stop; sipp++) {
233 if ((*sipp)->subsystem == SI_SUB_DUMMY)
234 continue; /* skip dummy task(s) */
237 (*((*sipp)->func)) ((*sipp)->udata);
243 linker_file_sysuninit(linker_file_t lf)
245 struct sysinit **start, **stop, **sipp, **xipp, *save;
247 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
250 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
255 * Perform a reverse bubble sort of the system initialization objects
256 * by their subsystem (primary key) and order (secondary key).
258 * Since some things care about execution order, this is the operation
259 * which ensures continued function.
261 for (sipp = start; sipp < stop; sipp++) {
262 for (xipp = sipp + 1; xipp < stop; xipp++) {
263 if ((*sipp)->subsystem > (*xipp)->subsystem ||
264 ((*sipp)->subsystem == (*xipp)->subsystem &&
265 (*sipp)->order >= (*xipp)->order))
274 * Traverse the (now) ordered list of system initialization tasks.
275 * Perform each task, and continue on to the next task.
278 for (sipp = start; sipp < stop; sipp++) {
279 if ((*sipp)->subsystem == SI_SUB_DUMMY)
280 continue; /* skip dummy task(s) */
283 (*((*sipp)->func)) ((*sipp)->udata);
289 linker_file_register_sysctls(linker_file_t lf)
291 struct sysctl_oid **start, **stop, **oidp;
294 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
297 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
301 for (oidp = start; oidp < stop; oidp++)
302 sysctl_register_oid(*oidp);
307 linker_file_unregister_sysctls(linker_file_t lf)
309 struct sysctl_oid **start, **stop, **oidp;
311 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
312 " for %s\n", lf->filename));
314 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
318 for (oidp = start; oidp < stop; oidp++)
319 sysctl_unregister_oid(*oidp);
324 linker_file_register_modules(linker_file_t lf)
326 struct mod_metadata **start, **stop, **mdp;
327 const moduledata_t *moddata;
328 int first_error, error;
330 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
331 " in %s\n", lf->filename));
333 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
336 * This fallback should be unnecessary, but if we get booted
337 * from boot2 instead of loader and we are missing our
338 * metadata then we have to try the best we can.
340 if (lf == linker_kernel_file) {
341 start = SET_BEGIN(modmetadata_set);
342 stop = SET_LIMIT(modmetadata_set);
347 for (mdp = start; mdp < stop; mdp++) {
348 if ((*mdp)->md_type != MDT_MODULE)
350 moddata = (*mdp)->md_data;
351 KLD_DPF(FILE, ("Registering module %s in %s\n",
352 moddata->name, lf->filename));
353 error = module_register(moddata, lf);
355 printf("Module %s failed to register: %d\n",
356 moddata->name, error);
357 if (first_error == 0)
361 return (first_error);
365 linker_init_kernel_modules(void)
368 linker_file_register_modules(linker_kernel_file);
371 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
375 linker_load_file(const char *filename, linker_file_t *result)
379 int foundfile, error, modules;
381 /* Refuse to load modules if securelevel raised */
382 if (prison0.pr_securelevel > 0)
386 lf = linker_find_file_by_name(filename);
388 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
389 " incrementing refs\n", filename));
398 * We do not need to protect (lock) classes here because there is
399 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
400 * and there is no class deregistration mechanism at this time.
402 TAILQ_FOREACH(lc, &classes, link) {
403 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
405 error = LINKER_LOAD_FILE(lc, filename, &lf);
407 * If we got something other than ENOENT, then it exists but
408 * we cannot load it for some other reason.
413 error = linker_file_register_modules(lf);
414 if (error == EEXIST) {
415 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
418 modules = !TAILQ_EMPTY(&lf->modules);
420 linker_file_register_sysctls(lf);
421 linker_file_sysinit(lf);
423 lf->flags |= LINKER_FILE_LINKED;
426 * If all of the modules in this file failed
427 * to load, unload the file and return an
430 if (modules && TAILQ_EMPTY(&lf->modules)) {
431 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
439 * Less than ideal, but tells the user whether it failed to load or
440 * the module was not found.
445 * If the file type has not been recognized by the last try
446 * printout a message before to fail.
449 printf("linker_load_file: Unsupported file type\n");
452 * Format not recognized or otherwise unloadable.
453 * When loading a module that is statically built into
454 * the kernel EEXIST percolates back up as the return
455 * value. Preserve this so that apps like sysinstall
456 * can recognize this special case and not post bogus
462 error = ENOENT; /* Nothing found */
467 linker_reference_module(const char *modname, struct mod_depend *verinfo,
468 linker_file_t *result)
474 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
475 *result = mod->container;
481 error = linker_load_module(NULL, modname, NULL, verinfo, result);
487 linker_release_module(const char *modname, struct mod_depend *verinfo,
495 KASSERT(modname != NULL,
496 ("linker_release_module: no file or name"));
497 mod = modlist_lookup2(modname, verinfo);
504 KASSERT(modname == NULL && verinfo == NULL,
505 ("linker_release_module: both file and name"));
506 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
512 linker_find_file_by_name(const char *filename)
517 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
518 sprintf(koname, "%s.ko", filename);
521 TAILQ_FOREACH(lf, &linker_files, link) {
522 if (strcmp(lf->filename, koname) == 0)
524 if (strcmp(lf->filename, filename) == 0)
527 free(koname, M_LINKER);
532 linker_find_file_by_id(int fileid)
537 TAILQ_FOREACH(lf, &linker_files, link)
538 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
544 linker_file_foreach(linker_predicate_t *predicate, void *context)
550 TAILQ_FOREACH(lf, &linker_files, link) {
551 retval = predicate(lf, context);
560 linker_make_file(const char *pathname, linker_class_t lc)
563 const char *filename;
566 filename = linker_basename(pathname);
568 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
569 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
575 lf->filename = linker_strdup(filename);
576 lf->pathname = linker_strdup(pathname);
577 LINKER_GET_NEXT_FILE_ID(lf->id);
580 lf->loadcnt = ++loadcnt;
581 lf->sdt_probes = NULL;
583 STAILQ_INIT(&lf->common);
584 TAILQ_INIT(&lf->modules);
585 TAILQ_INSERT_TAIL(&linker_files, lf, link);
590 linker_file_unload(linker_file_t file, int flags)
593 modlist_t ml, nextml;
594 struct common_symbol *cp;
597 /* Refuse to unload modules if securelevel raised. */
598 if (prison0.pr_securelevel > 0)
602 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
604 /* Easy case of just dropping a reference. */
605 if (file->refs > 1) {
610 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
611 " informing modules\n"));
614 * Quiesce all the modules to give them a chance to veto the unload.
617 for (mod = TAILQ_FIRST(&file->modules); mod;
618 mod = module_getfnext(mod)) {
620 error = module_quiesce(mod);
621 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
622 KLD_DPF(FILE, ("linker_file_unload: module %s"
623 " vetoed unload\n", module_getname(mod)));
625 * XXX: Do we need to tell all the quiesced modules
626 * that they can resume work now via a new module
636 * Inform any modules associated with this file that they are
640 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
641 next = module_getfnext(mod);
645 * Give the module a chance to veto the unload.
647 if ((error = module_unload(mod)) != 0) {
650 KLD_DPF(FILE, ("linker_file_unload: module %s"
651 " failed unload\n", module_getname(mod)));
661 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
662 if (ml->container == file) {
663 TAILQ_REMOVE(&found_modules, ml, link);
669 * Don't try to run SYSUNINITs if we are unloaded due to a
672 if (file->flags & LINKER_FILE_LINKED) {
673 file->flags &= ~LINKER_FILE_LINKED;
675 linker_file_sysuninit(file);
676 linker_file_unregister_sysctls(file);
679 TAILQ_REMOVE(&linker_files, file, link);
682 for (i = 0; i < file->ndeps; i++)
683 linker_file_unload(file->deps[i], flags);
684 free(file->deps, M_LINKER);
687 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
688 STAILQ_REMOVE_HEAD(&file->common, link);
693 if (file->filename) {
694 free(file->filename, M_LINKER);
695 file->filename = NULL;
697 if (file->pathname) {
698 free(file->pathname, M_LINKER);
699 file->pathname = NULL;
701 kobj_delete((kobj_t) file, M_LINKER);
706 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
708 return (LINKER_CTF_GET(file, lc));
712 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
714 linker_file_t *newdeps;
717 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
718 M_LINKER, M_WAITOK | M_ZERO);
723 bcopy(file->deps, newdeps,
724 file->ndeps * sizeof(linker_file_t *));
725 free(file->deps, M_LINKER);
727 file->deps = newdeps;
728 file->deps[file->ndeps] = dep;
734 * Locate a linker set and its contents. This is a helper function to avoid
735 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
736 * This function is used in this file so we can avoid having lots of (void **)
740 linker_file_lookup_set(linker_file_t file, const char *name,
741 void *firstp, void *lastp, int *countp)
745 locked = KLD_LOCKED();
748 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
755 * List all functions in a file.
758 linker_file_function_listall(linker_file_t lf,
759 linker_function_nameval_callback_t callback_func, void *arg)
761 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
765 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
770 locked = KLD_LOCKED();
773 sym = linker_file_lookup_symbol_internal(file, name, deps);
780 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
784 linker_symval_t symval;
786 size_t common_size = 0;
790 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
793 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
794 LINKER_SYMBOL_VALUES(file, sym, &symval);
795 if (symval.value == 0)
797 * For commons, first look them up in the
798 * dependencies and only allocate space if not found
801 common_size = symval.size;
803 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
804 ".value=%p\n", symval.value));
805 return (symval.value);
809 for (i = 0; i < file->ndeps; i++) {
810 address = linker_file_lookup_symbol_internal(
811 file->deps[i], name, 0);
813 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
814 " deps value=%p\n", address));
819 if (common_size > 0) {
821 * This is a common symbol which was not found in the
822 * dependencies. We maintain a simple common symbol table in
825 struct common_symbol *cp;
827 STAILQ_FOREACH(cp, &file->common, link) {
828 if (strcmp(cp->name, name) == 0) {
829 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
830 " old common value=%p\n", cp->address));
831 return (cp->address);
835 * Round the symbol size up to align.
837 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
838 cp = malloc(sizeof(struct common_symbol)
839 + common_size + strlen(name) + 1, M_LINKER,
841 cp->address = (caddr_t)(cp + 1);
842 cp->name = cp->address + common_size;
843 strcpy(cp->name, name);
844 bzero(cp->address, common_size);
845 STAILQ_INSERT_TAIL(&file->common, cp, link);
847 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
848 " value=%p\n", cp->address));
849 return (cp->address);
851 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
856 * Both DDB and stack(9) rely on the kernel linker to provide forward and
857 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
858 * do this in a lockfree manner. We provide a set of internal helper
859 * routines to perform these operations without locks, and then wrappers that
862 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
866 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
870 TAILQ_FOREACH(lf, &linker_files, link) {
871 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
879 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
882 c_linker_sym_t best, es;
883 u_long diff, bestdiff, off;
886 off = (uintptr_t)value;
888 TAILQ_FOREACH(lf, &linker_files, link) {
889 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
891 if (es != 0 && diff < bestdiff) {
910 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
914 TAILQ_FOREACH(lf, &linker_files, link) {
915 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
922 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
925 linker_symval_t symval;
930 error = linker_debug_search_symbol(value, &sym, offset);
933 error = linker_debug_symbol_values(sym, &symval);
936 strlcpy(buf, symval.name, buflen);
941 * DDB Helpers. DDB has to look across multiple files with their own symbol
942 * tables and string tables.
944 * Note that we do not obey list locking protocols here. We really don't need
945 * DDB to hang because somebody's got the lock held. We'll take the chance
946 * that the files list is inconsistant instead.
950 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
953 return (linker_debug_lookup(symstr, sym));
958 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
961 return (linker_debug_search_symbol(value, sym, diffp));
965 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
968 return (linker_debug_symbol_values(sym, symval));
972 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
976 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
980 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
981 * obey locking protocols, and offer a significantly less complex interface.
984 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
990 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
999 kern_kldload(struct thread *td, const char *file, int *fileid)
1002 struct pmckern_map_in pkm;
1004 const char *kldname, *modname;
1008 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1011 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1015 * It is possible that kldloaded module will attach a new ifnet,
1016 * so vnet context must be set when this ocurs.
1018 CURVNET_SET(TD_TO_VNET(td));
1021 * If file does not contain a qualified name or any dot in it
1022 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1025 if (index(file, '/') || index(file, '.')) {
1034 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1044 pkm.pm_file = lf->filename;
1045 pkm.pm_address = (uintptr_t) lf->address;
1046 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
1058 kldload(struct thread *td, struct kldload_args *uap)
1060 char *pathname = NULL;
1063 td->td_retval[0] = -1;
1065 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1066 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1068 error = kern_kldload(td, pathname, &fileid);
1070 td->td_retval[0] = fileid;
1072 free(pathname, M_TEMP);
1077 kern_kldunload(struct thread *td, int fileid, int flags)
1080 struct pmckern_map_out pkm;
1085 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1088 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1091 CURVNET_SET(TD_TO_VNET(td));
1093 lf = linker_find_file_by_id(fileid);
1095 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1097 /* Check if there are DTrace probes enabled on this file. */
1098 if (lf->nenabled > 0) {
1099 printf("kldunload: attempt to unload file that has"
1100 " DTrace probes enabled\n");
1102 } else if (lf->userrefs == 0) {
1104 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1106 printf("kldunload: attempt to unload file that was"
1107 " loaded by the kernel\n");
1111 /* Save data needed by hwpmc(4) before unloading. */
1112 pkm.pm_address = (uintptr_t) lf->address;
1113 pkm.pm_size = lf->size;
1116 error = linker_file_unload(lf, flags);
1126 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
1138 kldunload(struct thread *td, struct kldunload_args *uap)
1141 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1145 kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1148 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1149 uap->flags != LINKER_UNLOAD_FORCE)
1151 return (kern_kldunload(td, uap->fileid, uap->flags));
1155 kldfind(struct thread *td, struct kldfind_args *uap)
1158 const char *filename;
1163 error = mac_kld_check_stat(td->td_ucred);
1168 td->td_retval[0] = -1;
1170 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1171 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1174 filename = linker_basename(pathname);
1176 lf = linker_find_file_by_name(filename);
1178 td->td_retval[0] = lf->id;
1183 free(pathname, M_TEMP);
1188 kldnext(struct thread *td, struct kldnext_args *uap)
1194 error = mac_kld_check_stat(td->td_ucred);
1200 if (uap->fileid == 0)
1201 lf = TAILQ_FIRST(&linker_files);
1203 lf = linker_find_file_by_id(uap->fileid);
1208 lf = TAILQ_NEXT(lf, link);
1211 /* Skip partially loaded files. */
1212 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1213 lf = TAILQ_NEXT(lf, link);
1216 td->td_retval[0] = lf->id;
1218 td->td_retval[0] = 0;
1225 kldstat(struct thread *td, struct kldstat_args *uap)
1227 struct kld_file_stat stat;
1231 * Check the version of the user's structure.
1233 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1236 if (version != sizeof(struct kld_file_stat_1) &&
1237 version != sizeof(struct kld_file_stat))
1240 error = kern_kldstat(td, uap->fileid, &stat);
1243 return (copyout(&stat, uap->stat, version));
1247 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1254 error = mac_kld_check_stat(td->td_ucred);
1260 lf = linker_find_file_by_id(fileid);
1266 /* Version 1 fields: */
1267 namelen = strlen(lf->filename) + 1;
1268 if (namelen > MAXPATHLEN)
1269 namelen = MAXPATHLEN;
1270 bcopy(lf->filename, &stat->name[0], namelen);
1271 stat->refs = lf->refs;
1273 stat->address = lf->address;
1274 stat->size = lf->size;
1275 /* Version 2 fields: */
1276 namelen = strlen(lf->pathname) + 1;
1277 if (namelen > MAXPATHLEN)
1278 namelen = MAXPATHLEN;
1279 bcopy(lf->pathname, &stat->pathname[0], namelen);
1282 td->td_retval[0] = 0;
1287 kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1294 error = mac_kld_check_stat(td->td_ucred);
1300 lf = linker_find_file_by_id(uap->fileid);
1303 mp = TAILQ_FIRST(&lf->modules);
1305 td->td_retval[0] = module_getid(mp);
1307 td->td_retval[0] = 0;
1316 kldsym(struct thread *td, struct kldsym_args *uap)
1318 char *symstr = NULL;
1320 linker_symval_t symval;
1322 struct kld_sym_lookup lookup;
1326 error = mac_kld_check_stat(td->td_ucred);
1331 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1333 if (lookup.version != sizeof(lookup) ||
1334 uap->cmd != KLDSYM_LOOKUP)
1336 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1337 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1340 if (uap->fileid != 0) {
1341 lf = linker_find_file_by_id(uap->fileid);
1344 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1345 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1346 lookup.symvalue = (uintptr_t) symval.value;
1347 lookup.symsize = symval.size;
1348 error = copyout(&lookup, uap->data, sizeof(lookup));
1352 TAILQ_FOREACH(lf, &linker_files, link) {
1353 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1354 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1355 lookup.symvalue = (uintptr_t)symval.value;
1356 lookup.symsize = symval.size;
1357 error = copyout(&lookup, uap->data,
1367 free(symstr, M_TEMP);
1372 * Preloaded module support
1376 modlist_lookup(const char *name, int ver)
1380 TAILQ_FOREACH(mod, &found_modules, link) {
1381 if (strcmp(mod->name, name) == 0 &&
1382 (ver == 0 || mod->version == ver))
1389 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1391 modlist_t mod, bestmod;
1394 if (verinfo == NULL)
1395 return (modlist_lookup(name, 0));
1397 TAILQ_FOREACH(mod, &found_modules, link) {
1398 if (strcmp(mod->name, name) != 0)
1401 if (ver == verinfo->md_ver_preferred)
1403 if (ver >= verinfo->md_ver_minimum &&
1404 ver <= verinfo->md_ver_maximum &&
1405 (bestmod == NULL || ver > bestmod->version))
1412 modlist_newmodule(const char *modname, int version, linker_file_t container)
1416 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1418 panic("no memory for module list");
1419 mod->container = container;
1420 mod->name = modname;
1421 mod->version = version;
1422 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1427 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1428 struct mod_metadata **stop, int preload)
1430 struct mod_metadata *mp, **mdp;
1431 const char *modname;
1434 for (mdp = start; mdp < stop; mdp++) {
1436 if (mp->md_type != MDT_VERSION)
1438 modname = mp->md_cval;
1439 ver = ((struct mod_version *)mp->md_data)->mv_version;
1440 if (modlist_lookup(modname, ver) != NULL) {
1441 printf("module %s already present!\n", modname);
1442 /* XXX what can we do? this is a build error. :-( */
1445 modlist_newmodule(modname, ver, lf);
1450 linker_preload(void *arg)
1453 const char *modname, *nmodname;
1455 linker_file_t lf, nlf;
1458 linker_file_list_t loaded_files;
1459 linker_file_list_t depended_files;
1460 struct mod_metadata *mp, *nmp;
1461 struct mod_metadata **start, **stop, **mdp, **nmdp;
1462 struct mod_depend *verinfo;
1466 struct sysinit **si_start, **si_stop;
1468 TAILQ_INIT(&loaded_files);
1469 TAILQ_INIT(&depended_files);
1470 TAILQ_INIT(&found_modules);
1474 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1475 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1476 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1477 if (modname == NULL) {
1478 printf("Preloaded module at %p does not have a"
1479 " name!\n", modptr);
1482 if (modtype == NULL) {
1483 printf("Preloaded module at %p does not have a type!\n",
1488 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1491 TAILQ_FOREACH(lc, &classes, link) {
1492 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1498 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1502 * First get a list of stuff in the kernel.
1504 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1506 linker_addmodules(linker_kernel_file, start, stop, 1);
1509 * This is a once-off kinky bubble sort to resolve relocation
1510 * dependency requirements.
1513 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1514 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1517 * First, look to see if we would successfully link with this
1520 resolves = 1; /* unless we know otherwise */
1522 for (mdp = start; mdp < stop; mdp++) {
1524 if (mp->md_type != MDT_DEPEND)
1526 modname = mp->md_cval;
1527 verinfo = mp->md_data;
1528 for (nmdp = start; nmdp < stop; nmdp++) {
1530 if (nmp->md_type != MDT_VERSION)
1532 nmodname = nmp->md_cval;
1533 if (strcmp(modname, nmodname) == 0)
1536 if (nmdp < stop) /* it's a self reference */
1540 * ok, the module isn't here yet, we
1543 if (modlist_lookup2(modname, verinfo) == NULL)
1548 * OK, if we found our modules, we can link. So, "provide"
1549 * the modules inside and add it to the end of the link order
1554 for (mdp = start; mdp < stop; mdp++) {
1556 if (mp->md_type != MDT_VERSION)
1558 modname = mp->md_cval;
1559 nver = ((struct mod_version *)
1560 mp->md_data)->mv_version;
1561 if (modlist_lookup(modname,
1563 printf("module %s already"
1564 " present!\n", modname);
1565 TAILQ_REMOVE(&loaded_files,
1567 linker_file_unload(lf,
1568 LINKER_UNLOAD_FORCE);
1569 /* we changed tailq next ptr */
1572 modlist_newmodule(modname, nver, lf);
1575 TAILQ_REMOVE(&loaded_files, lf, loaded);
1576 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1578 * Since we provided modules, we need to restart the
1579 * sort so that the previous files that depend on us
1580 * have a chance. Also, we've busted the tailq next
1581 * pointer with the REMOVE.
1588 * At this point, we check to see what could not be resolved..
1590 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1591 TAILQ_REMOVE(&loaded_files, lf, loaded);
1592 printf("KLD file %s is missing dependencies\n", lf->filename);
1593 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1597 * We made it. Finish off the linking in the order we determined.
1599 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1600 if (linker_kernel_file) {
1601 linker_kernel_file->refs++;
1602 error = linker_file_add_dependency(lf,
1603 linker_kernel_file);
1605 panic("cannot add dependency");
1607 lf->userrefs++; /* so we can (try to) kldunload it */
1608 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1611 for (mdp = start; mdp < stop; mdp++) {
1613 if (mp->md_type != MDT_DEPEND)
1615 modname = mp->md_cval;
1616 verinfo = mp->md_data;
1617 mod = modlist_lookup2(modname, verinfo);
1618 /* Don't count self-dependencies */
1619 if (lf == mod->container)
1621 mod->container->refs++;
1622 error = linker_file_add_dependency(lf,
1625 panic("cannot add dependency");
1629 * Now do relocation etc using the symbol search paths
1630 * established by the dependencies
1632 error = LINKER_LINK_PRELOAD_FINISH(lf);
1634 TAILQ_REMOVE(&depended_files, lf, loaded);
1635 printf("KLD file %s - could not finalize loading\n",
1637 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1640 linker_file_register_modules(lf);
1641 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1642 &si_stop, NULL) == 0)
1643 sysinit_add(si_start, si_stop);
1644 linker_file_register_sysctls(lf);
1645 lf->flags |= LINKER_FILE_LINKED;
1647 /* woohoo! we made it! */
1650 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1653 * Search for a not-loaded module by name.
1655 * Modules may be found in the following locations:
1657 * - preloaded (result is just the module name) - on disk (result is full path
1660 * If the module name is qualified in any way (contains path, etc.) the we
1661 * simply return a copy of it.
1663 * The search path can be manipulated via sysctl. Note that we use the ';'
1664 * character as a separator to be consistent with the bootloader.
1667 static char linker_hintfile[] = "linker.hints";
1668 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1670 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1671 sizeof(linker_path), "module load search path");
1673 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1675 static char *linker_ext_list[] = {
1682 * Check if file actually exists either with or without extension listed in
1683 * the linker_ext_list. (probably should be generic for the rest of the
1687 linker_lookup_file(const char *path, int pathlen, const char *name,
1688 int namelen, struct vattr *vap)
1690 struct nameidata nd;
1691 struct thread *td = curthread; /* XXX */
1692 char *result, **cpp, *sep;
1693 int error, len, extlen, reclen, flags, vfslocked;
1697 for (cpp = linker_ext_list; *cpp; cpp++) {
1702 extlen++; /* trailing '\0' */
1703 sep = (path[pathlen - 1] != '/') ? "/" : "";
1705 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1706 result = malloc(reclen, M_LINKER, M_WAITOK);
1707 for (cpp = linker_ext_list; *cpp; cpp++) {
1708 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1709 namelen, name, *cpp);
1711 * Attempt to open the file, and return the path if
1712 * we succeed and it's a regular file.
1714 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
1716 error = vn_open(&nd, &flags, 0, NULL);
1718 vfslocked = NDHASGIANT(&nd);
1719 NDFREE(&nd, NDF_ONLY_PNBUF);
1720 type = nd.ni_vp->v_type;
1722 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1723 VOP_UNLOCK(nd.ni_vp, 0);
1724 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1725 VFS_UNLOCK_GIANT(vfslocked);
1730 free(result, M_LINKER);
1734 #define INT_ALIGN(base, ptr) ptr = \
1735 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1738 * Lookup KLD which contains requested module in the "linker.hints" file. If
1739 * version specification is available, then try to find the best KLD.
1740 * Otherwise just find the latest one.
1743 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1744 int modnamelen, struct mod_depend *verinfo)
1746 struct thread *td = curthread; /* XXX */
1747 struct ucred *cred = td ? td->td_ucred : NULL;
1748 struct nameidata nd;
1749 struct vattr vattr, mattr;
1750 u_char *hints = NULL;
1751 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1752 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1756 bestver = found = 0;
1758 sep = (path[pathlen - 1] != '/') ? "/" : "";
1759 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1761 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1762 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1765 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
1767 error = vn_open(&nd, &flags, 0, NULL);
1770 vfslocked = NDHASGIANT(&nd);
1771 NDFREE(&nd, NDF_ONLY_PNBUF);
1772 if (nd.ni_vp->v_type != VREG)
1775 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1779 * XXX: we need to limit this number to some reasonable value
1781 if (vattr.va_size > 100 * 1024) {
1782 printf("hints file too large %ld\n", (long)vattr.va_size);
1785 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1788 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1789 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1792 VOP_UNLOCK(nd.ni_vp, 0);
1793 vn_close(nd.ni_vp, FREAD, cred, td);
1794 VFS_UNLOCK_GIANT(vfslocked);
1797 printf("can't read %d\n", reclen);
1800 intp = (int *)hints;
1802 if (ival != LINKER_HINTS_VERSION) {
1803 printf("hints file version mismatch %d\n", ival);
1806 bufend = hints + vattr.va_size;
1807 recptr = (u_char *)intp;
1809 while (recptr < bufend && !found) {
1810 intp = (int *)recptr;
1817 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1820 INT_ALIGN(hints, cp);
1824 if (verinfo == NULL ||
1825 ival == verinfo->md_ver_preferred) {
1829 if (ival >= verinfo->md_ver_minimum &&
1830 ival <= verinfo->md_ver_maximum &&
1840 recptr += reclen + sizeof(int);
1843 * Finally check if KLD is in the place
1846 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1848 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1851 * KLD is newer than hints file. What we should do now?
1853 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1854 printf("warning: KLD '%s' is newer than the linker.hints"
1857 free(pathbuf, M_LINKER);
1859 free(hints, M_TEMP);
1860 if (nd.ni_vp != NULL) {
1861 VOP_UNLOCK(nd.ni_vp, 0);
1862 vn_close(nd.ni_vp, FREAD, cred, td);
1863 VFS_UNLOCK_GIANT(vfslocked);
1866 * If nothing found or hints is absent - fallback to the old
1867 * way by using "kldname[.ko]" as module name.
1869 if (!found && !bestver && result == NULL)
1870 result = linker_lookup_file(path, pathlen, modname,
1876 * Lookup KLD which contains requested module in the all directories.
1879 linker_search_module(const char *modname, int modnamelen,
1880 struct mod_depend *verinfo)
1882 char *cp, *ep, *result;
1885 * traverse the linker path
1887 for (cp = linker_path; *cp; cp = ep + 1) {
1888 /* find the end of this component */
1889 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1890 result = linker_hints_lookup(cp, ep - cp, modname,
1891 modnamelen, verinfo);
1901 * Search for module in all directories listed in the linker_path.
1904 linker_search_kld(const char *name)
1906 char *cp, *ep, *result;
1909 /* qualified at all? */
1910 if (index(name, '/'))
1911 return (linker_strdup(name));
1913 /* traverse the linker path */
1915 for (ep = linker_path; *ep; ep++) {
1917 /* find the end of this component */
1918 for (; *ep != 0 && *ep != ';'; ep++);
1919 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1927 linker_basename(const char *path)
1929 const char *filename;
1931 filename = rindex(path, '/');
1932 if (filename == NULL)
1941 * Inform hwpmc about the set of kernel modules currently loaded.
1944 linker_hwpmc_list_objects(void)
1947 struct pmckern_map_in *kobase;
1952 TAILQ_FOREACH(lf, &linker_files, link)
1955 /* Allocate nmappings + 1 entries. */
1956 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1957 M_LINKER, M_WAITOK | M_ZERO);
1959 TAILQ_FOREACH(lf, &linker_files, link) {
1961 /* Save the info for this linker file. */
1962 kobase[i].pm_file = lf->filename;
1963 kobase[i].pm_address = (uintptr_t)lf->address;
1968 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
1970 /* The last entry of the malloced area comprises of all zeros. */
1971 KASSERT(kobase[i].pm_file == NULL,
1972 ("linker_hwpmc_list_objects: last object not NULL"));
1974 return ((void *)kobase);
1979 * Find a file which contains given module and load it, if "parent" is not
1980 * NULL, register a reference to it.
1983 linker_load_module(const char *kldname, const char *modname,
1984 struct linker_file *parent, struct mod_depend *verinfo,
1985 struct linker_file **lfpp)
1987 linker_file_t lfdep;
1988 const char *filename;
1993 if (modname == NULL) {
1995 * We have to load KLD
1997 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1999 pathname = linker_search_kld(kldname);
2001 if (modlist_lookup2(modname, verinfo) != NULL)
2003 if (kldname != NULL)
2004 pathname = linker_strdup(kldname);
2005 else if (rootvnode == NULL)
2009 * Need to find a KLD with required module
2011 pathname = linker_search_module(modname,
2012 strlen(modname), verinfo);
2014 if (pathname == NULL)
2018 * Can't load more than one file with the same basename XXX:
2019 * Actually it should be possible to have multiple KLDs with
2020 * the same basename but different path because they can
2021 * provide different versions of the same modules.
2023 filename = linker_basename(pathname);
2024 if (linker_find_file_by_name(filename))
2027 error = linker_load_file(pathname, &lfdep);
2030 if (modname && verinfo &&
2031 modlist_lookup2(modname, verinfo) == NULL) {
2032 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2037 error = linker_file_add_dependency(parent, lfdep);
2044 free(pathname, M_LINKER);
2049 * This routine is responsible for finding dependencies of userland initiated
2050 * kldload(2)'s of files.
2053 linker_load_dependencies(linker_file_t lf)
2055 linker_file_t lfdep;
2056 struct mod_metadata **start, **stop, **mdp, **nmdp;
2057 struct mod_metadata *mp, *nmp;
2058 struct mod_depend *verinfo;
2060 const char *modname, *nmodname;
2061 int ver, error = 0, count;
2064 * All files are dependant on /kernel.
2067 if (linker_kernel_file) {
2068 linker_kernel_file->refs++;
2069 error = linker_file_add_dependency(lf, linker_kernel_file);
2073 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2076 for (mdp = start; mdp < stop; mdp++) {
2078 if (mp->md_type != MDT_VERSION)
2080 modname = mp->md_cval;
2081 ver = ((struct mod_version *)mp->md_data)->mv_version;
2082 mod = modlist_lookup(modname, ver);
2084 printf("interface %s.%d already present in the KLD"
2085 " '%s'!\n", modname, ver,
2086 mod->container->filename);
2091 for (mdp = start; mdp < stop; mdp++) {
2093 if (mp->md_type != MDT_DEPEND)
2095 modname = mp->md_cval;
2096 verinfo = mp->md_data;
2098 for (nmdp = start; nmdp < stop; nmdp++) {
2100 if (nmp->md_type != MDT_VERSION)
2102 nmodname = nmp->md_cval;
2103 if (strcmp(modname, nmodname) == 0)
2106 if (nmdp < stop)/* early exit, it's a self reference */
2108 mod = modlist_lookup2(modname, verinfo);
2109 if (mod) { /* woohoo, it's loaded already */
2110 lfdep = mod->container;
2112 error = linker_file_add_dependency(lf, lfdep);
2117 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2119 printf("KLD %s: depends on %s - not available or"
2120 " version mismatch\n", lf->filename, modname);
2127 linker_addmodules(lf, start, stop, 0);
2132 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2134 struct sysctl_req *req;
2137 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2141 * Export a nul-separated, double-nul-terminated list of all function names
2145 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2151 error = mac_kld_check_stat(req->td->td_ucred);
2155 error = sysctl_wire_old_buffer(req, 0);
2159 TAILQ_FOREACH(lf, &linker_files, link) {
2160 error = LINKER_EACH_FUNCTION_NAME(lf,
2161 sysctl_kern_function_list_iterate, req);
2168 return (SYSCTL_OUT(req, "", 1));
2171 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
2172 NULL, 0, sysctl_kern_function_list, "", "kernel function list");