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"
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>
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>
54 #include <sys/vimage.h>
56 #include <security/mac/mac_framework.h>
58 #include "linker_if.h"
61 #include <sys/pmckern.h>
68 #define KLD_LOCK() sx_xlock(&kld_sx)
69 #define KLD_UNLOCK() sx_xunlock(&kld_sx)
70 #define KLD_LOCKED() sx_xlocked(&kld_sx)
71 #define KLD_LOCK_ASSERT() do { \
73 sx_assert(&kld_sx, SX_XLOCKED); \
77 * static char *linker_search_path(const char *name, struct mod_depend
80 static const char *linker_basename(const char *path);
83 * Find a currently loaded file given its filename.
85 static linker_file_t linker_find_file_by_name(const char* _filename);
88 * Find a currently loaded file given its file id.
90 static linker_file_t linker_find_file_by_id(int _fileid);
92 /* Metadata from the static kernel */
93 SET_DECLARE(modmetadata_set, struct mod_metadata);
95 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
97 linker_file_t linker_kernel_file;
99 static struct sx kld_sx; /* kernel linker lock */
102 * Load counter used by clients to determine if a linker file has been
103 * re-loaded. This counter is incremented for each file load.
107 static linker_class_list_t classes;
108 static linker_file_list_t linker_files;
109 static int next_file_id = 1;
110 static int linker_no_more_classes = 0;
112 #define LINKER_GET_NEXT_FILE_ID(a) do { \
113 linker_file_t lftmp; \
117 TAILQ_FOREACH(lftmp, &linker_files, link) { \
118 if (next_file_id == lftmp->id) { \
123 (a) = next_file_id; \
127 /* XXX wrong name; we're looking at version provision tags here, not modules */
128 typedef TAILQ_HEAD(, modlist) modlisthead_t;
130 TAILQ_ENTRY(modlist) link; /* chain together all modules */
131 linker_file_t container;
135 typedef struct modlist *modlist_t;
136 static modlisthead_t found_modules;
138 static int linker_file_add_dependency(linker_file_t file,
140 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
141 const char* name, int deps);
142 static int linker_load_module(const char *kldname,
143 const char *modname, struct linker_file *parent,
144 struct mod_depend *verinfo, struct linker_file **lfpp);
145 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
148 linker_strdup(const char *str)
152 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
158 linker_init(void *arg)
161 sx_init(&kld_sx, "kernel linker");
162 TAILQ_INIT(&classes);
163 TAILQ_INIT(&linker_files);
166 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
169 linker_stop_class_add(void *arg)
172 linker_no_more_classes = 1;
175 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
178 linker_add_class(linker_class_t lc)
182 * We disallow any class registration past SI_ORDER_ANY
183 * of SI_SUB_KLD. We bump the reference count to keep the
184 * ops from being freed.
186 if (linker_no_more_classes == 1)
188 kobj_class_compile((kobj_class_t) lc);
189 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
190 TAILQ_INSERT_TAIL(&classes, lc, link);
195 linker_file_sysinit(linker_file_t lf)
197 struct sysinit **start, **stop, **sipp, **xipp, *save;
199 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
202 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
205 * Perform a bubble sort of the system initialization objects by
206 * their subsystem (primary key) and order (secondary key).
208 * Since some things care about execution order, this is the operation
209 * which ensures continued function.
211 for (sipp = start; sipp < stop; sipp++) {
212 for (xipp = sipp + 1; xipp < stop; xipp++) {
213 if ((*sipp)->subsystem < (*xipp)->subsystem ||
214 ((*sipp)->subsystem == (*xipp)->subsystem &&
215 (*sipp)->order <= (*xipp)->order))
224 * Traverse the (now) ordered list of system initialization tasks.
225 * Perform each task, and continue on to the next task.
228 for (sipp = start; sipp < stop; sipp++) {
229 if ((*sipp)->subsystem == SI_SUB_DUMMY)
230 continue; /* skip dummy task(s) */
233 (*((*sipp)->func)) ((*sipp)->udata);
239 linker_file_sysuninit(linker_file_t lf)
241 struct sysinit **start, **stop, **sipp, **xipp, *save;
243 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
246 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
251 * Perform a reverse bubble sort of the system initialization objects
252 * by their subsystem (primary key) and order (secondary key).
254 * Since some things care about execution order, this is the operation
255 * which ensures continued function.
257 for (sipp = start; sipp < stop; sipp++) {
258 for (xipp = sipp + 1; xipp < stop; xipp++) {
259 if ((*sipp)->subsystem > (*xipp)->subsystem ||
260 ((*sipp)->subsystem == (*xipp)->subsystem &&
261 (*sipp)->order >= (*xipp)->order))
270 * Traverse the (now) ordered list of system initialization tasks.
271 * Perform each task, and continue on to the next task.
274 for (sipp = start; sipp < stop; sipp++) {
275 if ((*sipp)->subsystem == SI_SUB_DUMMY)
276 continue; /* skip dummy task(s) */
279 (*((*sipp)->func)) ((*sipp)->udata);
285 linker_file_register_sysctls(linker_file_t lf)
287 struct sysctl_oid **start, **stop, **oidp;
290 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
293 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
297 for (oidp = start; oidp < stop; oidp++)
298 sysctl_register_oid(*oidp);
303 linker_file_unregister_sysctls(linker_file_t lf)
305 struct sysctl_oid **start, **stop, **oidp;
307 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
308 " for %s\n", lf->filename));
310 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
314 for (oidp = start; oidp < stop; oidp++)
315 sysctl_unregister_oid(*oidp);
320 linker_file_register_modules(linker_file_t lf)
322 struct mod_metadata **start, **stop, **mdp;
323 const moduledata_t *moddata;
324 int first_error, error;
326 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
327 " in %s\n", lf->filename));
329 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
332 * This fallback should be unnecessary, but if we get booted
333 * from boot2 instead of loader and we are missing our
334 * metadata then we have to try the best we can.
336 if (lf == linker_kernel_file) {
337 start = SET_BEGIN(modmetadata_set);
338 stop = SET_LIMIT(modmetadata_set);
343 for (mdp = start; mdp < stop; mdp++) {
344 if ((*mdp)->md_type != MDT_MODULE)
346 moddata = (*mdp)->md_data;
347 KLD_DPF(FILE, ("Registering module %s in %s\n",
348 moddata->name, lf->filename));
349 error = module_register(moddata, lf);
351 printf("Module %s failed to register: %d\n",
352 moddata->name, error);
353 if (first_error == 0)
357 return (first_error);
361 linker_init_kernel_modules(void)
364 linker_file_register_modules(linker_kernel_file);
367 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
371 linker_load_file(const char *filename, linker_file_t *result)
375 int foundfile, error;
377 /* Refuse to load modules if securelevel raised */
382 lf = linker_find_file_by_name(filename);
384 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
385 " incrementing refs\n", filename));
394 * We do not need to protect (lock) classes here because there is
395 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
396 * and there is no class deregistration mechanism at this time.
398 TAILQ_FOREACH(lc, &classes, link) {
399 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
401 error = LINKER_LOAD_FILE(lc, filename, &lf);
403 * If we got something other than ENOENT, then it exists but
404 * we cannot load it for some other reason.
409 error = linker_file_register_modules(lf);
410 if (error == EEXIST) {
411 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
415 linker_file_register_sysctls(lf);
416 linker_file_sysinit(lf);
418 lf->flags |= LINKER_FILE_LINKED;
424 * Less than ideal, but tells the user whether it failed to load or
425 * the module was not found.
430 * If the file type has not been recognized by the last try
431 * printout a message before to fail.
434 printf("linker_load_file: Unsupported file type\n");
437 * Format not recognized or otherwise unloadable.
438 * When loading a module that is statically built into
439 * the kernel EEXIST percolates back up as the return
440 * value. Preserve this so that apps like sysinstall
441 * can recognize this special case and not post bogus
447 error = ENOENT; /* Nothing found */
452 linker_reference_module(const char *modname, struct mod_depend *verinfo,
453 linker_file_t *result)
459 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
460 *result = mod->container;
466 error = linker_load_module(NULL, modname, NULL, verinfo, result);
472 linker_release_module(const char *modname, struct mod_depend *verinfo,
480 KASSERT(modname != NULL,
481 ("linker_release_module: no file or name"));
482 mod = modlist_lookup2(modname, verinfo);
489 KASSERT(modname == NULL && verinfo == NULL,
490 ("linker_release_module: both file and name"));
491 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
497 linker_find_file_by_name(const char *filename)
502 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
503 sprintf(koname, "%s.ko", filename);
506 TAILQ_FOREACH(lf, &linker_files, link) {
507 if (strcmp(lf->filename, koname) == 0)
509 if (strcmp(lf->filename, filename) == 0)
512 free(koname, M_LINKER);
517 linker_find_file_by_id(int fileid)
522 TAILQ_FOREACH(lf, &linker_files, link)
523 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
529 linker_file_foreach(linker_predicate_t *predicate, void *context)
535 TAILQ_FOREACH(lf, &linker_files, link) {
536 retval = predicate(lf, context);
545 linker_make_file(const char *pathname, linker_class_t lc)
548 const char *filename;
551 filename = linker_basename(pathname);
553 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
554 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
560 lf->filename = linker_strdup(filename);
561 lf->pathname = linker_strdup(pathname);
562 LINKER_GET_NEXT_FILE_ID(lf->id);
565 lf->loadcnt = ++loadcnt;
566 lf->sdt_probes = NULL;
568 STAILQ_INIT(&lf->common);
569 TAILQ_INIT(&lf->modules);
570 TAILQ_INSERT_TAIL(&linker_files, lf, link);
575 linker_file_unload(linker_file_t file, int flags)
578 modlist_t ml, nextml;
579 struct common_symbol *cp;
582 /* Refuse to unload modules if securelevel raised. */
587 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
589 /* Easy case of just dropping a reference. */
590 if (file->refs > 1) {
595 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
596 " informing modules\n"));
599 * Quiesce all the modules to give them a chance to veto the unload.
602 for (mod = TAILQ_FIRST(&file->modules); mod;
603 mod = module_getfnext(mod)) {
605 error = module_quiesce(mod);
606 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
607 KLD_DPF(FILE, ("linker_file_unload: module %s"
608 " vetoed unload\n", module_getname(mod)));
610 * XXX: Do we need to tell all the quiesced modules
611 * that they can resume work now via a new module
621 * Inform any modules associated with this file that they are
625 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
626 next = module_getfnext(mod);
630 * Give the module a chance to veto the unload.
632 if ((error = module_unload(mod)) != 0) {
633 KLD_DPF(FILE, ("linker_file_unload: module %s"
634 " failed unload\n", mod));
642 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
643 if (ml->container == file) {
644 TAILQ_REMOVE(&found_modules, ml, link);
650 * Don't try to run SYSUNINITs if we are unloaded due to a
653 if (file->flags & LINKER_FILE_LINKED) {
654 file->flags &= ~LINKER_FILE_LINKED;
656 linker_file_sysuninit(file);
657 linker_file_unregister_sysctls(file);
660 TAILQ_REMOVE(&linker_files, file, link);
663 for (i = 0; i < file->ndeps; i++)
664 linker_file_unload(file->deps[i], flags);
665 free(file->deps, M_LINKER);
668 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
669 STAILQ_REMOVE_HEAD(&file->common, link);
674 if (file->filename) {
675 free(file->filename, M_LINKER);
676 file->filename = NULL;
678 if (file->pathname) {
679 free(file->pathname, M_LINKER);
680 file->pathname = NULL;
682 kobj_delete((kobj_t) file, M_LINKER);
687 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
689 return (LINKER_CTF_GET(file, lc));
693 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
695 linker_file_t *newdeps;
698 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
699 M_LINKER, M_WAITOK | M_ZERO);
704 bcopy(file->deps, newdeps,
705 file->ndeps * sizeof(linker_file_t *));
706 free(file->deps, M_LINKER);
708 file->deps = newdeps;
709 file->deps[file->ndeps] = dep;
715 * Locate a linker set and its contents. This is a helper function to avoid
716 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
717 * This function is used in this file so we can avoid having lots of (void **)
721 linker_file_lookup_set(linker_file_t file, const char *name,
722 void *firstp, void *lastp, int *countp)
726 locked = KLD_LOCKED();
729 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
736 * List all functions in a file.
739 linker_file_function_listall(linker_file_t lf,
740 linker_function_nameval_callback_t callback_func, void *arg)
742 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
746 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
751 locked = KLD_LOCKED();
754 sym = linker_file_lookup_symbol_internal(file, name, deps);
761 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
765 linker_symval_t symval;
767 size_t common_size = 0;
771 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
774 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
775 LINKER_SYMBOL_VALUES(file, sym, &symval);
776 if (symval.value == 0)
778 * For commons, first look them up in the
779 * dependencies and only allocate space if not found
782 common_size = symval.size;
784 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
785 ".value=%p\n", symval.value));
786 return (symval.value);
790 for (i = 0; i < file->ndeps; i++) {
791 address = linker_file_lookup_symbol_internal(
792 file->deps[i], name, 0);
794 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
795 " deps value=%p\n", address));
800 if (common_size > 0) {
802 * This is a common symbol which was not found in the
803 * dependencies. We maintain a simple common symbol table in
806 struct common_symbol *cp;
808 STAILQ_FOREACH(cp, &file->common, link) {
809 if (strcmp(cp->name, name) == 0) {
810 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
811 " old common value=%p\n", cp->address));
812 return (cp->address);
816 * Round the symbol size up to align.
818 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
819 cp = malloc(sizeof(struct common_symbol)
820 + common_size + strlen(name) + 1, M_LINKER,
822 cp->address = (caddr_t)(cp + 1);
823 cp->name = cp->address + common_size;
824 strcpy(cp->name, name);
825 bzero(cp->address, common_size);
826 STAILQ_INSERT_TAIL(&file->common, cp, link);
828 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
829 " value=%p\n", cp->address));
830 return (cp->address);
832 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
837 * Both DDB and stack(9) rely on the kernel linker to provide forward and
838 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
839 * do this in a lockfree manner. We provide a set of internal helper
840 * routines to perform these operations without locks, and then wrappers that
843 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
847 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
851 TAILQ_FOREACH(lf, &linker_files, link) {
852 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
860 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
863 c_linker_sym_t best, es;
864 u_long diff, bestdiff, off;
867 off = (uintptr_t)value;
869 TAILQ_FOREACH(lf, &linker_files, link) {
870 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
872 if (es != 0 && diff < bestdiff) {
891 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
895 TAILQ_FOREACH(lf, &linker_files, link) {
896 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
903 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
906 linker_symval_t symval;
911 error = linker_debug_search_symbol(value, &sym, offset);
914 error = linker_debug_symbol_values(sym, &symval);
917 strlcpy(buf, symval.name, buflen);
923 * DDB Helpers. DDB has to look across multiple files with their own symbol
924 * tables and string tables.
926 * Note that we do not obey list locking protocols here. We really don't need
927 * DDB to hang because somebody's got the lock held. We'll take the chance
928 * that the files list is inconsistant instead.
931 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
934 return (linker_debug_lookup(symstr, sym));
938 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
941 return (linker_debug_search_symbol(value, sym, diffp));
945 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
948 return (linker_debug_symbol_values(sym, symval));
952 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
956 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
961 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
962 * obey locking protocols, and offer a significantly less complex interface.
965 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
971 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
980 kern_kldload(struct thread *td, const char *file, int *fileid)
983 struct pmckern_map_in pkm;
985 const char *kldname, *modname;
989 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
992 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
996 /* Only the default vimage is permitted to kldload modules. */
997 if (!IS_DEFAULT_VIMAGE(TD_TO_VIMAGE(td)))
1002 * It is possible that kldloaded module will attach a new ifnet,
1003 * so vnet context must be set when this ocurs.
1005 CURVNET_SET(TD_TO_VNET(td));
1008 * If file does not contain a qualified name or any dot in it
1009 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1012 if (index(file, '/') || index(file, '.')) {
1021 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1025 pkm.pm_file = lf->filename;
1026 pkm.pm_address = (uintptr_t) lf->address;
1027 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
1039 kldload(struct thread *td, struct kldload_args *uap)
1041 char *pathname = NULL;
1044 td->td_retval[0] = -1;
1046 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1047 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1049 error = kern_kldload(td, pathname, &fileid);
1051 td->td_retval[0] = fileid;
1053 free(pathname, M_TEMP);
1058 kern_kldunload(struct thread *td, int fileid, int flags)
1061 struct pmckern_map_out pkm;
1066 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1069 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1073 /* Only the default vimage is permitted to kldunload modules. */
1074 if (!IS_DEFAULT_VIMAGE(TD_TO_VIMAGE(td)))
1078 CURVNET_SET(TD_TO_VNET(td));
1080 lf = linker_find_file_by_id(fileid);
1082 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1084 /* Check if there are DTrace probes enabled on this file. */
1085 if (lf->nenabled > 0) {
1086 printf("kldunload: attempt to unload file that has"
1087 " DTrace probes enabled\n");
1089 } else if (lf->userrefs == 0) {
1091 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1093 printf("kldunload: attempt to unload file that was"
1094 " loaded by the kernel\n");
1098 /* Save data needed by hwpmc(4) before unloading. */
1099 pkm.pm_address = (uintptr_t) lf->address;
1100 pkm.pm_size = lf->size;
1103 error = linker_file_unload(lf, flags);
1112 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
1120 kldunload(struct thread *td, struct kldunload_args *uap)
1123 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1127 kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1130 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1131 uap->flags != LINKER_UNLOAD_FORCE)
1133 return (kern_kldunload(td, uap->fileid, uap->flags));
1137 kldfind(struct thread *td, struct kldfind_args *uap)
1140 const char *filename;
1145 error = mac_kld_check_stat(td->td_ucred);
1150 td->td_retval[0] = -1;
1152 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1153 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1156 filename = linker_basename(pathname);
1158 lf = linker_find_file_by_name(filename);
1160 td->td_retval[0] = lf->id;
1165 free(pathname, M_TEMP);
1170 kldnext(struct thread *td, struct kldnext_args *uap)
1176 error = mac_kld_check_stat(td->td_ucred);
1182 if (uap->fileid == 0)
1183 lf = TAILQ_FIRST(&linker_files);
1185 lf = linker_find_file_by_id(uap->fileid);
1190 lf = TAILQ_NEXT(lf, link);
1193 /* Skip partially loaded files. */
1194 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1195 lf = TAILQ_NEXT(lf, link);
1198 td->td_retval[0] = lf->id;
1200 td->td_retval[0] = 0;
1207 kldstat(struct thread *td, struct kldstat_args *uap)
1209 struct kld_file_stat stat;
1211 int error, namelen, version, version_num;
1214 * Check the version of the user's structure.
1216 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) != 0)
1218 if (version == sizeof(struct kld_file_stat_1))
1220 else if (version == sizeof(struct kld_file_stat))
1226 error = mac_kld_check_stat(td->td_ucred);
1232 lf = linker_find_file_by_id(uap->fileid);
1238 /* Version 1 fields: */
1239 namelen = strlen(lf->filename) + 1;
1240 if (namelen > MAXPATHLEN)
1241 namelen = MAXPATHLEN;
1242 bcopy(lf->filename, &stat.name[0], namelen);
1243 stat.refs = lf->refs;
1245 stat.address = lf->address;
1246 stat.size = lf->size;
1247 if (version_num > 1) {
1248 /* Version 2 fields: */
1249 namelen = strlen(lf->pathname) + 1;
1250 if (namelen > MAXPATHLEN)
1251 namelen = MAXPATHLEN;
1252 bcopy(lf->pathname, &stat.pathname[0], namelen);
1256 td->td_retval[0] = 0;
1258 return (copyout(&stat, uap->stat, version));
1262 kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1269 error = mac_kld_check_stat(td->td_ucred);
1275 lf = linker_find_file_by_id(uap->fileid);
1278 mp = TAILQ_FIRST(&lf->modules);
1280 td->td_retval[0] = module_getid(mp);
1282 td->td_retval[0] = 0;
1291 kldsym(struct thread *td, struct kldsym_args *uap)
1293 char *symstr = NULL;
1295 linker_symval_t symval;
1297 struct kld_sym_lookup lookup;
1301 error = mac_kld_check_stat(td->td_ucred);
1306 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1308 if (lookup.version != sizeof(lookup) ||
1309 uap->cmd != KLDSYM_LOOKUP)
1311 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1312 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1315 if (uap->fileid != 0) {
1316 lf = linker_find_file_by_id(uap->fileid);
1319 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1320 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1321 lookup.symvalue = (uintptr_t) symval.value;
1322 lookup.symsize = symval.size;
1323 error = copyout(&lookup, uap->data, sizeof(lookup));
1327 TAILQ_FOREACH(lf, &linker_files, link) {
1328 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1329 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1330 lookup.symvalue = (uintptr_t)symval.value;
1331 lookup.symsize = symval.size;
1332 error = copyout(&lookup, uap->data,
1337 #ifndef VIMAGE_GLOBALS
1339 * If the symbol is not found in global namespace,
1340 * try to look it up in the current vimage namespace.
1343 CURVNET_SET(TD_TO_VNET(td));
1344 error = vi_symlookup(&lookup, symstr);
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);
1608 /* Don't count self-dependencies */
1609 if (lf == mod->container)
1611 mod->container->refs++;
1612 error = linker_file_add_dependency(lf,
1615 panic("cannot add dependency");
1619 * Now do relocation etc using the symbol search paths
1620 * established by the dependencies
1622 error = LINKER_LINK_PRELOAD_FINISH(lf);
1624 TAILQ_REMOVE(&depended_files, lf, loaded);
1625 printf("KLD file %s - could not finalize loading\n",
1627 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1630 linker_file_register_modules(lf);
1631 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1632 &si_stop, NULL) == 0)
1633 sysinit_add(si_start, si_stop);
1634 linker_file_register_sysctls(lf);
1635 lf->flags |= LINKER_FILE_LINKED;
1637 /* woohoo! we made it! */
1640 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1643 * Search for a not-loaded module by name.
1645 * Modules may be found in the following locations:
1647 * - preloaded (result is just the module name) - on disk (result is full path
1650 * If the module name is qualified in any way (contains path, etc.) the we
1651 * simply return a copy of it.
1653 * The search path can be manipulated via sysctl. Note that we use the ';'
1654 * character as a separator to be consistent with the bootloader.
1657 static char linker_hintfile[] = "linker.hints";
1658 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1660 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1661 sizeof(linker_path), "module load search path");
1663 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1665 static char *linker_ext_list[] = {
1672 * Check if file actually exists either with or without extension listed in
1673 * the linker_ext_list. (probably should be generic for the rest of the
1677 linker_lookup_file(const char *path, int pathlen, const char *name,
1678 int namelen, struct vattr *vap)
1680 struct nameidata nd;
1681 struct thread *td = curthread; /* XXX */
1682 char *result, **cpp, *sep;
1683 int error, len, extlen, reclen, flags, vfslocked;
1687 for (cpp = linker_ext_list; *cpp; cpp++) {
1692 extlen++; /* trailing '\0' */
1693 sep = (path[pathlen - 1] != '/') ? "/" : "";
1695 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1696 result = malloc(reclen, M_LINKER, M_WAITOK);
1697 for (cpp = linker_ext_list; *cpp; cpp++) {
1698 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1699 namelen, name, *cpp);
1701 * Attempt to open the file, and return the path if
1702 * we succeed and it's a regular file.
1704 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
1706 error = vn_open(&nd, &flags, 0, NULL);
1708 vfslocked = NDHASGIANT(&nd);
1709 NDFREE(&nd, NDF_ONLY_PNBUF);
1710 type = nd.ni_vp->v_type;
1712 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1713 VOP_UNLOCK(nd.ni_vp, 0);
1714 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1715 VFS_UNLOCK_GIANT(vfslocked);
1720 free(result, M_LINKER);
1724 #define INT_ALIGN(base, ptr) ptr = \
1725 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1728 * Lookup KLD which contains requested module in the "linker.hints" file. If
1729 * version specification is available, then try to find the best KLD.
1730 * Otherwise just find the latest one.
1733 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1734 int modnamelen, struct mod_depend *verinfo)
1736 struct thread *td = curthread; /* XXX */
1737 struct ucred *cred = td ? td->td_ucred : NULL;
1738 struct nameidata nd;
1739 struct vattr vattr, mattr;
1740 u_char *hints = NULL;
1741 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1742 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1746 bestver = found = 0;
1748 sep = (path[pathlen - 1] != '/') ? "/" : "";
1749 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1751 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1752 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1755 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
1757 error = vn_open(&nd, &flags, 0, NULL);
1760 vfslocked = NDHASGIANT(&nd);
1761 NDFREE(&nd, NDF_ONLY_PNBUF);
1762 if (nd.ni_vp->v_type != VREG)
1765 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1769 * XXX: we need to limit this number to some reasonable value
1771 if (vattr.va_size > 100 * 1024) {
1772 printf("hints file too large %ld\n", (long)vattr.va_size);
1775 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1778 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1779 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1782 VOP_UNLOCK(nd.ni_vp, 0);
1783 vn_close(nd.ni_vp, FREAD, cred, td);
1784 VFS_UNLOCK_GIANT(vfslocked);
1787 printf("can't read %d\n", reclen);
1790 intp = (int *)hints;
1792 if (ival != LINKER_HINTS_VERSION) {
1793 printf("hints file version mismatch %d\n", ival);
1796 bufend = hints + vattr.va_size;
1797 recptr = (u_char *)intp;
1799 while (recptr < bufend && !found) {
1800 intp = (int *)recptr;
1807 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1810 INT_ALIGN(hints, cp);
1814 if (verinfo == NULL ||
1815 ival == verinfo->md_ver_preferred) {
1819 if (ival >= verinfo->md_ver_minimum &&
1820 ival <= verinfo->md_ver_maximum &&
1830 recptr += reclen + sizeof(int);
1833 * Finally check if KLD is in the place
1836 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1838 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1841 * KLD is newer than hints file. What we should do now?
1843 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1844 printf("warning: KLD '%s' is newer than the linker.hints"
1847 free(pathbuf, M_LINKER);
1849 free(hints, M_TEMP);
1850 if (nd.ni_vp != NULL) {
1851 VOP_UNLOCK(nd.ni_vp, 0);
1852 vn_close(nd.ni_vp, FREAD, cred, td);
1853 VFS_UNLOCK_GIANT(vfslocked);
1856 * If nothing found or hints is absent - fallback to the old
1857 * way by using "kldname[.ko]" as module name.
1859 if (!found && !bestver && result == NULL)
1860 result = linker_lookup_file(path, pathlen, modname,
1866 * Lookup KLD which contains requested module in the all directories.
1869 linker_search_module(const char *modname, int modnamelen,
1870 struct mod_depend *verinfo)
1872 char *cp, *ep, *result;
1875 * traverse the linker path
1877 for (cp = linker_path; *cp; cp = ep + 1) {
1878 /* find the end of this component */
1879 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1880 result = linker_hints_lookup(cp, ep - cp, modname,
1881 modnamelen, verinfo);
1891 * Search for module in all directories listed in the linker_path.
1894 linker_search_kld(const char *name)
1896 char *cp, *ep, *result;
1899 /* qualified at all? */
1900 if (index(name, '/'))
1901 return (linker_strdup(name));
1903 /* traverse the linker path */
1905 for (ep = linker_path; *ep; ep++) {
1907 /* find the end of this component */
1908 for (; *ep != 0 && *ep != ';'; ep++);
1909 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1917 linker_basename(const char *path)
1919 const char *filename;
1921 filename = rindex(path, '/');
1922 if (filename == NULL)
1931 struct hwpmc_context {
1934 struct pmckern_map_in *kobase;
1938 linker_hwpmc_list_object(linker_file_t lf, void *arg)
1940 struct hwpmc_context *hc;
1944 /* If we run out of mappings, fail. */
1945 if (hc->nobjects >= hc->nmappings)
1948 /* Save the info for this linker file. */
1949 hc->kobase[hc->nobjects].pm_file = lf->filename;
1950 hc->kobase[hc->nobjects].pm_address = (uintptr_t)lf->address;
1956 * Inform hwpmc about the set of kernel modules currently loaded.
1959 linker_hwpmc_list_objects(void)
1961 struct hwpmc_context hc;
1963 hc.nmappings = 15; /* a reasonable default */
1966 /* allocate nmappings+1 entries */
1967 hc.kobase = malloc((hc.nmappings + 1) * sizeof(struct pmckern_map_in),
1968 M_LINKER, M_WAITOK | M_ZERO);
1971 if (linker_file_foreach(linker_hwpmc_list_object, &hc) != 0) {
1972 hc.nmappings = hc.nobjects;
1973 free(hc.kobase, M_LINKER);
1977 KASSERT(hc.nobjects > 0, ("linker_hpwmc_list_objects: no kernel "
1980 /* The last entry of the malloced area comprises of all zeros. */
1981 KASSERT(hc.kobase[hc.nobjects].pm_file == NULL,
1982 ("linker_hwpmc_list_objects: last object not NULL"));
1984 return ((void *)hc.kobase);
1989 * Find a file which contains given module and load it, if "parent" is not
1990 * NULL, register a reference to it.
1993 linker_load_module(const char *kldname, const char *modname,
1994 struct linker_file *parent, struct mod_depend *verinfo,
1995 struct linker_file **lfpp)
1997 linker_file_t lfdep;
1998 const char *filename;
2003 if (modname == NULL) {
2005 * We have to load KLD
2007 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2009 pathname = linker_search_kld(kldname);
2011 if (modlist_lookup2(modname, verinfo) != NULL)
2013 if (kldname != NULL)
2014 pathname = linker_strdup(kldname);
2015 else if (rootvnode == NULL)
2019 * Need to find a KLD with required module
2021 pathname = linker_search_module(modname,
2022 strlen(modname), verinfo);
2024 if (pathname == NULL)
2028 * Can't load more than one file with the same basename XXX:
2029 * Actually it should be possible to have multiple KLDs with
2030 * the same basename but different path because they can
2031 * provide different versions of the same modules.
2033 filename = linker_basename(pathname);
2034 if (linker_find_file_by_name(filename))
2037 error = linker_load_file(pathname, &lfdep);
2040 if (modname && verinfo &&
2041 modlist_lookup2(modname, verinfo) == NULL) {
2042 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2047 error = linker_file_add_dependency(parent, lfdep);
2054 free(pathname, M_LINKER);
2059 * This routine is responsible for finding dependencies of userland initiated
2060 * kldload(2)'s of files.
2063 linker_load_dependencies(linker_file_t lf)
2065 linker_file_t lfdep;
2066 struct mod_metadata **start, **stop, **mdp, **nmdp;
2067 struct mod_metadata *mp, *nmp;
2068 struct mod_depend *verinfo;
2070 const char *modname, *nmodname;
2071 int ver, error = 0, count;
2074 * All files are dependant on /kernel.
2077 if (linker_kernel_file) {
2078 linker_kernel_file->refs++;
2079 error = linker_file_add_dependency(lf, linker_kernel_file);
2083 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2086 for (mdp = start; mdp < stop; mdp++) {
2088 if (mp->md_type != MDT_VERSION)
2090 modname = mp->md_cval;
2091 ver = ((struct mod_version *)mp->md_data)->mv_version;
2092 mod = modlist_lookup(modname, ver);
2094 printf("interface %s.%d already present in the KLD"
2095 " '%s'!\n", modname, ver,
2096 mod->container->filename);
2101 for (mdp = start; mdp < stop; mdp++) {
2103 if (mp->md_type != MDT_DEPEND)
2105 modname = mp->md_cval;
2106 verinfo = mp->md_data;
2108 for (nmdp = start; nmdp < stop; nmdp++) {
2110 if (nmp->md_type != MDT_VERSION)
2112 nmodname = nmp->md_cval;
2113 if (strcmp(modname, nmodname) == 0)
2116 if (nmdp < stop)/* early exit, it's a self reference */
2118 mod = modlist_lookup2(modname, verinfo);
2119 if (mod) { /* woohoo, it's loaded already */
2120 lfdep = mod->container;
2122 error = linker_file_add_dependency(lf, lfdep);
2127 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2129 printf("KLD %s: depends on %s - not available\n",
2130 lf->filename, modname);
2137 linker_addmodules(lf, start, stop, 0);
2142 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2144 struct sysctl_req *req;
2147 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2151 * Export a nul-separated, double-nul-terminated list of all function names
2155 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2161 error = mac_kld_check_stat(req->td->td_ucred);
2165 error = sysctl_wire_old_buffer(req, 0);
2169 TAILQ_FOREACH(lf, &linker_files, link) {
2170 error = LINKER_EACH_FUNCTION_NAME(lf,
2171 sysctl_kern_function_list_iterate, req);
2178 return (SYSCTL_OUT(req, "", 1));
2181 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
2182 NULL, 0, sysctl_kern_function_list, "", "kernel function list");