2 * Copyright (c) 1997-2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include "opt_hwpmc_hooks.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/sysproto.h>
39 #include <sys/sysent.h>
43 #include <sys/mutex.h>
45 #include <sys/module.h>
46 #include <sys/mount.h>
47 #include <sys/linker.h>
48 #include <sys/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;
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);
422 linker_file_register_sysctls(lf);
423 linker_file_sysinit(lf);
425 lf->flags |= LINKER_FILE_LINKED;
431 * Less than ideal, but tells the user whether it failed to load or
432 * the module was not found.
437 * If the file type has not been recognized by the last try
438 * printout a message before to fail.
441 printf("linker_load_file: Unsupported file type\n");
444 * Format not recognized or otherwise unloadable.
445 * When loading a module that is statically built into
446 * the kernel EEXIST percolates back up as the return
447 * value. Preserve this so that apps like sysinstall
448 * can recognize this special case and not post bogus
454 error = ENOENT; /* Nothing found */
459 linker_reference_module(const char *modname, struct mod_depend *verinfo,
460 linker_file_t *result)
466 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
467 *result = mod->container;
473 error = linker_load_module(NULL, modname, NULL, verinfo, result);
479 linker_release_module(const char *modname, struct mod_depend *verinfo,
487 KASSERT(modname != NULL,
488 ("linker_release_module: no file or name"));
489 mod = modlist_lookup2(modname, verinfo);
496 KASSERT(modname == NULL && verinfo == NULL,
497 ("linker_release_module: both file and name"));
498 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
504 linker_find_file_by_name(const char *filename)
509 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
510 sprintf(koname, "%s.ko", filename);
513 TAILQ_FOREACH(lf, &linker_files, link) {
514 if (strcmp(lf->filename, koname) == 0)
516 if (strcmp(lf->filename, filename) == 0)
519 free(koname, M_LINKER);
524 linker_find_file_by_id(int fileid)
529 TAILQ_FOREACH(lf, &linker_files, link)
530 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
536 linker_file_foreach(linker_predicate_t *predicate, void *context)
542 TAILQ_FOREACH(lf, &linker_files, link) {
543 retval = predicate(lf, context);
552 linker_make_file(const char *pathname, linker_class_t lc)
555 const char *filename;
558 filename = linker_basename(pathname);
560 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
561 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
567 lf->filename = linker_strdup(filename);
568 lf->pathname = linker_strdup(pathname);
569 LINKER_GET_NEXT_FILE_ID(lf->id);
572 lf->loadcnt = ++loadcnt;
573 lf->sdt_probes = NULL;
575 STAILQ_INIT(&lf->common);
576 TAILQ_INIT(&lf->modules);
577 TAILQ_INSERT_TAIL(&linker_files, lf, link);
582 linker_file_unload(linker_file_t file, int flags)
585 modlist_t ml, nextml;
586 struct common_symbol *cp;
589 /* Refuse to unload modules if securelevel raised. */
590 if (prison0.pr_securelevel > 0)
594 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
596 /* Easy case of just dropping a reference. */
597 if (file->refs > 1) {
602 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
603 " informing modules\n"));
606 * Quiesce all the modules to give them a chance to veto the unload.
609 for (mod = TAILQ_FIRST(&file->modules); mod;
610 mod = module_getfnext(mod)) {
612 error = module_quiesce(mod);
613 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
614 KLD_DPF(FILE, ("linker_file_unload: module %s"
615 " vetoed unload\n", module_getname(mod)));
617 * XXX: Do we need to tell all the quiesced modules
618 * that they can resume work now via a new module
628 * Inform any modules associated with this file that they are
632 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
633 next = module_getfnext(mod);
637 * Give the module a chance to veto the unload.
639 if ((error = module_unload(mod)) != 0) {
640 KLD_DPF(FILE, ("linker_file_unload: module %s"
641 " failed unload\n", module_getname(mod)));
649 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
650 if (ml->container == file) {
651 TAILQ_REMOVE(&found_modules, ml, link);
657 * Don't try to run SYSUNINITs if we are unloaded due to a
660 if (file->flags & LINKER_FILE_LINKED) {
661 file->flags &= ~LINKER_FILE_LINKED;
663 linker_file_sysuninit(file);
664 linker_file_unregister_sysctls(file);
667 TAILQ_REMOVE(&linker_files, file, link);
670 for (i = 0; i < file->ndeps; i++)
671 linker_file_unload(file->deps[i], flags);
672 free(file->deps, M_LINKER);
675 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
676 STAILQ_REMOVE_HEAD(&file->common, link);
681 if (file->filename) {
682 free(file->filename, M_LINKER);
683 file->filename = NULL;
685 if (file->pathname) {
686 free(file->pathname, M_LINKER);
687 file->pathname = NULL;
689 kobj_delete((kobj_t) file, M_LINKER);
694 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
696 return (LINKER_CTF_GET(file, lc));
700 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
702 linker_file_t *newdeps;
705 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
706 M_LINKER, M_WAITOK | M_ZERO);
711 bcopy(file->deps, newdeps,
712 file->ndeps * sizeof(linker_file_t *));
713 free(file->deps, M_LINKER);
715 file->deps = newdeps;
716 file->deps[file->ndeps] = dep;
718 KLD_DPF(FILE, ("linker_file_add_dependency:"
719 " adding %s as dependency for %s\n",
720 dep->filename, file->filename));
725 * Locate a linker set and its contents. This is a helper function to avoid
726 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
727 * This function is used in this file so we can avoid having lots of (void **)
731 linker_file_lookup_set(linker_file_t file, const char *name,
732 void *firstp, void *lastp, int *countp)
736 locked = KLD_LOCKED();
739 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
746 * List all functions in a file.
749 linker_file_function_listall(linker_file_t lf,
750 linker_function_nameval_callback_t callback_func, void *arg)
752 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
756 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
761 locked = KLD_LOCKED();
764 sym = linker_file_lookup_symbol_internal(file, name, deps);
771 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
775 linker_symval_t symval;
777 size_t common_size = 0;
781 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
784 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
785 LINKER_SYMBOL_VALUES(file, sym, &symval);
786 if (symval.value == 0)
788 * For commons, first look them up in the
789 * dependencies and only allocate space if not found
792 common_size = symval.size;
794 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
795 ".value=%p\n", symval.value));
796 return (symval.value);
800 for (i = 0; i < file->ndeps; i++) {
801 address = linker_file_lookup_symbol_internal(
802 file->deps[i], name, 0);
804 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
805 " deps value=%p\n", address));
810 if (common_size > 0) {
812 * This is a common symbol which was not found in the
813 * dependencies. We maintain a simple common symbol table in
816 struct common_symbol *cp;
818 STAILQ_FOREACH(cp, &file->common, link) {
819 if (strcmp(cp->name, name) == 0) {
820 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
821 " old common value=%p\n", cp->address));
822 return (cp->address);
826 * Round the symbol size up to align.
828 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
829 cp = malloc(sizeof(struct common_symbol)
830 + common_size + strlen(name) + 1, M_LINKER,
832 cp->address = (caddr_t)(cp + 1);
833 cp->name = cp->address + common_size;
834 strcpy(cp->name, name);
835 bzero(cp->address, common_size);
836 STAILQ_INSERT_TAIL(&file->common, cp, link);
838 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
839 " value=%p\n", cp->address));
840 return (cp->address);
842 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
847 * Both DDB and stack(9) rely on the kernel linker to provide forward and
848 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
849 * do this in a lockfree manner. We provide a set of internal helper
850 * routines to perform these operations without locks, and then wrappers that
853 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
857 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
861 TAILQ_FOREACH(lf, &linker_files, link) {
862 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
870 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
873 c_linker_sym_t best, es;
874 u_long diff, bestdiff, off;
877 off = (uintptr_t)value;
879 TAILQ_FOREACH(lf, &linker_files, link) {
880 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
882 if (es != 0 && diff < bestdiff) {
901 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
905 TAILQ_FOREACH(lf, &linker_files, link) {
906 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
913 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
916 linker_symval_t symval;
921 error = linker_debug_search_symbol(value, &sym, offset);
924 error = linker_debug_symbol_values(sym, &symval);
927 strlcpy(buf, symval.name, buflen);
932 * DDB Helpers. DDB has to look across multiple files with their own symbol
933 * tables and string tables.
935 * Note that we do not obey list locking protocols here. We really don't need
936 * DDB to hang because somebody's got the lock held. We'll take the chance
937 * that the files list is inconsistant instead.
941 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
944 return (linker_debug_lookup(symstr, sym));
949 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
952 return (linker_debug_search_symbol(value, sym, diffp));
956 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
959 return (linker_debug_symbol_values(sym, symval));
963 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
967 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
971 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
972 * obey locking protocols, and offer a significantly less complex interface.
975 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
981 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
990 kern_kldload(struct thread *td, const char *file, int *fileid)
993 struct pmckern_map_in pkm;
995 const char *kldname, *modname;
999 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1002 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1006 * It is possible that kldloaded module will attach a new ifnet,
1007 * so vnet context must be set when this ocurs.
1009 CURVNET_SET(TD_TO_VNET(td));
1012 * If file does not contain a qualified name or any dot in it
1013 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1016 if (strchr(file, '/') || strchr(file, '.')) {
1025 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1035 pkm.pm_file = lf->filename;
1036 pkm.pm_address = (uintptr_t) lf->address;
1037 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
1049 sys_kldload(struct thread *td, struct kldload_args *uap)
1051 char *pathname = NULL;
1054 td->td_retval[0] = -1;
1056 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1057 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1059 error = kern_kldload(td, pathname, &fileid);
1061 td->td_retval[0] = fileid;
1063 free(pathname, M_TEMP);
1068 kern_kldunload(struct thread *td, int fileid, int flags)
1071 struct pmckern_map_out pkm;
1076 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1079 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1082 CURVNET_SET(TD_TO_VNET(td));
1084 lf = linker_find_file_by_id(fileid);
1086 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1088 /* Check if there are DTrace probes enabled on this file. */
1089 if (lf->nenabled > 0) {
1090 printf("kldunload: attempt to unload file that has"
1091 " DTrace probes enabled\n");
1093 } else if (lf->userrefs == 0) {
1095 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1097 printf("kldunload: attempt to unload file that was"
1098 " loaded by the kernel\n");
1102 /* Save data needed by hwpmc(4) before unloading. */
1103 pkm.pm_address = (uintptr_t) lf->address;
1104 pkm.pm_size = lf->size;
1107 error = linker_file_unload(lf, flags);
1117 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
1129 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1132 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1136 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1139 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1140 uap->flags != LINKER_UNLOAD_FORCE)
1142 return (kern_kldunload(td, uap->fileid, uap->flags));
1146 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1149 const char *filename;
1154 error = mac_kld_check_stat(td->td_ucred);
1159 td->td_retval[0] = -1;
1161 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1162 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1165 filename = linker_basename(pathname);
1167 lf = linker_find_file_by_name(filename);
1169 td->td_retval[0] = lf->id;
1174 free(pathname, M_TEMP);
1179 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1185 error = mac_kld_check_stat(td->td_ucred);
1191 if (uap->fileid == 0)
1192 lf = TAILQ_FIRST(&linker_files);
1194 lf = linker_find_file_by_id(uap->fileid);
1199 lf = TAILQ_NEXT(lf, link);
1202 /* Skip partially loaded files. */
1203 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1204 lf = TAILQ_NEXT(lf, link);
1207 td->td_retval[0] = lf->id;
1209 td->td_retval[0] = 0;
1216 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1218 struct kld_file_stat stat;
1222 * Check the version of the user's structure.
1224 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1227 if (version != sizeof(struct kld_file_stat_1) &&
1228 version != sizeof(struct kld_file_stat))
1231 error = kern_kldstat(td, uap->fileid, &stat);
1234 return (copyout(&stat, uap->stat, version));
1238 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1245 error = mac_kld_check_stat(td->td_ucred);
1251 lf = linker_find_file_by_id(fileid);
1257 /* Version 1 fields: */
1258 namelen = strlen(lf->filename) + 1;
1259 if (namelen > MAXPATHLEN)
1260 namelen = MAXPATHLEN;
1261 bcopy(lf->filename, &stat->name[0], namelen);
1262 stat->refs = lf->refs;
1264 stat->address = lf->address;
1265 stat->size = lf->size;
1266 /* Version 2 fields: */
1267 namelen = strlen(lf->pathname) + 1;
1268 if (namelen > MAXPATHLEN)
1269 namelen = MAXPATHLEN;
1270 bcopy(lf->pathname, &stat->pathname[0], namelen);
1273 td->td_retval[0] = 0;
1278 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1285 error = mac_kld_check_stat(td->td_ucred);
1291 lf = linker_find_file_by_id(uap->fileid);
1294 mp = TAILQ_FIRST(&lf->modules);
1296 td->td_retval[0] = module_getid(mp);
1298 td->td_retval[0] = 0;
1307 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1309 char *symstr = NULL;
1311 linker_symval_t symval;
1313 struct kld_sym_lookup lookup;
1317 error = mac_kld_check_stat(td->td_ucred);
1322 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1324 if (lookup.version != sizeof(lookup) ||
1325 uap->cmd != KLDSYM_LOOKUP)
1327 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1328 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1331 if (uap->fileid != 0) {
1332 lf = linker_find_file_by_id(uap->fileid);
1335 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1336 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1337 lookup.symvalue = (uintptr_t) symval.value;
1338 lookup.symsize = symval.size;
1339 error = copyout(&lookup, uap->data, sizeof(lookup));
1343 TAILQ_FOREACH(lf, &linker_files, link) {
1344 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,
1358 free(symstr, M_TEMP);
1363 * Preloaded module support
1367 modlist_lookup(const char *name, int ver)
1371 TAILQ_FOREACH(mod, &found_modules, link) {
1372 if (strcmp(mod->name, name) == 0 &&
1373 (ver == 0 || mod->version == ver))
1380 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1382 modlist_t mod, bestmod;
1385 if (verinfo == NULL)
1386 return (modlist_lookup(name, 0));
1388 TAILQ_FOREACH(mod, &found_modules, link) {
1389 if (strcmp(mod->name, name) != 0)
1392 if (ver == verinfo->md_ver_preferred)
1394 if (ver >= verinfo->md_ver_minimum &&
1395 ver <= verinfo->md_ver_maximum &&
1396 (bestmod == NULL || ver > bestmod->version))
1403 modlist_newmodule(const char *modname, int version, linker_file_t container)
1407 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1409 panic("no memory for module list");
1410 mod->container = container;
1411 mod->name = modname;
1412 mod->version = version;
1413 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1418 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1419 struct mod_metadata **stop, int preload)
1421 struct mod_metadata *mp, **mdp;
1422 const char *modname;
1425 for (mdp = start; mdp < stop; mdp++) {
1427 if (mp->md_type != MDT_VERSION)
1429 modname = mp->md_cval;
1430 ver = ((struct mod_version *)mp->md_data)->mv_version;
1431 if (modlist_lookup(modname, ver) != NULL) {
1432 printf("module %s already present!\n", modname);
1433 /* XXX what can we do? this is a build error. :-( */
1436 modlist_newmodule(modname, ver, lf);
1441 linker_preload(void *arg)
1444 const char *modname, *nmodname;
1446 linker_file_t lf, nlf;
1449 linker_file_list_t loaded_files;
1450 linker_file_list_t depended_files;
1451 struct mod_metadata *mp, *nmp;
1452 struct mod_metadata **start, **stop, **mdp, **nmdp;
1453 struct mod_depend *verinfo;
1457 struct sysinit **si_start, **si_stop;
1459 TAILQ_INIT(&loaded_files);
1460 TAILQ_INIT(&depended_files);
1461 TAILQ_INIT(&found_modules);
1465 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1466 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1467 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1468 if (modname == NULL) {
1469 printf("Preloaded module at %p does not have a"
1470 " name!\n", modptr);
1473 if (modtype == NULL) {
1474 printf("Preloaded module at %p does not have a type!\n",
1479 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1482 TAILQ_FOREACH(lc, &classes, link) {
1483 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1489 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1493 * First get a list of stuff in the kernel.
1495 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1497 linker_addmodules(linker_kernel_file, start, stop, 1);
1500 * This is a once-off kinky bubble sort to resolve relocation
1501 * dependency requirements.
1504 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1505 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1508 * First, look to see if we would successfully link with this
1511 resolves = 1; /* unless we know otherwise */
1513 for (mdp = start; mdp < stop; mdp++) {
1515 if (mp->md_type != MDT_DEPEND)
1517 modname = mp->md_cval;
1518 verinfo = mp->md_data;
1519 for (nmdp = start; nmdp < stop; nmdp++) {
1521 if (nmp->md_type != MDT_VERSION)
1523 nmodname = nmp->md_cval;
1524 if (strcmp(modname, nmodname) == 0)
1527 if (nmdp < stop) /* it's a self reference */
1531 * ok, the module isn't here yet, we
1534 if (modlist_lookup2(modname, verinfo) == NULL)
1539 * OK, if we found our modules, we can link. So, "provide"
1540 * the modules inside and add it to the end of the link order
1545 for (mdp = start; mdp < stop; mdp++) {
1547 if (mp->md_type != MDT_VERSION)
1549 modname = mp->md_cval;
1550 nver = ((struct mod_version *)
1551 mp->md_data)->mv_version;
1552 if (modlist_lookup(modname,
1554 printf("module %s already"
1555 " present!\n", modname);
1556 TAILQ_REMOVE(&loaded_files,
1558 linker_file_unload(lf,
1559 LINKER_UNLOAD_FORCE);
1560 /* we changed tailq next ptr */
1563 modlist_newmodule(modname, nver, lf);
1566 TAILQ_REMOVE(&loaded_files, lf, loaded);
1567 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1569 * Since we provided modules, we need to restart the
1570 * sort so that the previous files that depend on us
1571 * have a chance. Also, we've busted the tailq next
1572 * pointer with the REMOVE.
1579 * At this point, we check to see what could not be resolved..
1581 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1582 TAILQ_REMOVE(&loaded_files, lf, loaded);
1583 printf("KLD file %s is missing dependencies\n", lf->filename);
1584 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1588 * We made it. Finish off the linking in the order we determined.
1590 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1591 if (linker_kernel_file) {
1592 linker_kernel_file->refs++;
1593 error = linker_file_add_dependency(lf,
1594 linker_kernel_file);
1596 panic("cannot add dependency");
1598 lf->userrefs++; /* so we can (try to) kldunload it */
1599 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1602 for (mdp = start; mdp < stop; mdp++) {
1604 if (mp->md_type != MDT_DEPEND)
1606 modname = mp->md_cval;
1607 verinfo = mp->md_data;
1608 mod = modlist_lookup2(modname, verinfo);
1610 printf("KLD file %s - cannot find "
1611 "dependency \"%s\"\n",
1612 lf->filename, modname);
1615 /* Don't count self-dependencies */
1616 if (lf == mod->container)
1618 mod->container->refs++;
1619 error = linker_file_add_dependency(lf,
1622 panic("cannot add dependency");
1626 * Now do relocation etc using the symbol search paths
1627 * established by the dependencies
1629 error = LINKER_LINK_PRELOAD_FINISH(lf);
1631 printf("KLD file %s - could not finalize loading\n",
1635 linker_file_register_modules(lf);
1636 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1637 &si_stop, NULL) == 0)
1638 sysinit_add(si_start, si_stop);
1639 linker_file_register_sysctls(lf);
1640 lf->flags |= LINKER_FILE_LINKED;
1643 TAILQ_REMOVE(&depended_files, lf, loaded);
1644 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1646 /* woohoo! we made it! */
1649 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1652 * Search for a not-loaded module by name.
1654 * Modules may be found in the following locations:
1656 * - preloaded (result is just the module name) - on disk (result is full path
1659 * If the module name is qualified in any way (contains path, etc.) the we
1660 * simply return a copy of it.
1662 * The search path can be manipulated via sysctl. Note that we use the ';'
1663 * character as a separator to be consistent with the bootloader.
1666 static char linker_hintfile[] = "linker.hints";
1667 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1669 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1670 sizeof(linker_path), "module load search path");
1672 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1674 static char *linker_ext_list[] = {
1681 * Check if file actually exists either with or without extension listed in
1682 * the linker_ext_list. (probably should be generic for the rest of the
1686 linker_lookup_file(const char *path, int pathlen, const char *name,
1687 int namelen, struct vattr *vap)
1689 struct nameidata nd;
1690 struct thread *td = curthread; /* XXX */
1691 char *result, **cpp, *sep;
1692 int error, len, extlen, reclen, flags, vfslocked;
1696 for (cpp = linker_ext_list; *cpp; cpp++) {
1701 extlen++; /* trailing '\0' */
1702 sep = (path[pathlen - 1] != '/') ? "/" : "";
1704 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1705 result = malloc(reclen, M_LINKER, M_WAITOK);
1706 for (cpp = linker_ext_list; *cpp; cpp++) {
1707 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1708 namelen, name, *cpp);
1710 * Attempt to open the file, and return the path if
1711 * we succeed and it's a regular file.
1713 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
1715 error = vn_open(&nd, &flags, 0, NULL);
1717 vfslocked = NDHASGIANT(&nd);
1718 NDFREE(&nd, NDF_ONLY_PNBUF);
1719 type = nd.ni_vp->v_type;
1721 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1722 VOP_UNLOCK(nd.ni_vp, 0);
1723 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1724 VFS_UNLOCK_GIANT(vfslocked);
1729 free(result, M_LINKER);
1733 #define INT_ALIGN(base, ptr) ptr = \
1734 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1737 * Lookup KLD which contains requested module in the "linker.hints" file. If
1738 * version specification is available, then try to find the best KLD.
1739 * Otherwise just find the latest one.
1742 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1743 int modnamelen, struct mod_depend *verinfo)
1745 struct thread *td = curthread; /* XXX */
1746 struct ucred *cred = td ? td->td_ucred : NULL;
1747 struct nameidata nd;
1748 struct vattr vattr, mattr;
1749 u_char *hints = NULL;
1750 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1751 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1755 bestver = found = 0;
1757 sep = (path[pathlen - 1] != '/') ? "/" : "";
1758 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1760 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1761 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1764 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
1766 error = vn_open(&nd, &flags, 0, NULL);
1769 vfslocked = NDHASGIANT(&nd);
1770 NDFREE(&nd, NDF_ONLY_PNBUF);
1771 if (nd.ni_vp->v_type != VREG)
1774 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1778 * XXX: we need to limit this number to some reasonable value
1780 if (vattr.va_size > 100 * 1024) {
1781 printf("hints file too large %ld\n", (long)vattr.va_size);
1784 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1787 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1788 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1791 VOP_UNLOCK(nd.ni_vp, 0);
1792 vn_close(nd.ni_vp, FREAD, cred, td);
1793 VFS_UNLOCK_GIANT(vfslocked);
1796 printf("can't read %d\n", reclen);
1799 intp = (int *)hints;
1801 if (ival != LINKER_HINTS_VERSION) {
1802 printf("hints file version mismatch %d\n", ival);
1805 bufend = hints + vattr.va_size;
1806 recptr = (u_char *)intp;
1808 while (recptr < bufend && !found) {
1809 intp = (int *)recptr;
1816 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1819 INT_ALIGN(hints, cp);
1823 if (verinfo == NULL ||
1824 ival == verinfo->md_ver_preferred) {
1828 if (ival >= verinfo->md_ver_minimum &&
1829 ival <= verinfo->md_ver_maximum &&
1839 recptr += reclen + sizeof(int);
1842 * Finally check if KLD is in the place
1845 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1847 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1850 * KLD is newer than hints file. What we should do now?
1852 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1853 printf("warning: KLD '%s' is newer than the linker.hints"
1856 free(pathbuf, M_LINKER);
1858 free(hints, M_TEMP);
1859 if (nd.ni_vp != NULL) {
1860 VOP_UNLOCK(nd.ni_vp, 0);
1861 vn_close(nd.ni_vp, FREAD, cred, td);
1862 VFS_UNLOCK_GIANT(vfslocked);
1865 * If nothing found or hints is absent - fallback to the old
1866 * way by using "kldname[.ko]" as module name.
1868 if (!found && !bestver && result == NULL)
1869 result = linker_lookup_file(path, pathlen, modname,
1875 * Lookup KLD which contains requested module in the all directories.
1878 linker_search_module(const char *modname, int modnamelen,
1879 struct mod_depend *verinfo)
1881 char *cp, *ep, *result;
1884 * traverse the linker path
1886 for (cp = linker_path; *cp; cp = ep + 1) {
1887 /* find the end of this component */
1888 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1889 result = linker_hints_lookup(cp, ep - cp, modname,
1890 modnamelen, verinfo);
1900 * Search for module in all directories listed in the linker_path.
1903 linker_search_kld(const char *name)
1905 char *cp, *ep, *result;
1908 /* qualified at all? */
1909 if (strchr(name, '/'))
1910 return (linker_strdup(name));
1912 /* traverse the linker path */
1914 for (ep = linker_path; *ep; ep++) {
1916 /* find the end of this component */
1917 for (; *ep != 0 && *ep != ';'; ep++);
1918 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1926 linker_basename(const char *path)
1928 const char *filename;
1930 filename = strrchr(path, '/');
1931 if (filename == NULL)
1940 * Inform hwpmc about the set of kernel modules currently loaded.
1943 linker_hwpmc_list_objects(void)
1946 struct pmckern_map_in *kobase;
1951 TAILQ_FOREACH(lf, &linker_files, link)
1954 /* Allocate nmappings + 1 entries. */
1955 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1956 M_LINKER, M_WAITOK | M_ZERO);
1958 TAILQ_FOREACH(lf, &linker_files, link) {
1960 /* Save the info for this linker file. */
1961 kobase[i].pm_file = lf->filename;
1962 kobase[i].pm_address = (uintptr_t)lf->address;
1967 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
1969 /* The last entry of the malloced area comprises of all zeros. */
1970 KASSERT(kobase[i].pm_file == NULL,
1971 ("linker_hwpmc_list_objects: last object not NULL"));
1973 return ((void *)kobase);
1978 * Find a file which contains given module and load it, if "parent" is not
1979 * NULL, register a reference to it.
1982 linker_load_module(const char *kldname, const char *modname,
1983 struct linker_file *parent, struct mod_depend *verinfo,
1984 struct linker_file **lfpp)
1986 linker_file_t lfdep;
1987 const char *filename;
1992 if (modname == NULL) {
1994 * We have to load KLD
1996 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1998 pathname = linker_search_kld(kldname);
2000 if (modlist_lookup2(modname, verinfo) != NULL)
2002 if (kldname != NULL)
2003 pathname = linker_strdup(kldname);
2004 else if (rootvnode == NULL)
2008 * Need to find a KLD with required module
2010 pathname = linker_search_module(modname,
2011 strlen(modname), verinfo);
2013 if (pathname == NULL)
2017 * Can't load more than one file with the same basename XXX:
2018 * Actually it should be possible to have multiple KLDs with
2019 * the same basename but different path because they can
2020 * provide different versions of the same modules.
2022 filename = linker_basename(pathname);
2023 if (linker_find_file_by_name(filename))
2026 error = linker_load_file(pathname, &lfdep);
2029 if (modname && verinfo &&
2030 modlist_lookup2(modname, verinfo) == NULL) {
2031 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2036 error = linker_file_add_dependency(parent, lfdep);
2043 free(pathname, M_LINKER);
2048 * This routine is responsible for finding dependencies of userland initiated
2049 * kldload(2)'s of files.
2052 linker_load_dependencies(linker_file_t lf)
2054 linker_file_t lfdep;
2055 struct mod_metadata **start, **stop, **mdp, **nmdp;
2056 struct mod_metadata *mp, *nmp;
2057 struct mod_depend *verinfo;
2059 const char *modname, *nmodname;
2060 int ver, error = 0, count;
2063 * All files are dependant on /kernel.
2066 if (linker_kernel_file) {
2067 linker_kernel_file->refs++;
2068 error = linker_file_add_dependency(lf, linker_kernel_file);
2072 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2075 for (mdp = start; mdp < stop; mdp++) {
2077 if (mp->md_type != MDT_VERSION)
2079 modname = mp->md_cval;
2080 ver = ((struct mod_version *)mp->md_data)->mv_version;
2081 mod = modlist_lookup(modname, ver);
2083 printf("interface %s.%d already present in the KLD"
2084 " '%s'!\n", modname, ver,
2085 mod->container->filename);
2090 for (mdp = start; mdp < stop; mdp++) {
2092 if (mp->md_type != MDT_DEPEND)
2094 modname = mp->md_cval;
2095 verinfo = mp->md_data;
2097 for (nmdp = start; nmdp < stop; nmdp++) {
2099 if (nmp->md_type != MDT_VERSION)
2101 nmodname = nmp->md_cval;
2102 if (strcmp(modname, nmodname) == 0)
2105 if (nmdp < stop)/* early exit, it's a self reference */
2107 mod = modlist_lookup2(modname, verinfo);
2108 if (mod) { /* woohoo, it's loaded already */
2109 lfdep = mod->container;
2111 error = linker_file_add_dependency(lf, lfdep);
2116 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2118 printf("KLD %s: depends on %s - not available or"
2119 " version mismatch\n", lf->filename, modname);
2126 linker_addmodules(lf, start, stop, 0);
2131 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2133 struct sysctl_req *req;
2136 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2140 * Export a nul-separated, double-nul-terminated list of all function names
2144 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2150 error = mac_kld_check_stat(req->td->td_ucred);
2154 error = sysctl_wire_old_buffer(req, 0);
2158 TAILQ_FOREACH(lf, &linker_files, link) {
2159 error = LINKER_EACH_FUNCTION_NAME(lf,
2160 sysctl_kern_function_list_iterate, req);
2167 return (SYSCTL_OUT(req, "", 1));
2170 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD,
2171 NULL, 0, sysctl_kern_function_list, "", "kernel function list");