2 * Copyright (c) 1997-2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_hwpmc_hooks.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/sysproto.h>
38 #include <sys/sysent.h>
42 #include <sys/mutex.h>
44 #include <sys/module.h>
45 #include <sys/mount.h>
46 #include <sys/linker.h>
47 #include <sys/fcntl.h>
49 #include <sys/libkern.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysctl.h>
57 #include <security/mac/mac_framework.h>
59 #include "linker_if.h"
62 #include <sys/pmckern.h>
69 #define KLD_LOCK() sx_xlock(&kld_sx)
70 #define KLD_UNLOCK() sx_xunlock(&kld_sx)
71 #define KLD_LOCKED() sx_xlocked(&kld_sx)
72 #define KLD_LOCK_ASSERT() do { \
74 sx_assert(&kld_sx, SX_XLOCKED); \
78 * static char *linker_search_path(const char *name, struct mod_depend
81 static const char *linker_basename(const char *path);
84 * Find a currently loaded file given its filename.
86 static linker_file_t linker_find_file_by_name(const char* _filename);
89 * Find a currently loaded file given its file id.
91 static linker_file_t linker_find_file_by_id(int _fileid);
93 /* Metadata from the static kernel */
94 SET_DECLARE(modmetadata_set, struct mod_metadata);
96 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
98 linker_file_t linker_kernel_file;
100 static struct sx kld_sx; /* kernel linker lock */
103 * Load counter used by clients to determine if a linker file has been
104 * re-loaded. This counter is incremented for each file load.
108 static linker_class_list_t classes;
109 static linker_file_list_t linker_files;
110 static int next_file_id = 1;
111 static int linker_no_more_classes = 0;
113 #define LINKER_GET_NEXT_FILE_ID(a) do { \
114 linker_file_t lftmp; \
118 TAILQ_FOREACH(lftmp, &linker_files, link) { \
119 if (next_file_id == lftmp->id) { \
124 (a) = next_file_id; \
128 /* XXX wrong name; we're looking at version provision tags here, not modules */
129 typedef TAILQ_HEAD(, modlist) modlisthead_t;
131 TAILQ_ENTRY(modlist) link; /* chain together all modules */
132 linker_file_t container;
136 typedef struct modlist *modlist_t;
137 static modlisthead_t found_modules;
139 static int linker_file_add_dependency(linker_file_t file,
141 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
142 const char* name, int deps);
143 static int linker_load_module(const char *kldname,
144 const char *modname, struct linker_file *parent,
145 struct mod_depend *verinfo, struct linker_file **lfpp);
146 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
149 linker_strdup(const char *str)
153 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
159 linker_init(void *arg)
162 sx_init(&kld_sx, "kernel linker");
163 TAILQ_INIT(&classes);
164 TAILQ_INIT(&linker_files);
167 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
170 linker_stop_class_add(void *arg)
173 linker_no_more_classes = 1;
176 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
179 linker_add_class(linker_class_t lc)
183 * We disallow any class registration past SI_ORDER_ANY
184 * of SI_SUB_KLD. We bump the reference count to keep the
185 * ops from being freed.
187 if (linker_no_more_classes == 1)
189 kobj_class_compile((kobj_class_t) lc);
190 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
191 TAILQ_INSERT_TAIL(&classes, lc, link);
196 linker_file_sysinit(linker_file_t lf)
198 struct sysinit **start, **stop, **sipp, **xipp, *save;
200 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
203 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
206 * Perform a bubble sort of the system initialization objects by
207 * their subsystem (primary key) and order (secondary key).
209 * Since some things care about execution order, this is the operation
210 * which ensures continued function.
212 for (sipp = start; sipp < stop; sipp++) {
213 for (xipp = sipp + 1; xipp < stop; xipp++) {
214 if ((*sipp)->subsystem < (*xipp)->subsystem ||
215 ((*sipp)->subsystem == (*xipp)->subsystem &&
216 (*sipp)->order <= (*xipp)->order))
225 * Traverse the (now) ordered list of system initialization tasks.
226 * Perform each task, and continue on to the next task.
229 for (sipp = start; sipp < stop; sipp++) {
230 if ((*sipp)->subsystem == SI_SUB_DUMMY)
231 continue; /* skip dummy task(s) */
234 (*((*sipp)->func)) ((*sipp)->udata);
240 linker_file_sysuninit(linker_file_t lf)
242 struct sysinit **start, **stop, **sipp, **xipp, *save;
244 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
247 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
252 * Perform a reverse bubble sort of the system initialization objects
253 * by their subsystem (primary key) and order (secondary key).
255 * Since some things care about execution order, this is the operation
256 * which ensures continued function.
258 for (sipp = start; sipp < stop; sipp++) {
259 for (xipp = sipp + 1; xipp < stop; xipp++) {
260 if ((*sipp)->subsystem > (*xipp)->subsystem ||
261 ((*sipp)->subsystem == (*xipp)->subsystem &&
262 (*sipp)->order >= (*xipp)->order))
271 * Traverse the (now) ordered list of system initialization tasks.
272 * Perform each task, and continue on to the next task.
275 for (sipp = start; sipp < stop; sipp++) {
276 if ((*sipp)->subsystem == SI_SUB_DUMMY)
277 continue; /* skip dummy task(s) */
280 (*((*sipp)->func)) ((*sipp)->udata);
286 linker_file_register_sysctls(linker_file_t lf)
288 struct sysctl_oid **start, **stop, **oidp;
291 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
294 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
298 for (oidp = start; oidp < stop; oidp++)
299 sysctl_register_oid(*oidp);
304 linker_file_unregister_sysctls(linker_file_t lf)
306 struct sysctl_oid **start, **stop, **oidp;
308 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
309 " for %s\n", lf->filename));
311 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
315 for (oidp = start; oidp < stop; oidp++)
316 sysctl_unregister_oid(*oidp);
321 linker_file_register_modules(linker_file_t lf)
323 struct mod_metadata **start, **stop, **mdp;
324 const moduledata_t *moddata;
325 int first_error, error;
327 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
328 " in %s\n", lf->filename));
330 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
333 * This fallback should be unnecessary, but if we get booted
334 * from boot2 instead of loader and we are missing our
335 * metadata then we have to try the best we can.
337 if (lf == linker_kernel_file) {
338 start = SET_BEGIN(modmetadata_set);
339 stop = SET_LIMIT(modmetadata_set);
344 for (mdp = start; mdp < stop; mdp++) {
345 if ((*mdp)->md_type != MDT_MODULE)
347 moddata = (*mdp)->md_data;
348 KLD_DPF(FILE, ("Registering module %s in %s\n",
349 moddata->name, lf->filename));
350 error = module_register(moddata, lf);
352 printf("Module %s failed to register: %d\n",
353 moddata->name, error);
354 if (first_error == 0)
358 return (first_error);
362 linker_init_kernel_modules(void)
365 linker_file_register_modules(linker_kernel_file);
368 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
372 linker_load_file(const char *filename, linker_file_t *result)
376 int foundfile, error;
378 /* Refuse to load modules if securelevel raised */
379 if (prison0.pr_securelevel > 0)
383 lf = linker_find_file_by_name(filename);
385 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
386 " incrementing refs\n", filename));
395 * We do not need to protect (lock) classes here because there is
396 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
397 * and there is no class deregistration mechanism at this time.
399 TAILQ_FOREACH(lc, &classes, link) {
400 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
402 error = LINKER_LOAD_FILE(lc, filename, &lf);
404 * If we got something other than ENOENT, then it exists but
405 * we cannot load it for some other reason.
410 error = linker_file_register_modules(lf);
411 if (error == EEXIST) {
412 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
416 linker_file_register_sysctls(lf);
417 linker_file_sysinit(lf);
419 lf->flags |= LINKER_FILE_LINKED;
425 * Less than ideal, but tells the user whether it failed to load or
426 * the module was not found.
431 * If the file type has not been recognized by the last try
432 * printout a message before to fail.
435 printf("linker_load_file: Unsupported file type\n");
438 * Format not recognized or otherwise unloadable.
439 * When loading a module that is statically built into
440 * the kernel EEXIST percolates back up as the return
441 * value. Preserve this so that apps like sysinstall
442 * can recognize this special case and not post bogus
448 error = ENOENT; /* Nothing found */
453 linker_reference_module(const char *modname, struct mod_depend *verinfo,
454 linker_file_t *result)
460 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
461 *result = mod->container;
467 error = linker_load_module(NULL, modname, NULL, verinfo, result);
473 linker_release_module(const char *modname, struct mod_depend *verinfo,
481 KASSERT(modname != NULL,
482 ("linker_release_module: no file or name"));
483 mod = modlist_lookup2(modname, verinfo);
490 KASSERT(modname == NULL && verinfo == NULL,
491 ("linker_release_module: both file and name"));
492 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
498 linker_find_file_by_name(const char *filename)
503 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
504 sprintf(koname, "%s.ko", filename);
507 TAILQ_FOREACH(lf, &linker_files, link) {
508 if (strcmp(lf->filename, koname) == 0)
510 if (strcmp(lf->filename, filename) == 0)
513 free(koname, M_LINKER);
518 linker_find_file_by_id(int fileid)
523 TAILQ_FOREACH(lf, &linker_files, link)
524 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
530 linker_file_foreach(linker_predicate_t *predicate, void *context)
536 TAILQ_FOREACH(lf, &linker_files, link) {
537 retval = predicate(lf, context);
546 linker_make_file(const char *pathname, linker_class_t lc)
549 const char *filename;
552 filename = linker_basename(pathname);
554 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
555 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
561 lf->filename = linker_strdup(filename);
562 lf->pathname = linker_strdup(pathname);
563 LINKER_GET_NEXT_FILE_ID(lf->id);
566 lf->loadcnt = ++loadcnt;
567 lf->sdt_probes = NULL;
569 STAILQ_INIT(&lf->common);
570 TAILQ_INIT(&lf->modules);
571 TAILQ_INSERT_TAIL(&linker_files, lf, link);
576 linker_file_unload(linker_file_t file, int flags)
579 modlist_t ml, nextml;
580 struct common_symbol *cp;
583 /* Refuse to unload modules if securelevel raised. */
584 if (prison0.pr_securelevel > 0)
588 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
590 /* Easy case of just dropping a reference. */
591 if (file->refs > 1) {
596 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
597 " informing modules\n"));
600 * Quiesce all the modules to give them a chance to veto the unload.
603 for (mod = TAILQ_FIRST(&file->modules); mod;
604 mod = module_getfnext(mod)) {
606 error = module_quiesce(mod);
607 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
608 KLD_DPF(FILE, ("linker_file_unload: module %s"
609 " vetoed unload\n", module_getname(mod)));
611 * XXX: Do we need to tell all the quiesced modules
612 * that they can resume work now via a new module
622 * Inform any modules associated with this file that they are
626 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
627 next = module_getfnext(mod);
631 * Give the module a chance to veto the unload.
633 if ((error = module_unload(mod)) != 0) {
634 KLD_DPF(FILE, ("linker_file_unload: module %s"
635 " failed unload\n", mod));
643 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
644 if (ml->container == file) {
645 TAILQ_REMOVE(&found_modules, ml, link);
651 * Don't try to run SYSUNINITs if we are unloaded due to a
654 if (file->flags & LINKER_FILE_LINKED) {
655 file->flags &= ~LINKER_FILE_LINKED;
657 linker_file_sysuninit(file);
658 linker_file_unregister_sysctls(file);
661 TAILQ_REMOVE(&linker_files, file, link);
664 for (i = 0; i < file->ndeps; i++)
665 linker_file_unload(file->deps[i], flags);
666 free(file->deps, M_LINKER);
669 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
670 STAILQ_REMOVE_HEAD(&file->common, link);
675 if (file->filename) {
676 free(file->filename, M_LINKER);
677 file->filename = NULL;
679 if (file->pathname) {
680 free(file->pathname, M_LINKER);
681 file->pathname = NULL;
683 kobj_delete((kobj_t) file, M_LINKER);
688 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
690 return (LINKER_CTF_GET(file, lc));
694 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
696 linker_file_t *newdeps;
699 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
700 M_LINKER, M_WAITOK | M_ZERO);
705 bcopy(file->deps, newdeps,
706 file->ndeps * sizeof(linker_file_t *));
707 free(file->deps, M_LINKER);
709 file->deps = newdeps;
710 file->deps[file->ndeps] = dep;
716 * Locate a linker set and its contents. This is a helper function to avoid
717 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
718 * This function is used in this file so we can avoid having lots of (void **)
722 linker_file_lookup_set(linker_file_t file, const char *name,
723 void *firstp, void *lastp, int *countp)
727 locked = KLD_LOCKED();
730 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
737 * List all functions in a file.
740 linker_file_function_listall(linker_file_t lf,
741 linker_function_nameval_callback_t callback_func, void *arg)
743 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
747 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
752 locked = KLD_LOCKED();
755 sym = linker_file_lookup_symbol_internal(file, name, deps);
762 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
766 linker_symval_t symval;
768 size_t common_size = 0;
772 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
775 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
776 LINKER_SYMBOL_VALUES(file, sym, &symval);
777 if (symval.value == 0)
779 * For commons, first look them up in the
780 * dependencies and only allocate space if not found
783 common_size = symval.size;
785 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
786 ".value=%p\n", symval.value));
787 return (symval.value);
791 for (i = 0; i < file->ndeps; i++) {
792 address = linker_file_lookup_symbol_internal(
793 file->deps[i], name, 0);
795 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
796 " deps value=%p\n", address));
801 if (common_size > 0) {
803 * This is a common symbol which was not found in the
804 * dependencies. We maintain a simple common symbol table in
807 struct common_symbol *cp;
809 STAILQ_FOREACH(cp, &file->common, link) {
810 if (strcmp(cp->name, name) == 0) {
811 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
812 " old common value=%p\n", cp->address));
813 return (cp->address);
817 * Round the symbol size up to align.
819 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
820 cp = malloc(sizeof(struct common_symbol)
821 + common_size + strlen(name) + 1, M_LINKER,
823 cp->address = (caddr_t)(cp + 1);
824 cp->name = cp->address + common_size;
825 strcpy(cp->name, name);
826 bzero(cp->address, common_size);
827 STAILQ_INSERT_TAIL(&file->common, cp, link);
829 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
830 " value=%p\n", cp->address));
831 return (cp->address);
833 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
838 * Both DDB and stack(9) rely on the kernel linker to provide forward and
839 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
840 * do this in a lockfree manner. We provide a set of internal helper
841 * routines to perform these operations without locks, and then wrappers that
844 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
848 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
852 TAILQ_FOREACH(lf, &linker_files, link) {
853 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
861 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
864 c_linker_sym_t best, es;
865 u_long diff, bestdiff, off;
868 off = (uintptr_t)value;
870 TAILQ_FOREACH(lf, &linker_files, link) {
871 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
873 if (es != 0 && diff < bestdiff) {
892 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
896 TAILQ_FOREACH(lf, &linker_files, link) {
897 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
904 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
907 linker_symval_t symval;
912 error = linker_debug_search_symbol(value, &sym, offset);
915 error = linker_debug_symbol_values(sym, &symval);
918 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.
932 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
935 return (linker_debug_lookup(symstr, sym));
940 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
943 return (linker_debug_search_symbol(value, sym, diffp));
947 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
950 return (linker_debug_symbol_values(sym, symval));
954 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
958 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
962 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
963 * obey locking protocols, and offer a significantly less complex interface.
966 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
972 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
981 kern_kldload(struct thread *td, const char *file, int *fileid)
984 struct pmckern_map_in pkm;
986 const char *kldname, *modname;
990 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
993 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
997 * It is possible that kldloaded module will attach a new ifnet,
998 * so vnet context must be set when this ocurs.
1000 CURVNET_SET(TD_TO_VNET(td));
1003 * If file does not contain a qualified name or any dot in it
1004 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1007 if (index(file, '/') || index(file, '.')) {
1016 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1020 pkm.pm_file = lf->filename;
1021 pkm.pm_address = (uintptr_t) lf->address;
1022 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
1034 kldload(struct thread *td, struct kldload_args *uap)
1036 char *pathname = NULL;
1039 td->td_retval[0] = -1;
1041 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1042 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1044 error = kern_kldload(td, pathname, &fileid);
1046 td->td_retval[0] = fileid;
1048 free(pathname, M_TEMP);
1053 kern_kldunload(struct thread *td, int fileid, int flags)
1056 struct pmckern_map_out pkm;
1061 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1064 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1067 CURVNET_SET(TD_TO_VNET(td));
1069 lf = linker_find_file_by_id(fileid);
1071 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1073 /* Check if there are DTrace probes enabled on this file. */
1074 if (lf->nenabled > 0) {
1075 printf("kldunload: attempt to unload file that has"
1076 " DTrace probes enabled\n");
1078 } else if (lf->userrefs == 0) {
1080 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1082 printf("kldunload: attempt to unload file that was"
1083 " loaded by the kernel\n");
1087 /* Save data needed by hwpmc(4) before unloading. */
1088 pkm.pm_address = (uintptr_t) lf->address;
1089 pkm.pm_size = lf->size;
1092 error = linker_file_unload(lf, flags);
1101 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
1109 kldunload(struct thread *td, struct kldunload_args *uap)
1112 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1116 kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1119 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1120 uap->flags != LINKER_UNLOAD_FORCE)
1122 return (kern_kldunload(td, uap->fileid, uap->flags));
1126 kldfind(struct thread *td, struct kldfind_args *uap)
1129 const char *filename;
1134 error = mac_kld_check_stat(td->td_ucred);
1139 td->td_retval[0] = -1;
1141 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1142 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1145 filename = linker_basename(pathname);
1147 lf = linker_find_file_by_name(filename);
1149 td->td_retval[0] = lf->id;
1154 free(pathname, M_TEMP);
1159 kldnext(struct thread *td, struct kldnext_args *uap)
1165 error = mac_kld_check_stat(td->td_ucred);
1171 if (uap->fileid == 0)
1172 lf = TAILQ_FIRST(&linker_files);
1174 lf = linker_find_file_by_id(uap->fileid);
1179 lf = TAILQ_NEXT(lf, link);
1182 /* Skip partially loaded files. */
1183 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1184 lf = TAILQ_NEXT(lf, link);
1187 td->td_retval[0] = lf->id;
1189 td->td_retval[0] = 0;
1196 kldstat(struct thread *td, struct kldstat_args *uap)
1198 struct kld_file_stat stat;
1200 int error, namelen, version, version_num;
1203 * Check the version of the user's structure.
1205 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) != 0)
1207 if (version == sizeof(struct kld_file_stat_1))
1209 else if (version == sizeof(struct kld_file_stat))
1215 error = mac_kld_check_stat(td->td_ucred);
1221 lf = linker_find_file_by_id(uap->fileid);
1227 /* Version 1 fields: */
1228 namelen = strlen(lf->filename) + 1;
1229 if (namelen > MAXPATHLEN)
1230 namelen = MAXPATHLEN;
1231 bcopy(lf->filename, &stat.name[0], namelen);
1232 stat.refs = lf->refs;
1234 stat.address = lf->address;
1235 stat.size = lf->size;
1236 if (version_num > 1) {
1237 /* Version 2 fields: */
1238 namelen = strlen(lf->pathname) + 1;
1239 if (namelen > MAXPATHLEN)
1240 namelen = MAXPATHLEN;
1241 bcopy(lf->pathname, &stat.pathname[0], namelen);
1245 td->td_retval[0] = 0;
1247 return (copyout(&stat, uap->stat, version));
1251 kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1258 error = mac_kld_check_stat(td->td_ucred);
1264 lf = linker_find_file_by_id(uap->fileid);
1267 mp = TAILQ_FIRST(&lf->modules);
1269 td->td_retval[0] = module_getid(mp);
1271 td->td_retval[0] = 0;
1280 kldsym(struct thread *td, struct kldsym_args *uap)
1282 char *symstr = NULL;
1284 linker_symval_t symval;
1286 struct kld_sym_lookup lookup;
1290 error = mac_kld_check_stat(td->td_ucred);
1295 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1297 if (lookup.version != sizeof(lookup) ||
1298 uap->cmd != KLDSYM_LOOKUP)
1300 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1301 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1304 if (uap->fileid != 0) {
1305 lf = linker_find_file_by_id(uap->fileid);
1308 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1309 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1310 lookup.symvalue = (uintptr_t) symval.value;
1311 lookup.symsize = symval.size;
1312 error = copyout(&lookup, uap->data, sizeof(lookup));
1316 TAILQ_FOREACH(lf, &linker_files, link) {
1317 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1318 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1319 lookup.symvalue = (uintptr_t)symval.value;
1320 lookup.symsize = symval.size;
1321 error = copyout(&lookup, uap->data,
1331 free(symstr, M_TEMP);
1336 * Preloaded module support
1340 modlist_lookup(const char *name, int ver)
1344 TAILQ_FOREACH(mod, &found_modules, link) {
1345 if (strcmp(mod->name, name) == 0 &&
1346 (ver == 0 || mod->version == ver))
1353 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1355 modlist_t mod, bestmod;
1358 if (verinfo == NULL)
1359 return (modlist_lookup(name, 0));
1361 TAILQ_FOREACH(mod, &found_modules, link) {
1362 if (strcmp(mod->name, name) != 0)
1365 if (ver == verinfo->md_ver_preferred)
1367 if (ver >= verinfo->md_ver_minimum &&
1368 ver <= verinfo->md_ver_maximum &&
1369 (bestmod == NULL || ver > bestmod->version))
1376 modlist_newmodule(const char *modname, int version, linker_file_t container)
1380 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1382 panic("no memory for module list");
1383 mod->container = container;
1384 mod->name = modname;
1385 mod->version = version;
1386 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1391 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1392 struct mod_metadata **stop, int preload)
1394 struct mod_metadata *mp, **mdp;
1395 const char *modname;
1398 for (mdp = start; mdp < stop; mdp++) {
1400 if (mp->md_type != MDT_VERSION)
1402 modname = mp->md_cval;
1403 ver = ((struct mod_version *)mp->md_data)->mv_version;
1404 if (modlist_lookup(modname, ver) != NULL) {
1405 printf("module %s already present!\n", modname);
1406 /* XXX what can we do? this is a build error. :-( */
1409 modlist_newmodule(modname, ver, lf);
1414 linker_preload(void *arg)
1417 const char *modname, *nmodname;
1419 linker_file_t lf, nlf;
1422 linker_file_list_t loaded_files;
1423 linker_file_list_t depended_files;
1424 struct mod_metadata *mp, *nmp;
1425 struct mod_metadata **start, **stop, **mdp, **nmdp;
1426 struct mod_depend *verinfo;
1430 struct sysinit **si_start, **si_stop;
1432 TAILQ_INIT(&loaded_files);
1433 TAILQ_INIT(&depended_files);
1434 TAILQ_INIT(&found_modules);
1438 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1439 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1440 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1441 if (modname == NULL) {
1442 printf("Preloaded module at %p does not have a"
1443 " name!\n", modptr);
1446 if (modtype == NULL) {
1447 printf("Preloaded module at %p does not have a type!\n",
1452 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1455 TAILQ_FOREACH(lc, &classes, link) {
1456 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1462 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1466 * First get a list of stuff in the kernel.
1468 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1470 linker_addmodules(linker_kernel_file, start, stop, 1);
1473 * This is a once-off kinky bubble sort to resolve relocation
1474 * dependency requirements.
1477 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1478 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1481 * First, look to see if we would successfully link with this
1484 resolves = 1; /* unless we know otherwise */
1486 for (mdp = start; mdp < stop; mdp++) {
1488 if (mp->md_type != MDT_DEPEND)
1490 modname = mp->md_cval;
1491 verinfo = mp->md_data;
1492 for (nmdp = start; nmdp < stop; nmdp++) {
1494 if (nmp->md_type != MDT_VERSION)
1496 nmodname = nmp->md_cval;
1497 if (strcmp(modname, nmodname) == 0)
1500 if (nmdp < stop) /* it's a self reference */
1504 * ok, the module isn't here yet, we
1507 if (modlist_lookup2(modname, verinfo) == NULL)
1512 * OK, if we found our modules, we can link. So, "provide"
1513 * the modules inside and add it to the end of the link order
1518 for (mdp = start; mdp < stop; mdp++) {
1520 if (mp->md_type != MDT_VERSION)
1522 modname = mp->md_cval;
1523 nver = ((struct mod_version *)
1524 mp->md_data)->mv_version;
1525 if (modlist_lookup(modname,
1527 printf("module %s already"
1528 " present!\n", modname);
1529 TAILQ_REMOVE(&loaded_files,
1531 linker_file_unload(lf,
1532 LINKER_UNLOAD_FORCE);
1533 /* we changed tailq next ptr */
1536 modlist_newmodule(modname, nver, lf);
1539 TAILQ_REMOVE(&loaded_files, lf, loaded);
1540 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1542 * Since we provided modules, we need to restart the
1543 * sort so that the previous files that depend on us
1544 * have a chance. Also, we've busted the tailq next
1545 * pointer with the REMOVE.
1552 * At this point, we check to see what could not be resolved..
1554 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1555 TAILQ_REMOVE(&loaded_files, lf, loaded);
1556 printf("KLD file %s is missing dependencies\n", lf->filename);
1557 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1561 * We made it. Finish off the linking in the order we determined.
1563 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1564 if (linker_kernel_file) {
1565 linker_kernel_file->refs++;
1566 error = linker_file_add_dependency(lf,
1567 linker_kernel_file);
1569 panic("cannot add dependency");
1571 lf->userrefs++; /* so we can (try to) kldunload it */
1572 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1575 for (mdp = start; mdp < stop; mdp++) {
1577 if (mp->md_type != MDT_DEPEND)
1579 modname = mp->md_cval;
1580 verinfo = mp->md_data;
1581 mod = modlist_lookup2(modname, verinfo);
1582 /* Don't count self-dependencies */
1583 if (lf == mod->container)
1585 mod->container->refs++;
1586 error = linker_file_add_dependency(lf,
1589 panic("cannot add dependency");
1593 * Now do relocation etc using the symbol search paths
1594 * established by the dependencies
1596 error = LINKER_LINK_PRELOAD_FINISH(lf);
1598 TAILQ_REMOVE(&depended_files, lf, loaded);
1599 printf("KLD file %s - could not finalize loading\n",
1601 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1604 linker_file_register_modules(lf);
1605 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1606 &si_stop, NULL) == 0)
1607 sysinit_add(si_start, si_stop);
1608 linker_file_register_sysctls(lf);
1609 lf->flags |= LINKER_FILE_LINKED;
1611 /* woohoo! we made it! */
1614 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1617 * Search for a not-loaded module by name.
1619 * Modules may be found in the following locations:
1621 * - preloaded (result is just the module name) - on disk (result is full path
1624 * If the module name is qualified in any way (contains path, etc.) the we
1625 * simply return a copy of it.
1627 * The search path can be manipulated via sysctl. Note that we use the ';'
1628 * character as a separator to be consistent with the bootloader.
1631 static char linker_hintfile[] = "linker.hints";
1632 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1634 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1635 sizeof(linker_path), "module load search path");
1637 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1639 static char *linker_ext_list[] = {
1646 * Check if file actually exists either with or without extension listed in
1647 * the linker_ext_list. (probably should be generic for the rest of the
1651 linker_lookup_file(const char *path, int pathlen, const char *name,
1652 int namelen, struct vattr *vap)
1654 struct nameidata nd;
1655 struct thread *td = curthread; /* XXX */
1656 char *result, **cpp, *sep;
1657 int error, len, extlen, reclen, flags, vfslocked;
1661 for (cpp = linker_ext_list; *cpp; cpp++) {
1666 extlen++; /* trailing '\0' */
1667 sep = (path[pathlen - 1] != '/') ? "/" : "";
1669 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1670 result = malloc(reclen, M_LINKER, M_WAITOK);
1671 for (cpp = linker_ext_list; *cpp; cpp++) {
1672 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1673 namelen, name, *cpp);
1675 * Attempt to open the file, and return the path if
1676 * we succeed and it's a regular file.
1678 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
1680 error = vn_open(&nd, &flags, 0, NULL);
1682 vfslocked = NDHASGIANT(&nd);
1683 NDFREE(&nd, NDF_ONLY_PNBUF);
1684 type = nd.ni_vp->v_type;
1686 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1687 VOP_UNLOCK(nd.ni_vp, 0);
1688 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1689 VFS_UNLOCK_GIANT(vfslocked);
1694 free(result, M_LINKER);
1698 #define INT_ALIGN(base, ptr) ptr = \
1699 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1702 * Lookup KLD which contains requested module in the "linker.hints" file. If
1703 * version specification is available, then try to find the best KLD.
1704 * Otherwise just find the latest one.
1707 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1708 int modnamelen, struct mod_depend *verinfo)
1710 struct thread *td = curthread; /* XXX */
1711 struct ucred *cred = td ? td->td_ucred : NULL;
1712 struct nameidata nd;
1713 struct vattr vattr, mattr;
1714 u_char *hints = NULL;
1715 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1716 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1720 bestver = found = 0;
1722 sep = (path[pathlen - 1] != '/') ? "/" : "";
1723 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1725 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1726 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1729 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
1731 error = vn_open(&nd, &flags, 0, NULL);
1734 vfslocked = NDHASGIANT(&nd);
1735 NDFREE(&nd, NDF_ONLY_PNBUF);
1736 if (nd.ni_vp->v_type != VREG)
1739 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1743 * XXX: we need to limit this number to some reasonable value
1745 if (vattr.va_size > 100 * 1024) {
1746 printf("hints file too large %ld\n", (long)vattr.va_size);
1749 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1752 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1753 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1756 VOP_UNLOCK(nd.ni_vp, 0);
1757 vn_close(nd.ni_vp, FREAD, cred, td);
1758 VFS_UNLOCK_GIANT(vfslocked);
1761 printf("can't read %d\n", reclen);
1764 intp = (int *)hints;
1766 if (ival != LINKER_HINTS_VERSION) {
1767 printf("hints file version mismatch %d\n", ival);
1770 bufend = hints + vattr.va_size;
1771 recptr = (u_char *)intp;
1773 while (recptr < bufend && !found) {
1774 intp = (int *)recptr;
1781 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1784 INT_ALIGN(hints, cp);
1788 if (verinfo == NULL ||
1789 ival == verinfo->md_ver_preferred) {
1793 if (ival >= verinfo->md_ver_minimum &&
1794 ival <= verinfo->md_ver_maximum &&
1804 recptr += reclen + sizeof(int);
1807 * Finally check if KLD is in the place
1810 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1812 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1815 * KLD is newer than hints file. What we should do now?
1817 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1818 printf("warning: KLD '%s' is newer than the linker.hints"
1821 free(pathbuf, M_LINKER);
1823 free(hints, M_TEMP);
1824 if (nd.ni_vp != NULL) {
1825 VOP_UNLOCK(nd.ni_vp, 0);
1826 vn_close(nd.ni_vp, FREAD, cred, td);
1827 VFS_UNLOCK_GIANT(vfslocked);
1830 * If nothing found or hints is absent - fallback to the old
1831 * way by using "kldname[.ko]" as module name.
1833 if (!found && !bestver && result == NULL)
1834 result = linker_lookup_file(path, pathlen, modname,
1840 * Lookup KLD which contains requested module in the all directories.
1843 linker_search_module(const char *modname, int modnamelen,
1844 struct mod_depend *verinfo)
1846 char *cp, *ep, *result;
1849 * traverse the linker path
1851 for (cp = linker_path; *cp; cp = ep + 1) {
1852 /* find the end of this component */
1853 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1854 result = linker_hints_lookup(cp, ep - cp, modname,
1855 modnamelen, verinfo);
1865 * Search for module in all directories listed in the linker_path.
1868 linker_search_kld(const char *name)
1870 char *cp, *ep, *result;
1873 /* qualified at all? */
1874 if (index(name, '/'))
1875 return (linker_strdup(name));
1877 /* traverse the linker path */
1879 for (ep = linker_path; *ep; ep++) {
1881 /* find the end of this component */
1882 for (; *ep != 0 && *ep != ';'; ep++);
1883 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1891 linker_basename(const char *path)
1893 const char *filename;
1895 filename = rindex(path, '/');
1896 if (filename == NULL)
1905 * Inform hwpmc about the set of kernel modules currently loaded.
1908 linker_hwpmc_list_objects(void)
1911 struct pmckern_map_in *kobase;
1916 TAILQ_FOREACH(lf, &linker_files, link)
1919 /* Allocate nmappings + 1 entries. */
1920 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1921 M_LINKER, M_WAITOK | M_ZERO);
1923 TAILQ_FOREACH(lf, &linker_files, link) {
1925 /* Save the info for this linker file. */
1926 kobase[i].pm_file = lf->filename;
1927 kobase[i].pm_address = (uintptr_t)lf->address;
1932 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
1934 /* The last entry of the malloced area comprises of all zeros. */
1935 KASSERT(kobase[i].pm_file == NULL,
1936 ("linker_hwpmc_list_objects: last object not NULL"));
1938 return ((void *)kobase);
1943 * Find a file which contains given module and load it, if "parent" is not
1944 * NULL, register a reference to it.
1947 linker_load_module(const char *kldname, const char *modname,
1948 struct linker_file *parent, struct mod_depend *verinfo,
1949 struct linker_file **lfpp)
1951 linker_file_t lfdep;
1952 const char *filename;
1957 if (modname == NULL) {
1959 * We have to load KLD
1961 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1963 pathname = linker_search_kld(kldname);
1965 if (modlist_lookup2(modname, verinfo) != NULL)
1967 if (kldname != NULL)
1968 pathname = linker_strdup(kldname);
1969 else if (rootvnode == NULL)
1973 * Need to find a KLD with required module
1975 pathname = linker_search_module(modname,
1976 strlen(modname), verinfo);
1978 if (pathname == NULL)
1982 * Can't load more than one file with the same basename XXX:
1983 * Actually it should be possible to have multiple KLDs with
1984 * the same basename but different path because they can
1985 * provide different versions of the same modules.
1987 filename = linker_basename(pathname);
1988 if (linker_find_file_by_name(filename))
1991 error = linker_load_file(pathname, &lfdep);
1994 if (modname && verinfo &&
1995 modlist_lookup2(modname, verinfo) == NULL) {
1996 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2001 error = linker_file_add_dependency(parent, lfdep);
2008 free(pathname, M_LINKER);
2013 * This routine is responsible for finding dependencies of userland initiated
2014 * kldload(2)'s of files.
2017 linker_load_dependencies(linker_file_t lf)
2019 linker_file_t lfdep;
2020 struct mod_metadata **start, **stop, **mdp, **nmdp;
2021 struct mod_metadata *mp, *nmp;
2022 struct mod_depend *verinfo;
2024 const char *modname, *nmodname;
2025 int ver, error = 0, count;
2028 * All files are dependant on /kernel.
2031 if (linker_kernel_file) {
2032 linker_kernel_file->refs++;
2033 error = linker_file_add_dependency(lf, linker_kernel_file);
2037 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2040 for (mdp = start; mdp < stop; mdp++) {
2042 if (mp->md_type != MDT_VERSION)
2044 modname = mp->md_cval;
2045 ver = ((struct mod_version *)mp->md_data)->mv_version;
2046 mod = modlist_lookup(modname, ver);
2048 printf("interface %s.%d already present in the KLD"
2049 " '%s'!\n", modname, ver,
2050 mod->container->filename);
2055 for (mdp = start; mdp < stop; mdp++) {
2057 if (mp->md_type != MDT_DEPEND)
2059 modname = mp->md_cval;
2060 verinfo = mp->md_data;
2062 for (nmdp = start; nmdp < stop; nmdp++) {
2064 if (nmp->md_type != MDT_VERSION)
2066 nmodname = nmp->md_cval;
2067 if (strcmp(modname, nmodname) == 0)
2070 if (nmdp < stop)/* early exit, it's a self reference */
2072 mod = modlist_lookup2(modname, verinfo);
2073 if (mod) { /* woohoo, it's loaded already */
2074 lfdep = mod->container;
2076 error = linker_file_add_dependency(lf, lfdep);
2081 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2083 printf("KLD %s: depends on %s - not available or"
2084 " version mismatch\n", lf->filename, modname);
2091 linker_addmodules(lf, start, stop, 0);
2096 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2098 struct sysctl_req *req;
2101 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2105 * Export a nul-separated, double-nul-terminated list of all function names
2109 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2115 error = mac_kld_check_stat(req->td->td_ucred);
2119 error = sysctl_wire_old_buffer(req, 0);
2123 TAILQ_FOREACH(lf, &linker_files, link) {
2124 error = LINKER_EACH_FUNCTION_NAME(lf,
2125 sysctl_kern_function_list_iterate, req);
2132 return (SYSCTL_OUT(req, "", 1));
2135 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
2136 NULL, 0, sysctl_kern_function_list, "", "kernel function list");