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_DOWNGRADE() sx_downgrade(&kld_sx)
74 #define KLD_LOCK_READ() sx_slock(&kld_sx)
75 #define KLD_UNLOCK_READ() sx_sunlock(&kld_sx)
76 #define KLD_LOCKED() sx_xlocked(&kld_sx)
77 #define KLD_LOCK_ASSERT() do { \
79 sx_assert(&kld_sx, SX_XLOCKED); \
83 * static char *linker_search_path(const char *name, struct mod_depend
86 static const char *linker_basename(const char *path);
89 * Find a currently loaded file given its filename.
91 static linker_file_t linker_find_file_by_name(const char* _filename);
94 * Find a currently loaded file given its file id.
96 static linker_file_t linker_find_file_by_id(int _fileid);
98 /* Metadata from the static kernel */
99 SET_DECLARE(modmetadata_set, struct mod_metadata);
101 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
103 linker_file_t linker_kernel_file;
105 static struct sx kld_sx; /* kernel linker lock */
108 * Load counter used by clients to determine if a linker file has been
109 * re-loaded. This counter is incremented for each file load.
113 static linker_class_list_t classes;
114 static linker_file_list_t linker_files;
115 static int next_file_id = 1;
116 static int linker_no_more_classes = 0;
118 #define LINKER_GET_NEXT_FILE_ID(a) do { \
119 linker_file_t lftmp; \
123 TAILQ_FOREACH(lftmp, &linker_files, link) { \
124 if (next_file_id == lftmp->id) { \
129 (a) = next_file_id; \
133 /* XXX wrong name; we're looking at version provision tags here, not modules */
134 typedef TAILQ_HEAD(, modlist) modlisthead_t;
136 TAILQ_ENTRY(modlist) link; /* chain together all modules */
137 linker_file_t container;
141 typedef struct modlist *modlist_t;
142 static modlisthead_t found_modules;
144 static int linker_file_add_dependency(linker_file_t file,
146 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
147 const char* name, int deps);
148 static int linker_load_module(const char *kldname,
149 const char *modname, struct linker_file *parent,
150 struct mod_depend *verinfo, struct linker_file **lfpp);
151 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
154 linker_strdup(const char *str)
158 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
164 linker_init(void *arg)
167 sx_init(&kld_sx, "kernel linker");
168 TAILQ_INIT(&classes);
169 TAILQ_INIT(&linker_files);
172 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
175 linker_stop_class_add(void *arg)
178 linker_no_more_classes = 1;
181 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
184 linker_add_class(linker_class_t lc)
188 * We disallow any class registration past SI_ORDER_ANY
189 * of SI_SUB_KLD. We bump the reference count to keep the
190 * ops from being freed.
192 if (linker_no_more_classes == 1)
194 kobj_class_compile((kobj_class_t) lc);
195 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
196 TAILQ_INSERT_TAIL(&classes, lc, link);
201 linker_file_sysinit(linker_file_t lf)
203 struct sysinit **start, **stop, **sipp, **xipp, *save;
205 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
208 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
211 * Perform a bubble sort of the system initialization objects by
212 * their subsystem (primary key) and order (secondary key).
214 * Since some things care about execution order, this is the operation
215 * which ensures continued function.
217 for (sipp = start; sipp < stop; sipp++) {
218 for (xipp = sipp + 1; xipp < stop; xipp++) {
219 if ((*sipp)->subsystem < (*xipp)->subsystem ||
220 ((*sipp)->subsystem == (*xipp)->subsystem &&
221 (*sipp)->order <= (*xipp)->order))
230 * Traverse the (now) ordered list of system initialization tasks.
231 * Perform each task, and continue on to the next task.
234 for (sipp = start; sipp < stop; sipp++) {
235 if ((*sipp)->subsystem == SI_SUB_DUMMY)
236 continue; /* skip dummy task(s) */
239 (*((*sipp)->func)) ((*sipp)->udata);
245 linker_file_sysuninit(linker_file_t lf)
247 struct sysinit **start, **stop, **sipp, **xipp, *save;
249 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
252 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
257 * Perform a reverse bubble sort of the system initialization objects
258 * by their subsystem (primary key) and order (secondary key).
260 * Since some things care about execution order, this is the operation
261 * which ensures continued function.
263 for (sipp = start; sipp < stop; sipp++) {
264 for (xipp = sipp + 1; xipp < stop; xipp++) {
265 if ((*sipp)->subsystem > (*xipp)->subsystem ||
266 ((*sipp)->subsystem == (*xipp)->subsystem &&
267 (*sipp)->order >= (*xipp)->order))
276 * Traverse the (now) ordered list of system initialization tasks.
277 * Perform each task, and continue on to the next task.
280 for (sipp = start; sipp < stop; sipp++) {
281 if ((*sipp)->subsystem == SI_SUB_DUMMY)
282 continue; /* skip dummy task(s) */
285 (*((*sipp)->func)) ((*sipp)->udata);
291 linker_file_register_sysctls(linker_file_t lf)
293 struct sysctl_oid **start, **stop, **oidp;
296 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
299 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
303 for (oidp = start; oidp < stop; oidp++)
304 sysctl_register_oid(*oidp);
309 linker_file_unregister_sysctls(linker_file_t lf)
311 struct sysctl_oid **start, **stop, **oidp;
313 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
314 " for %s\n", lf->filename));
316 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
320 for (oidp = start; oidp < stop; oidp++)
321 sysctl_unregister_oid(*oidp);
326 linker_file_register_modules(linker_file_t lf)
328 struct mod_metadata **start, **stop, **mdp;
329 const moduledata_t *moddata;
330 int first_error, error;
332 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
333 " in %s\n", lf->filename));
335 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
338 * This fallback should be unnecessary, but if we get booted
339 * from boot2 instead of loader and we are missing our
340 * metadata then we have to try the best we can.
342 if (lf == linker_kernel_file) {
343 start = SET_BEGIN(modmetadata_set);
344 stop = SET_LIMIT(modmetadata_set);
349 for (mdp = start; mdp < stop; mdp++) {
350 if ((*mdp)->md_type != MDT_MODULE)
352 moddata = (*mdp)->md_data;
353 KLD_DPF(FILE, ("Registering module %s in %s\n",
354 moddata->name, lf->filename));
355 error = module_register(moddata, lf);
357 printf("Module %s failed to register: %d\n",
358 moddata->name, error);
359 if (first_error == 0)
363 return (first_error);
367 linker_init_kernel_modules(void)
370 linker_file_register_modules(linker_kernel_file);
373 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
377 linker_load_file(const char *filename, linker_file_t *result)
381 int foundfile, error;
383 /* Refuse to load modules if securelevel raised */
384 if (prison0.pr_securelevel > 0)
388 lf = linker_find_file_by_name(filename);
390 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
391 " incrementing refs\n", filename));
400 * We do not need to protect (lock) classes here because there is
401 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
402 * and there is no class deregistration mechanism at this time.
404 TAILQ_FOREACH(lc, &classes, link) {
405 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
407 error = LINKER_LOAD_FILE(lc, filename, &lf);
409 * If we got something other than ENOENT, then it exists but
410 * we cannot load it for some other reason.
415 error = linker_file_register_modules(lf);
416 if (error == EEXIST) {
417 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
421 linker_file_register_sysctls(lf);
422 linker_file_sysinit(lf);
424 lf->flags |= LINKER_FILE_LINKED;
430 * Less than ideal, but tells the user whether it failed to load or
431 * the module was not found.
436 * If the file type has not been recognized by the last try
437 * printout a message before to fail.
440 printf("linker_load_file: Unsupported file type\n");
443 * Format not recognized or otherwise unloadable.
444 * When loading a module that is statically built into
445 * the kernel EEXIST percolates back up as the return
446 * value. Preserve this so that apps like sysinstall
447 * can recognize this special case and not post bogus
453 error = ENOENT; /* Nothing found */
458 linker_reference_module(const char *modname, struct mod_depend *verinfo,
459 linker_file_t *result)
465 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
466 *result = mod->container;
472 error = linker_load_module(NULL, modname, NULL, verinfo, result);
478 linker_release_module(const char *modname, struct mod_depend *verinfo,
486 KASSERT(modname != NULL,
487 ("linker_release_module: no file or name"));
488 mod = modlist_lookup2(modname, verinfo);
495 KASSERT(modname == NULL && verinfo == NULL,
496 ("linker_release_module: both file and name"));
497 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
503 linker_find_file_by_name(const char *filename)
508 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
509 sprintf(koname, "%s.ko", filename);
512 TAILQ_FOREACH(lf, &linker_files, link) {
513 if (strcmp(lf->filename, koname) == 0)
515 if (strcmp(lf->filename, filename) == 0)
518 free(koname, M_LINKER);
523 linker_find_file_by_id(int fileid)
528 TAILQ_FOREACH(lf, &linker_files, link)
529 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
535 linker_file_foreach(linker_predicate_t *predicate, void *context)
541 TAILQ_FOREACH(lf, &linker_files, link) {
542 retval = predicate(lf, context);
551 linker_make_file(const char *pathname, linker_class_t lc)
554 const char *filename;
557 filename = linker_basename(pathname);
559 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
560 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
566 lf->filename = linker_strdup(filename);
567 lf->pathname = linker_strdup(pathname);
568 LINKER_GET_NEXT_FILE_ID(lf->id);
571 lf->loadcnt = ++loadcnt;
572 lf->sdt_probes = NULL;
574 STAILQ_INIT(&lf->common);
575 TAILQ_INIT(&lf->modules);
576 TAILQ_INSERT_TAIL(&linker_files, lf, link);
581 linker_file_unload(linker_file_t file, int flags)
584 modlist_t ml, nextml;
585 struct common_symbol *cp;
588 /* Refuse to unload modules if securelevel raised. */
589 if (prison0.pr_securelevel > 0)
593 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
595 /* Easy case of just dropping a reference. */
596 if (file->refs > 1) {
601 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
602 " informing modules\n"));
605 * Quiesce all the modules to give them a chance to veto the unload.
608 for (mod = TAILQ_FIRST(&file->modules); mod;
609 mod = module_getfnext(mod)) {
611 error = module_quiesce(mod);
612 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
613 KLD_DPF(FILE, ("linker_file_unload: module %s"
614 " vetoed unload\n", module_getname(mod)));
616 * XXX: Do we need to tell all the quiesced modules
617 * that they can resume work now via a new module
627 * Inform any modules associated with this file that they are
631 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
632 next = module_getfnext(mod);
636 * Give the module a chance to veto the unload.
638 if ((error = module_unload(mod)) != 0) {
639 KLD_DPF(FILE, ("linker_file_unload: module %s"
640 " failed unload\n", module_getname(mod)));
648 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
649 if (ml->container == file) {
650 TAILQ_REMOVE(&found_modules, ml, link);
656 * Don't try to run SYSUNINITs if we are unloaded due to a
659 if (file->flags & LINKER_FILE_LINKED) {
660 file->flags &= ~LINKER_FILE_LINKED;
662 linker_file_sysuninit(file);
663 linker_file_unregister_sysctls(file);
666 TAILQ_REMOVE(&linker_files, file, link);
669 for (i = 0; i < file->ndeps; i++)
670 linker_file_unload(file->deps[i], flags);
671 free(file->deps, M_LINKER);
674 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
675 STAILQ_REMOVE_HEAD(&file->common, link);
680 if (file->filename) {
681 free(file->filename, M_LINKER);
682 file->filename = NULL;
684 if (file->pathname) {
685 free(file->pathname, M_LINKER);
686 file->pathname = NULL;
688 kobj_delete((kobj_t) file, M_LINKER);
693 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
695 return (LINKER_CTF_GET(file, lc));
699 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
701 linker_file_t *newdeps;
704 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
705 M_LINKER, M_WAITOK | M_ZERO);
710 bcopy(file->deps, newdeps,
711 file->ndeps * sizeof(linker_file_t *));
712 free(file->deps, M_LINKER);
714 file->deps = newdeps;
715 file->deps[file->ndeps] = dep;
717 KLD_DPF(FILE, ("linker_file_add_dependency:"
718 " adding %s as dependency for %s\n",
719 dep->filename, file->filename));
724 * Locate a linker set and its contents. This is a helper function to avoid
725 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
726 * This function is used in this file so we can avoid having lots of (void **)
730 linker_file_lookup_set(linker_file_t file, const char *name,
731 void *firstp, void *lastp, int *countp)
735 locked = KLD_LOCKED();
738 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
745 * List all functions in a file.
748 linker_file_function_listall(linker_file_t lf,
749 linker_function_nameval_callback_t callback_func, void *arg)
751 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
755 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
760 locked = KLD_LOCKED();
763 sym = linker_file_lookup_symbol_internal(file, name, deps);
770 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
774 linker_symval_t symval;
776 size_t common_size = 0;
780 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
783 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
784 LINKER_SYMBOL_VALUES(file, sym, &symval);
785 if (symval.value == 0)
787 * For commons, first look them up in the
788 * dependencies and only allocate space if not found
791 common_size = symval.size;
793 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
794 ".value=%p\n", symval.value));
795 return (symval.value);
799 for (i = 0; i < file->ndeps; i++) {
800 address = linker_file_lookup_symbol_internal(
801 file->deps[i], name, 0);
803 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
804 " deps value=%p\n", address));
809 if (common_size > 0) {
811 * This is a common symbol which was not found in the
812 * dependencies. We maintain a simple common symbol table in
815 struct common_symbol *cp;
817 STAILQ_FOREACH(cp, &file->common, link) {
818 if (strcmp(cp->name, name) == 0) {
819 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
820 " old common value=%p\n", cp->address));
821 return (cp->address);
825 * Round the symbol size up to align.
827 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
828 cp = malloc(sizeof(struct common_symbol)
829 + common_size + strlen(name) + 1, M_LINKER,
831 cp->address = (caddr_t)(cp + 1);
832 cp->name = cp->address + common_size;
833 strcpy(cp->name, name);
834 bzero(cp->address, common_size);
835 STAILQ_INSERT_TAIL(&file->common, cp, link);
837 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
838 " value=%p\n", cp->address));
839 return (cp->address);
841 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
846 * Both DDB and stack(9) rely on the kernel linker to provide forward and
847 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
848 * do this in a lockfree manner. We provide a set of internal helper
849 * routines to perform these operations without locks, and then wrappers that
852 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
856 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
860 TAILQ_FOREACH(lf, &linker_files, link) {
861 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
869 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
872 c_linker_sym_t best, es;
873 u_long diff, bestdiff, off;
876 off = (uintptr_t)value;
878 TAILQ_FOREACH(lf, &linker_files, link) {
879 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
881 if (es != 0 && diff < bestdiff) {
900 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
904 TAILQ_FOREACH(lf, &linker_files, link) {
905 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
912 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
915 linker_symval_t symval;
920 error = linker_debug_search_symbol(value, &sym, offset);
923 error = linker_debug_symbol_values(sym, &symval);
926 strlcpy(buf, symval.name, buflen);
931 * DDB Helpers. DDB has to look across multiple files with their own symbol
932 * tables and string tables.
934 * Note that we do not obey list locking protocols here. We really don't need
935 * DDB to hang because somebody's got the lock held. We'll take the chance
936 * that the files list is inconsistant instead.
940 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
943 return (linker_debug_lookup(symstr, sym));
948 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
951 return (linker_debug_search_symbol(value, sym, diffp));
955 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
958 return (linker_debug_symbol_values(sym, symval));
962 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
966 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
970 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
971 * obey locking protocols, and offer a significantly less complex interface.
974 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
980 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
989 kern_kldload(struct thread *td, const char *file, int *fileid)
992 struct pmckern_map_in pkm;
994 const char *kldname, *modname;
998 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1001 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1005 * It is possible that kldloaded module will attach a new ifnet,
1006 * so vnet context must be set when this ocurs.
1008 CURVNET_SET(TD_TO_VNET(td));
1011 * If file does not contain a qualified name or any dot in it
1012 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1015 if (index(file, '/') || index(file, '.')) {
1024 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1034 pkm.pm_file = lf->filename;
1035 pkm.pm_address = (uintptr_t) lf->address;
1036 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
1048 sys_kldload(struct thread *td, struct kldload_args *uap)
1050 char *pathname = NULL;
1053 td->td_retval[0] = -1;
1055 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1056 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1058 error = kern_kldload(td, pathname, &fileid);
1060 td->td_retval[0] = fileid;
1062 free(pathname, M_TEMP);
1067 kern_kldunload(struct thread *td, int fileid, int flags)
1070 struct pmckern_map_out pkm;
1075 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1078 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1081 CURVNET_SET(TD_TO_VNET(td));
1083 lf = linker_find_file_by_id(fileid);
1085 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1087 /* Check if there are DTrace probes enabled on this file. */
1088 if (lf->nenabled > 0) {
1089 printf("kldunload: attempt to unload file that has"
1090 " DTrace probes enabled\n");
1092 } else if (lf->userrefs == 0) {
1094 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1096 printf("kldunload: attempt to unload file that was"
1097 " loaded by the kernel\n");
1101 /* Save data needed by hwpmc(4) before unloading. */
1102 pkm.pm_address = (uintptr_t) lf->address;
1103 pkm.pm_size = lf->size;
1106 error = linker_file_unload(lf, flags);
1116 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
1128 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1131 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1135 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1138 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1139 uap->flags != LINKER_UNLOAD_FORCE)
1141 return (kern_kldunload(td, uap->fileid, uap->flags));
1145 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1148 const char *filename;
1153 error = mac_kld_check_stat(td->td_ucred);
1158 td->td_retval[0] = -1;
1160 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1161 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1164 filename = linker_basename(pathname);
1166 lf = linker_find_file_by_name(filename);
1168 td->td_retval[0] = lf->id;
1173 free(pathname, M_TEMP);
1178 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1184 error = mac_kld_check_stat(td->td_ucred);
1190 if (uap->fileid == 0)
1191 lf = TAILQ_FIRST(&linker_files);
1193 lf = linker_find_file_by_id(uap->fileid);
1198 lf = TAILQ_NEXT(lf, link);
1201 /* Skip partially loaded files. */
1202 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1203 lf = TAILQ_NEXT(lf, link);
1206 td->td_retval[0] = lf->id;
1208 td->td_retval[0] = 0;
1215 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1217 struct kld_file_stat stat;
1221 * Check the version of the user's structure.
1223 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1226 if (version != sizeof(struct kld_file_stat_1) &&
1227 version != sizeof(struct kld_file_stat))
1230 error = kern_kldstat(td, uap->fileid, &stat);
1233 return (copyout(&stat, uap->stat, version));
1237 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1244 error = mac_kld_check_stat(td->td_ucred);
1250 lf = linker_find_file_by_id(fileid);
1256 /* Version 1 fields: */
1257 namelen = strlen(lf->filename) + 1;
1258 if (namelen > MAXPATHLEN)
1259 namelen = MAXPATHLEN;
1260 bcopy(lf->filename, &stat->name[0], namelen);
1261 stat->refs = lf->refs;
1263 stat->address = lf->address;
1264 stat->size = lf->size;
1265 /* Version 2 fields: */
1266 namelen = strlen(lf->pathname) + 1;
1267 if (namelen > MAXPATHLEN)
1268 namelen = MAXPATHLEN;
1269 bcopy(lf->pathname, &stat->pathname[0], namelen);
1272 td->td_retval[0] = 0;
1277 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1284 error = mac_kld_check_stat(td->td_ucred);
1290 lf = linker_find_file_by_id(uap->fileid);
1293 mp = TAILQ_FIRST(&lf->modules);
1295 td->td_retval[0] = module_getid(mp);
1297 td->td_retval[0] = 0;
1306 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1308 char *symstr = NULL;
1310 linker_symval_t symval;
1312 struct kld_sym_lookup lookup;
1316 error = mac_kld_check_stat(td->td_ucred);
1321 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1323 if (lookup.version != sizeof(lookup) ||
1324 uap->cmd != KLDSYM_LOOKUP)
1326 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1327 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1330 if (uap->fileid != 0) {
1331 lf = linker_find_file_by_id(uap->fileid);
1334 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1335 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1336 lookup.symvalue = (uintptr_t) symval.value;
1337 lookup.symsize = symval.size;
1338 error = copyout(&lookup, uap->data, sizeof(lookup));
1342 TAILQ_FOREACH(lf, &linker_files, link) {
1343 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1344 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1345 lookup.symvalue = (uintptr_t)symval.value;
1346 lookup.symsize = symval.size;
1347 error = copyout(&lookup, uap->data,
1357 free(symstr, M_TEMP);
1362 * Preloaded module support
1366 modlist_lookup(const char *name, int ver)
1370 TAILQ_FOREACH(mod, &found_modules, link) {
1371 if (strcmp(mod->name, name) == 0 &&
1372 (ver == 0 || mod->version == ver))
1379 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1381 modlist_t mod, bestmod;
1384 if (verinfo == NULL)
1385 return (modlist_lookup(name, 0));
1387 TAILQ_FOREACH(mod, &found_modules, link) {
1388 if (strcmp(mod->name, name) != 0)
1391 if (ver == verinfo->md_ver_preferred)
1393 if (ver >= verinfo->md_ver_minimum &&
1394 ver <= verinfo->md_ver_maximum &&
1395 (bestmod == NULL || ver > bestmod->version))
1402 modlist_newmodule(const char *modname, int version, linker_file_t container)
1406 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1408 panic("no memory for module list");
1409 mod->container = container;
1410 mod->name = modname;
1411 mod->version = version;
1412 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1417 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1418 struct mod_metadata **stop, int preload)
1420 struct mod_metadata *mp, **mdp;
1421 const char *modname;
1424 for (mdp = start; mdp < stop; mdp++) {
1426 if (mp->md_type != MDT_VERSION)
1428 modname = mp->md_cval;
1429 ver = ((struct mod_version *)mp->md_data)->mv_version;
1430 if (modlist_lookup(modname, ver) != NULL) {
1431 printf("module %s already present!\n", modname);
1432 /* XXX what can we do? this is a build error. :-( */
1435 modlist_newmodule(modname, ver, lf);
1440 linker_preload(void *arg)
1443 const char *modname, *nmodname;
1445 linker_file_t lf, nlf;
1448 linker_file_list_t loaded_files;
1449 linker_file_list_t depended_files;
1450 struct mod_metadata *mp, *nmp;
1451 struct mod_metadata **start, **stop, **mdp, **nmdp;
1452 struct mod_depend *verinfo;
1456 struct sysinit **si_start, **si_stop;
1458 TAILQ_INIT(&loaded_files);
1459 TAILQ_INIT(&depended_files);
1460 TAILQ_INIT(&found_modules);
1464 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1465 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1466 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1467 if (modname == NULL) {
1468 printf("Preloaded module at %p does not have a"
1469 " name!\n", modptr);
1472 if (modtype == NULL) {
1473 printf("Preloaded module at %p does not have a type!\n",
1478 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1481 TAILQ_FOREACH(lc, &classes, link) {
1482 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1488 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1492 * First get a list of stuff in the kernel.
1494 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1496 linker_addmodules(linker_kernel_file, start, stop, 1);
1499 * This is a once-off kinky bubble sort to resolve relocation
1500 * dependency requirements.
1503 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1504 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1507 * First, look to see if we would successfully link with this
1510 resolves = 1; /* unless we know otherwise */
1512 for (mdp = start; mdp < stop; mdp++) {
1514 if (mp->md_type != MDT_DEPEND)
1516 modname = mp->md_cval;
1517 verinfo = mp->md_data;
1518 for (nmdp = start; nmdp < stop; nmdp++) {
1520 if (nmp->md_type != MDT_VERSION)
1522 nmodname = nmp->md_cval;
1523 if (strcmp(modname, nmodname) == 0)
1526 if (nmdp < stop) /* it's a self reference */
1530 * ok, the module isn't here yet, we
1533 if (modlist_lookup2(modname, verinfo) == NULL)
1538 * OK, if we found our modules, we can link. So, "provide"
1539 * the modules inside and add it to the end of the link order
1544 for (mdp = start; mdp < stop; mdp++) {
1546 if (mp->md_type != MDT_VERSION)
1548 modname = mp->md_cval;
1549 nver = ((struct mod_version *)
1550 mp->md_data)->mv_version;
1551 if (modlist_lookup(modname,
1553 printf("module %s already"
1554 " present!\n", modname);
1555 TAILQ_REMOVE(&loaded_files,
1557 linker_file_unload(lf,
1558 LINKER_UNLOAD_FORCE);
1559 /* we changed tailq next ptr */
1562 modlist_newmodule(modname, nver, lf);
1565 TAILQ_REMOVE(&loaded_files, lf, loaded);
1566 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1568 * Since we provided modules, we need to restart the
1569 * sort so that the previous files that depend on us
1570 * have a chance. Also, we've busted the tailq next
1571 * pointer with the REMOVE.
1578 * At this point, we check to see what could not be resolved..
1580 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1581 TAILQ_REMOVE(&loaded_files, lf, loaded);
1582 printf("KLD file %s is missing dependencies\n", lf->filename);
1583 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1587 * We made it. Finish off the linking in the order we determined.
1589 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1590 if (linker_kernel_file) {
1591 linker_kernel_file->refs++;
1592 error = linker_file_add_dependency(lf,
1593 linker_kernel_file);
1595 panic("cannot add dependency");
1597 lf->userrefs++; /* so we can (try to) kldunload it */
1598 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1601 for (mdp = start; mdp < stop; mdp++) {
1603 if (mp->md_type != MDT_DEPEND)
1605 modname = mp->md_cval;
1606 verinfo = mp->md_data;
1607 mod = modlist_lookup2(modname, verinfo);
1609 printf("KLD file %s - cannot find "
1610 "dependency \"%s\"\n",
1611 lf->filename, modname);
1614 /* Don't count self-dependencies */
1615 if (lf == mod->container)
1617 mod->container->refs++;
1618 error = linker_file_add_dependency(lf,
1621 panic("cannot add dependency");
1625 * Now do relocation etc using the symbol search paths
1626 * established by the dependencies
1628 error = LINKER_LINK_PRELOAD_FINISH(lf);
1630 printf("KLD file %s - could not finalize loading\n",
1634 linker_file_register_modules(lf);
1635 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1636 &si_stop, NULL) == 0)
1637 sysinit_add(si_start, si_stop);
1638 linker_file_register_sysctls(lf);
1639 lf->flags |= LINKER_FILE_LINKED;
1642 TAILQ_REMOVE(&depended_files, lf, loaded);
1643 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1645 /* woohoo! we made it! */
1648 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1651 * Search for a not-loaded module by name.
1653 * Modules may be found in the following locations:
1655 * - preloaded (result is just the module name) - on disk (result is full path
1658 * If the module name is qualified in any way (contains path, etc.) the we
1659 * simply return a copy of it.
1661 * The search path can be manipulated via sysctl. Note that we use the ';'
1662 * character as a separator to be consistent with the bootloader.
1665 static char linker_hintfile[] = "linker.hints";
1666 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1668 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1669 sizeof(linker_path), "module load search path");
1671 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1673 static char *linker_ext_list[] = {
1680 * Check if file actually exists either with or without extension listed in
1681 * the linker_ext_list. (probably should be generic for the rest of the
1685 linker_lookup_file(const char *path, int pathlen, const char *name,
1686 int namelen, struct vattr *vap)
1688 struct nameidata nd;
1689 struct thread *td = curthread; /* XXX */
1690 char *result, **cpp, *sep;
1691 int error, len, extlen, reclen, flags, vfslocked;
1695 for (cpp = linker_ext_list; *cpp; cpp++) {
1700 extlen++; /* trailing '\0' */
1701 sep = (path[pathlen - 1] != '/') ? "/" : "";
1703 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1704 result = malloc(reclen, M_LINKER, M_WAITOK);
1705 for (cpp = linker_ext_list; *cpp; cpp++) {
1706 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1707 namelen, name, *cpp);
1709 * Attempt to open the file, and return the path if
1710 * we succeed and it's a regular file.
1712 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
1714 error = vn_open(&nd, &flags, 0, NULL);
1716 vfslocked = NDHASGIANT(&nd);
1717 NDFREE(&nd, NDF_ONLY_PNBUF);
1718 type = nd.ni_vp->v_type;
1720 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1721 VOP_UNLOCK(nd.ni_vp, 0);
1722 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1723 VFS_UNLOCK_GIANT(vfslocked);
1728 free(result, M_LINKER);
1732 #define INT_ALIGN(base, ptr) ptr = \
1733 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1736 * Lookup KLD which contains requested module in the "linker.hints" file. If
1737 * version specification is available, then try to find the best KLD.
1738 * Otherwise just find the latest one.
1741 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1742 int modnamelen, struct mod_depend *verinfo)
1744 struct thread *td = curthread; /* XXX */
1745 struct ucred *cred = td ? td->td_ucred : NULL;
1746 struct nameidata nd;
1747 struct vattr vattr, mattr;
1748 u_char *hints = NULL;
1749 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1750 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1754 bestver = found = 0;
1756 sep = (path[pathlen - 1] != '/') ? "/" : "";
1757 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1759 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1760 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1763 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
1765 error = vn_open(&nd, &flags, 0, NULL);
1768 vfslocked = NDHASGIANT(&nd);
1769 NDFREE(&nd, NDF_ONLY_PNBUF);
1770 if (nd.ni_vp->v_type != VREG)
1773 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1777 * XXX: we need to limit this number to some reasonable value
1779 if (vattr.va_size > 100 * 1024) {
1780 printf("hints file too large %ld\n", (long)vattr.va_size);
1783 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1786 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1787 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1790 VOP_UNLOCK(nd.ni_vp, 0);
1791 vn_close(nd.ni_vp, FREAD, cred, td);
1792 VFS_UNLOCK_GIANT(vfslocked);
1795 printf("can't read %d\n", reclen);
1798 intp = (int *)hints;
1800 if (ival != LINKER_HINTS_VERSION) {
1801 printf("hints file version mismatch %d\n", ival);
1804 bufend = hints + vattr.va_size;
1805 recptr = (u_char *)intp;
1807 while (recptr < bufend && !found) {
1808 intp = (int *)recptr;
1815 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1818 INT_ALIGN(hints, cp);
1822 if (verinfo == NULL ||
1823 ival == verinfo->md_ver_preferred) {
1827 if (ival >= verinfo->md_ver_minimum &&
1828 ival <= verinfo->md_ver_maximum &&
1838 recptr += reclen + sizeof(int);
1841 * Finally check if KLD is in the place
1844 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1846 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1849 * KLD is newer than hints file. What we should do now?
1851 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1852 printf("warning: KLD '%s' is newer than the linker.hints"
1855 free(pathbuf, M_LINKER);
1857 free(hints, M_TEMP);
1858 if (nd.ni_vp != NULL) {
1859 VOP_UNLOCK(nd.ni_vp, 0);
1860 vn_close(nd.ni_vp, FREAD, cred, td);
1861 VFS_UNLOCK_GIANT(vfslocked);
1864 * If nothing found or hints is absent - fallback to the old
1865 * way by using "kldname[.ko]" as module name.
1867 if (!found && !bestver && result == NULL)
1868 result = linker_lookup_file(path, pathlen, modname,
1874 * Lookup KLD which contains requested module in the all directories.
1877 linker_search_module(const char *modname, int modnamelen,
1878 struct mod_depend *verinfo)
1880 char *cp, *ep, *result;
1883 * traverse the linker path
1885 for (cp = linker_path; *cp; cp = ep + 1) {
1886 /* find the end of this component */
1887 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1888 result = linker_hints_lookup(cp, ep - cp, modname,
1889 modnamelen, verinfo);
1899 * Search for module in all directories listed in the linker_path.
1902 linker_search_kld(const char *name)
1904 char *cp, *ep, *result;
1907 /* qualified at all? */
1908 if (index(name, '/'))
1909 return (linker_strdup(name));
1911 /* traverse the linker path */
1913 for (ep = linker_path; *ep; ep++) {
1915 /* find the end of this component */
1916 for (; *ep != 0 && *ep != ';'; ep++);
1917 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1925 linker_basename(const char *path)
1927 const char *filename;
1929 filename = rindex(path, '/');
1930 if (filename == NULL)
1939 * Inform hwpmc about the set of kernel modules currently loaded.
1942 linker_hwpmc_list_objects(void)
1945 struct pmckern_map_in *kobase;
1950 TAILQ_FOREACH(lf, &linker_files, link)
1953 /* Allocate nmappings + 1 entries. */
1954 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1955 M_LINKER, M_WAITOK | M_ZERO);
1957 TAILQ_FOREACH(lf, &linker_files, link) {
1959 /* Save the info for this linker file. */
1960 kobase[i].pm_file = lf->filename;
1961 kobase[i].pm_address = (uintptr_t)lf->address;
1966 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
1968 /* The last entry of the malloced area comprises of all zeros. */
1969 KASSERT(kobase[i].pm_file == NULL,
1970 ("linker_hwpmc_list_objects: last object not NULL"));
1972 return ((void *)kobase);
1977 * Find a file which contains given module and load it, if "parent" is not
1978 * NULL, register a reference to it.
1981 linker_load_module(const char *kldname, const char *modname,
1982 struct linker_file *parent, struct mod_depend *verinfo,
1983 struct linker_file **lfpp)
1985 linker_file_t lfdep;
1986 const char *filename;
1991 if (modname == NULL) {
1993 * We have to load KLD
1995 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1997 pathname = linker_search_kld(kldname);
1999 if (modlist_lookup2(modname, verinfo) != NULL)
2001 if (kldname != NULL)
2002 pathname = linker_strdup(kldname);
2003 else if (rootvnode == NULL)
2007 * Need to find a KLD with required module
2009 pathname = linker_search_module(modname,
2010 strlen(modname), verinfo);
2012 if (pathname == NULL)
2016 * Can't load more than one file with the same basename XXX:
2017 * Actually it should be possible to have multiple KLDs with
2018 * the same basename but different path because they can
2019 * provide different versions of the same modules.
2021 filename = linker_basename(pathname);
2022 if (linker_find_file_by_name(filename))
2025 error = linker_load_file(pathname, &lfdep);
2028 if (modname && verinfo &&
2029 modlist_lookup2(modname, verinfo) == NULL) {
2030 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2035 error = linker_file_add_dependency(parent, lfdep);
2042 free(pathname, M_LINKER);
2047 * This routine is responsible for finding dependencies of userland initiated
2048 * kldload(2)'s of files.
2051 linker_load_dependencies(linker_file_t lf)
2053 linker_file_t lfdep;
2054 struct mod_metadata **start, **stop, **mdp, **nmdp;
2055 struct mod_metadata *mp, *nmp;
2056 struct mod_depend *verinfo;
2058 const char *modname, *nmodname;
2059 int ver, error = 0, count;
2062 * All files are dependant on /kernel.
2065 if (linker_kernel_file) {
2066 linker_kernel_file->refs++;
2067 error = linker_file_add_dependency(lf, linker_kernel_file);
2071 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2074 for (mdp = start; mdp < stop; mdp++) {
2076 if (mp->md_type != MDT_VERSION)
2078 modname = mp->md_cval;
2079 ver = ((struct mod_version *)mp->md_data)->mv_version;
2080 mod = modlist_lookup(modname, ver);
2082 printf("interface %s.%d already present in the KLD"
2083 " '%s'!\n", modname, ver,
2084 mod->container->filename);
2089 for (mdp = start; mdp < stop; mdp++) {
2091 if (mp->md_type != MDT_DEPEND)
2093 modname = mp->md_cval;
2094 verinfo = mp->md_data;
2096 for (nmdp = start; nmdp < stop; nmdp++) {
2098 if (nmp->md_type != MDT_VERSION)
2100 nmodname = nmp->md_cval;
2101 if (strcmp(modname, nmodname) == 0)
2104 if (nmdp < stop)/* early exit, it's a self reference */
2106 mod = modlist_lookup2(modname, verinfo);
2107 if (mod) { /* woohoo, it's loaded already */
2108 lfdep = mod->container;
2110 error = linker_file_add_dependency(lf, lfdep);
2115 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2117 printf("KLD %s: depends on %s - not available or"
2118 " version mismatch\n", lf->filename, modname);
2125 linker_addmodules(lf, start, stop, 0);
2130 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2132 struct sysctl_req *req;
2135 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2139 * Export a nul-separated, double-nul-terminated list of all function names
2143 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2149 error = mac_kld_check_stat(req->td->td_ucred);
2153 error = sysctl_wire_old_buffer(req, 0);
2157 TAILQ_FOREACH(lf, &linker_files, link) {
2158 error = LINKER_EACH_FUNCTION_NAME(lf,
2159 sysctl_kern_function_list_iterate, req);
2166 return (SYSCTL_OUT(req, "", 1));
2169 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD,
2170 NULL, 0, sysctl_kern_function_list, "", "kernel function list");