2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997-2000 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include "opt_hwpmc_hooks.h"
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/sysproto.h>
41 #include <sys/sysent.h>
45 #include <sys/mutex.h>
47 #include <sys/module.h>
48 #include <sys/mount.h>
49 #include <sys/linker.h>
50 #include <sys/eventhandler.h>
51 #include <sys/fcntl.h>
53 #include <sys/libkern.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysctl.h>
65 #include <security/mac/mac_framework.h>
67 #include "linker_if.h"
70 #include <sys/pmckern.h>
75 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RWTUN,
76 &kld_debug, 0, "Set various levels of KLD debug");
79 /* These variables are used by kernel debuggers to enumerate loaded files. */
80 const int kld_off_address = offsetof(struct linker_file, address);
81 const int kld_off_filename = offsetof(struct linker_file, filename);
82 const int kld_off_pathname = offsetof(struct linker_file, pathname);
83 const int kld_off_next = offsetof(struct linker_file, link.tqe_next);
86 * static char *linker_search_path(const char *name, struct mod_depend
89 static const char *linker_basename(const char *path);
92 * Find a currently loaded file given its filename.
94 static linker_file_t linker_find_file_by_name(const char* _filename);
97 * Find a currently loaded file given its file id.
99 static linker_file_t linker_find_file_by_id(int _fileid);
101 /* Metadata from the static kernel */
102 SET_DECLARE(modmetadata_set, struct mod_metadata);
104 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
106 linker_file_t linker_kernel_file;
108 static struct sx kld_sx; /* kernel linker lock */
109 static u_int kld_busy;
110 static struct thread *kld_busy_owner;
113 * Load counter used by clients to determine if a linker file has been
114 * re-loaded. This counter is incremented for each file load.
118 static linker_class_list_t classes;
119 static linker_file_list_t linker_files;
120 static int next_file_id = 1;
121 static int linker_no_more_classes = 0;
123 #define LINKER_GET_NEXT_FILE_ID(a) do { \
124 linker_file_t lftmp; \
127 sx_assert(&kld_sx, SA_XLOCKED); \
129 TAILQ_FOREACH(lftmp, &linker_files, link) { \
130 if (next_file_id == lftmp->id) { \
135 (a) = next_file_id; \
138 /* XXX wrong name; we're looking at version provision tags here, not modules */
139 typedef TAILQ_HEAD(, modlist) modlisthead_t;
141 TAILQ_ENTRY(modlist) link; /* chain together all modules */
142 linker_file_t container;
146 typedef struct modlist *modlist_t;
147 static modlisthead_t found_modules;
149 static int linker_file_add_dependency(linker_file_t file,
151 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
152 const char* name, int deps);
153 static int linker_load_module(const char *kldname,
154 const char *modname, struct linker_file *parent,
155 const struct mod_depend *verinfo, struct linker_file **lfpp);
156 static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo);
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, NULL);
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 sx_assert(&kld_sx, SA_XLOCKED);
205 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
208 * Perform a bubble sort of the system initialization objects by
209 * their subsystem (primary key) and order (secondary key).
211 * Since some things care about execution order, this is the operation
212 * which ensures continued function.
214 for (sipp = start; sipp < stop; sipp++) {
215 for (xipp = sipp + 1; xipp < stop; xipp++) {
216 if ((*sipp)->subsystem < (*xipp)->subsystem ||
217 ((*sipp)->subsystem == (*xipp)->subsystem &&
218 (*sipp)->order <= (*xipp)->order))
227 * Traverse the (now) ordered list of system initialization tasks.
228 * Perform each task, and continue on to the next task.
232 for (sipp = start; sipp < stop; sipp++) {
233 if ((*sipp)->subsystem == SI_SUB_DUMMY)
234 continue; /* skip dummy task(s) */
237 (*((*sipp)->func)) ((*sipp)->udata);
244 linker_file_sysuninit(linker_file_t lf)
246 struct sysinit **start, **stop, **sipp, **xipp, *save;
248 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
251 sx_assert(&kld_sx, SA_XLOCKED);
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.
282 for (sipp = start; sipp < stop; sipp++) {
283 if ((*sipp)->subsystem == SI_SUB_DUMMY)
284 continue; /* skip dummy task(s) */
287 (*((*sipp)->func)) ((*sipp)->udata);
294 linker_file_register_sysctls(linker_file_t lf, bool enable)
296 struct sysctl_oid **start, **stop, **oidp;
299 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
302 sx_assert(&kld_sx, SA_XLOCKED);
304 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
309 for (oidp = start; oidp < stop; oidp++) {
311 sysctl_register_oid(*oidp);
313 sysctl_register_disabled_oid(*oidp);
320 linker_file_enable_sysctls(linker_file_t lf)
322 struct sysctl_oid **start, **stop, **oidp;
325 ("linker_file_enable_sysctls: enable SYSCTLs for %s\n",
328 sx_assert(&kld_sx, SA_XLOCKED);
330 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
335 for (oidp = start; oidp < stop; oidp++)
336 sysctl_enable_oid(*oidp);
342 linker_file_unregister_sysctls(linker_file_t lf)
344 struct sysctl_oid **start, **stop, **oidp;
346 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
347 " for %s\n", lf->filename));
349 sx_assert(&kld_sx, SA_XLOCKED);
351 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
356 for (oidp = start; oidp < stop; oidp++)
357 sysctl_unregister_oid(*oidp);
363 linker_file_register_modules(linker_file_t lf)
365 struct mod_metadata **start, **stop, **mdp;
366 const moduledata_t *moddata;
367 int first_error, error;
369 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
370 " in %s\n", lf->filename));
372 sx_assert(&kld_sx, SA_XLOCKED);
374 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL) != 0) {
376 * This fallback should be unnecessary, but if we get booted
377 * from boot2 instead of loader and we are missing our
378 * metadata then we have to try the best we can.
380 if (lf == linker_kernel_file) {
381 start = SET_BEGIN(modmetadata_set);
382 stop = SET_LIMIT(modmetadata_set);
387 for (mdp = start; mdp < stop; mdp++) {
388 if ((*mdp)->md_type != MDT_MODULE)
390 moddata = (*mdp)->md_data;
391 KLD_DPF(FILE, ("Registering module %s in %s\n",
392 moddata->name, lf->filename));
393 error = module_register(moddata, lf);
395 printf("Module %s failed to register: %d\n",
396 moddata->name, error);
397 if (first_error == 0)
401 return (first_error);
405 linker_init_kernel_modules(void)
409 linker_file_register_modules(linker_kernel_file);
413 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
417 linker_load_file(const char *filename, linker_file_t *result)
421 int foundfile, error, modules;
423 /* Refuse to load modules if securelevel raised */
424 if (prison0.pr_securelevel > 0)
427 sx_assert(&kld_sx, SA_XLOCKED);
428 lf = linker_find_file_by_name(filename);
430 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
431 " incrementing refs\n", filename));
440 * We do not need to protect (lock) classes here because there is
441 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
442 * and there is no class deregistration mechanism at this time.
444 TAILQ_FOREACH(lc, &classes, link) {
445 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
447 error = LINKER_LOAD_FILE(lc, filename, &lf);
449 * If we got something other than ENOENT, then it exists but
450 * we cannot load it for some other reason.
455 error = linker_file_register_modules(lf);
456 if (error == EEXIST) {
457 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
460 modules = !TAILQ_EMPTY(&lf->modules);
461 linker_file_register_sysctls(lf, false);
462 linker_file_sysinit(lf);
463 lf->flags |= LINKER_FILE_LINKED;
466 * If all of the modules in this file failed
467 * to load, unload the file and return an
470 if (modules && TAILQ_EMPTY(&lf->modules)) {
471 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
474 linker_file_enable_sysctls(lf);
475 EVENTHANDLER_INVOKE(kld_load, lf);
481 * Less than ideal, but tells the user whether it failed to load or
482 * the module was not found.
486 * If the file type has not been recognized by the last try
487 * printout a message before to fail.
490 printf("%s: %s - unsupported file type\n",
494 * Format not recognized or otherwise unloadable.
495 * When loading a module that is statically built into
496 * the kernel EEXIST percolates back up as the return
497 * value. Preserve this so that apps like sysinstall
498 * can recognize this special case and not post bogus
504 error = ENOENT; /* Nothing found */
509 linker_reference_module(const char *modname, struct mod_depend *verinfo,
510 linker_file_t *result)
516 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
517 *result = mod->container;
523 error = linker_load_module(NULL, modname, NULL, verinfo, result);
529 linker_release_module(const char *modname, struct mod_depend *verinfo,
537 KASSERT(modname != NULL,
538 ("linker_release_module: no file or name"));
539 mod = modlist_lookup2(modname, verinfo);
546 KASSERT(modname == NULL && verinfo == NULL,
547 ("linker_release_module: both file and name"));
548 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
554 linker_find_file_by_name(const char *filename)
559 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
560 sprintf(koname, "%s.ko", filename);
562 sx_assert(&kld_sx, SA_XLOCKED);
563 TAILQ_FOREACH(lf, &linker_files, link) {
564 if (strcmp(lf->filename, koname) == 0)
566 if (strcmp(lf->filename, filename) == 0)
569 free(koname, M_LINKER);
574 linker_find_file_by_id(int fileid)
578 sx_assert(&kld_sx, SA_XLOCKED);
579 TAILQ_FOREACH(lf, &linker_files, link)
580 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
586 linker_file_foreach(linker_predicate_t *predicate, void *context)
592 TAILQ_FOREACH(lf, &linker_files, link) {
593 retval = predicate(lf, context);
602 linker_make_file(const char *pathname, linker_class_t lc)
605 const char *filename;
608 sx_assert(&kld_sx, SA_XLOCKED);
609 filename = linker_basename(pathname);
611 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
612 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
622 lf->filename = strdup(filename, M_LINKER);
623 lf->pathname = strdup(pathname, M_LINKER);
624 LINKER_GET_NEXT_FILE_ID(lf->id);
627 lf->loadcnt = ++loadcnt;
632 STAILQ_INIT(&lf->common);
633 TAILQ_INIT(&lf->modules);
634 TAILQ_INSERT_TAIL(&linker_files, lf, link);
639 linker_file_unload(linker_file_t file, int flags)
642 modlist_t ml, nextml;
643 struct common_symbol *cp;
646 /* Refuse to unload modules if securelevel raised. */
647 if (prison0.pr_securelevel > 0)
650 sx_assert(&kld_sx, SA_XLOCKED);
651 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
653 /* Easy case of just dropping a reference. */
654 if (file->refs > 1) {
659 /* Give eventhandlers a chance to prevent the unload. */
661 EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
665 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
666 " informing modules\n"));
669 * Quiesce all the modules to give them a chance to veto the unload.
672 for (mod = TAILQ_FIRST(&file->modules); mod;
673 mod = module_getfnext(mod)) {
674 error = module_quiesce(mod);
675 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
676 KLD_DPF(FILE, ("linker_file_unload: module %s"
677 " vetoed unload\n", module_getname(mod)));
679 * XXX: Do we need to tell all the quiesced modules
680 * that they can resume work now via a new module
690 * Inform any modules associated with this file that they are
694 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
695 next = module_getfnext(mod);
699 * Give the module a chance to veto the unload.
701 if ((error = module_unload(mod)) != 0) {
704 KLD_DPF(FILE, ("linker_file_unload: module %s"
705 " failed unload\n", module_getname(mod)));
715 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
716 if (ml->container == file) {
717 TAILQ_REMOVE(&found_modules, ml, link);
723 * Don't try to run SYSUNINITs if we are unloaded due to a
726 if (file->flags & LINKER_FILE_LINKED) {
727 file->flags &= ~LINKER_FILE_LINKED;
728 linker_file_unregister_sysctls(file);
729 linker_file_sysuninit(file);
731 TAILQ_REMOVE(&linker_files, file, link);
734 for (i = 0; i < file->ndeps; i++)
735 linker_file_unload(file->deps[i], flags);
736 free(file->deps, M_LINKER);
739 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
740 STAILQ_REMOVE_HEAD(&file->common, link);
746 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
749 if (file->filename) {
750 free(file->filename, M_LINKER);
751 file->filename = NULL;
753 if (file->pathname) {
754 free(file->pathname, M_LINKER);
755 file->pathname = NULL;
757 kobj_delete((kobj_t) file, M_LINKER);
762 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
764 return (LINKER_CTF_GET(file, lc));
768 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
770 linker_file_t *newdeps;
772 sx_assert(&kld_sx, SA_XLOCKED);
773 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
774 M_LINKER, M_WAITOK | M_ZERO);
775 file->deps[file->ndeps] = dep;
777 KLD_DPF(FILE, ("linker_file_add_dependency:"
778 " adding %s as dependency for %s\n",
779 dep->filename, file->filename));
784 * Locate a linker set and its contents. This is a helper function to avoid
785 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
786 * This function is used in this file so we can avoid having lots of (void **)
790 linker_file_lookup_set(linker_file_t file, const char *name,
791 void *firstp, void *lastp, int *countp)
794 sx_assert(&kld_sx, SA_LOCKED);
795 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
799 * List all functions in a file.
802 linker_file_function_listall(linker_file_t lf,
803 linker_function_nameval_callback_t callback_func, void *arg)
805 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
809 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
814 locked = sx_xlocked(&kld_sx);
817 sym = linker_file_lookup_symbol_internal(file, name, deps);
824 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
828 linker_symval_t symval;
830 size_t common_size = 0;
833 sx_assert(&kld_sx, SA_XLOCKED);
834 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
837 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
838 LINKER_SYMBOL_VALUES(file, sym, &symval);
839 if (symval.value == 0)
841 * For commons, first look them up in the
842 * dependencies and only allocate space if not found
845 common_size = symval.size;
847 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
848 ".value=%p\n", symval.value));
849 return (symval.value);
853 for (i = 0; i < file->ndeps; i++) {
854 address = linker_file_lookup_symbol_internal(
855 file->deps[i], name, 0);
857 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
858 " deps value=%p\n", address));
863 if (common_size > 0) {
865 * This is a common symbol which was not found in the
866 * dependencies. We maintain a simple common symbol table in
869 struct common_symbol *cp;
871 STAILQ_FOREACH(cp, &file->common, link) {
872 if (strcmp(cp->name, name) == 0) {
873 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
874 " old common value=%p\n", cp->address));
875 return (cp->address);
879 * Round the symbol size up to align.
881 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
882 cp = malloc(sizeof(struct common_symbol)
883 + common_size + strlen(name) + 1, M_LINKER,
885 cp->address = (caddr_t)(cp + 1);
886 cp->name = cp->address + common_size;
887 strcpy(cp->name, name);
888 bzero(cp->address, common_size);
889 STAILQ_INSERT_TAIL(&file->common, cp, link);
891 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
892 " value=%p\n", cp->address));
893 return (cp->address);
895 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
900 * Both DDB and stack(9) rely on the kernel linker to provide forward and
901 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
902 * do this in a lockfree manner. We provide a set of internal helper
903 * routines to perform these operations without locks, and then wrappers that
906 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
910 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
914 TAILQ_FOREACH(lf, &linker_files, link) {
915 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
923 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
926 c_linker_sym_t best, es;
927 u_long diff, bestdiff, off;
930 off = (uintptr_t)value;
932 TAILQ_FOREACH(lf, &linker_files, link) {
933 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
935 if (es != 0 && diff < bestdiff) {
954 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
958 TAILQ_FOREACH(lf, &linker_files, link) {
959 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
966 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
969 linker_symval_t symval;
974 error = linker_debug_search_symbol(value, &sym, offset);
977 error = linker_debug_symbol_values(sym, &symval);
980 strlcpy(buf, symval.name, buflen);
985 * DDB Helpers. DDB has to look across multiple files with their own symbol
986 * tables and string tables.
988 * Note that we do not obey list locking protocols here. We really don't need
989 * DDB to hang because somebody's got the lock held. We'll take the chance
990 * that the files list is inconsistent instead.
994 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
997 return (linker_debug_lookup(symstr, sym));
1002 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
1005 return (linker_debug_search_symbol(value, sym, diffp));
1009 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
1012 return (linker_debug_symbol_values(sym, symval));
1016 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1020 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
1024 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
1025 * obey locking protocols, and offer a significantly less complex interface.
1028 linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen,
1029 long *offset, int flags)
1033 KASSERT((flags & (M_NOWAIT | M_WAITOK)) != 0 &&
1034 (flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
1035 ("%s: bad flags: 0x%x", __func__, flags));
1037 if (flags & M_NOWAIT) {
1038 if (!sx_try_slock(&kld_sx))
1039 return (EWOULDBLOCK);
1043 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
1044 sx_sunlock(&kld_sx);
1049 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1053 return (linker_search_symbol_name_flags(value, buf, buflen, offset,
1058 linker_kldload_busy(int flags)
1062 MPASS((flags & ~(LINKER_UB_UNLOCK | LINKER_UB_LOCKED |
1063 LINKER_UB_PCATCH)) == 0);
1064 if ((flags & LINKER_UB_LOCKED) != 0)
1065 sx_assert(&kld_sx, SA_XLOCKED);
1067 if ((flags & LINKER_UB_LOCKED) == 0)
1069 while (kld_busy > 0) {
1070 if (kld_busy_owner == curthread)
1072 error = sx_sleep(&kld_busy, &kld_sx,
1073 (flags & LINKER_UB_PCATCH) != 0 ? PCATCH : 0,
1076 if ((flags & LINKER_UB_UNLOCK) != 0)
1077 sx_xunlock(&kld_sx);
1082 kld_busy_owner = curthread;
1083 if ((flags & LINKER_UB_UNLOCK) != 0)
1084 sx_xunlock(&kld_sx);
1089 linker_kldload_unbusy(int flags)
1091 MPASS((flags & ~LINKER_UB_LOCKED) == 0);
1092 if ((flags & LINKER_UB_LOCKED) != 0)
1093 sx_assert(&kld_sx, SA_XLOCKED);
1095 if ((flags & LINKER_UB_LOCKED) == 0)
1097 MPASS(kld_busy > 0);
1098 if (kld_busy_owner != curthread)
1099 panic("linker_kldload_unbusy done by not owning thread %p",
1102 if (kld_busy == 0) {
1103 kld_busy_owner = NULL;
1106 sx_xunlock(&kld_sx);
1113 kern_kldload(struct thread *td, const char *file, int *fileid)
1115 const char *kldname, *modname;
1119 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1122 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1126 * If file does not contain a qualified name or any dot in it
1127 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1130 if (strchr(file, '/') || strchr(file, '.')) {
1138 error = linker_kldload_busy(LINKER_UB_PCATCH);
1140 sx_xunlock(&kld_sx);
1145 * It is possible that kldloaded module will attach a new ifnet,
1146 * so vnet context must be set when this ocurs.
1148 CURVNET_SET(TD_TO_VNET(td));
1150 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1158 linker_kldload_unbusy(LINKER_UB_LOCKED);
1163 sys_kldload(struct thread *td, struct kldload_args *uap)
1165 char *pathname = NULL;
1168 td->td_retval[0] = -1;
1170 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1171 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1173 error = kern_kldload(td, pathname, &fileid);
1175 td->td_retval[0] = fileid;
1177 free(pathname, M_TEMP);
1182 kern_kldunload(struct thread *td, int fileid, int flags)
1187 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1190 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1193 error = linker_kldload_busy(LINKER_UB_PCATCH);
1195 sx_xunlock(&kld_sx);
1199 CURVNET_SET(TD_TO_VNET(td));
1200 lf = linker_find_file_by_id(fileid);
1202 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1204 if (lf->userrefs == 0) {
1206 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1208 printf("kldunload: attempt to unload file that was"
1209 " loaded by the kernel\n");
1213 error = linker_file_unload(lf, flags);
1220 linker_kldload_unbusy(LINKER_UB_LOCKED);
1225 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1228 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1232 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1235 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1236 uap->flags != LINKER_UNLOAD_FORCE)
1238 return (kern_kldunload(td, uap->fileid, uap->flags));
1242 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1245 const char *filename;
1250 error = mac_kld_check_stat(td->td_ucred);
1255 td->td_retval[0] = -1;
1257 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1258 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1261 filename = linker_basename(pathname);
1263 lf = linker_find_file_by_name(filename);
1265 td->td_retval[0] = lf->id;
1268 sx_xunlock(&kld_sx);
1270 free(pathname, M_TEMP);
1275 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1281 error = mac_kld_check_stat(td->td_ucred);
1287 if (uap->fileid == 0)
1288 lf = TAILQ_FIRST(&linker_files);
1290 lf = linker_find_file_by_id(uap->fileid);
1295 lf = TAILQ_NEXT(lf, link);
1298 /* Skip partially loaded files. */
1299 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1300 lf = TAILQ_NEXT(lf, link);
1303 td->td_retval[0] = lf->id;
1305 td->td_retval[0] = 0;
1307 sx_xunlock(&kld_sx);
1312 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1314 struct kld_file_stat *stat;
1318 * Check the version of the user's structure.
1320 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1323 if (version != sizeof(struct kld_file_stat_1) &&
1324 version != sizeof(struct kld_file_stat))
1327 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
1328 error = kern_kldstat(td, uap->fileid, stat);
1330 error = copyout(stat, uap->stat, version);
1336 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1343 error = mac_kld_check_stat(td->td_ucred);
1349 lf = linker_find_file_by_id(fileid);
1351 sx_xunlock(&kld_sx);
1355 /* Version 1 fields: */
1356 namelen = strlen(lf->filename) + 1;
1357 if (namelen > sizeof(stat->name))
1358 namelen = sizeof(stat->name);
1359 bcopy(lf->filename, &stat->name[0], namelen);
1360 stat->refs = lf->refs;
1362 stat->address = lf->address;
1363 stat->size = lf->size;
1364 /* Version 2 fields: */
1365 namelen = strlen(lf->pathname) + 1;
1366 if (namelen > sizeof(stat->pathname))
1367 namelen = sizeof(stat->pathname);
1368 bcopy(lf->pathname, &stat->pathname[0], namelen);
1369 sx_xunlock(&kld_sx);
1371 td->td_retval[0] = 0;
1376 DB_COMMAND(kldstat, db_kldstat)
1380 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
1381 db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
1382 #undef POINTER_WIDTH
1383 TAILQ_FOREACH(lf, &linker_files, link) {
1386 db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs,
1387 lf->address, lf->size, lf->filename);
1393 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1400 error = mac_kld_check_stat(td->td_ucred);
1406 lf = linker_find_file_by_id(uap->fileid);
1409 mp = TAILQ_FIRST(&lf->modules);
1411 td->td_retval[0] = module_getid(mp);
1413 td->td_retval[0] = 0;
1417 sx_xunlock(&kld_sx);
1422 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1424 char *symstr = NULL;
1426 linker_symval_t symval;
1428 struct kld_sym_lookup lookup;
1432 error = mac_kld_check_stat(td->td_ucred);
1437 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1439 if (lookup.version != sizeof(lookup) ||
1440 uap->cmd != KLDSYM_LOOKUP)
1442 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1443 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1446 if (uap->fileid != 0) {
1447 lf = linker_find_file_by_id(uap->fileid);
1450 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1451 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1452 lookup.symvalue = (uintptr_t) symval.value;
1453 lookup.symsize = symval.size;
1454 error = copyout(&lookup, uap->data, sizeof(lookup));
1458 TAILQ_FOREACH(lf, &linker_files, link) {
1459 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1460 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1461 lookup.symvalue = (uintptr_t)symval.value;
1462 lookup.symsize = symval.size;
1463 error = copyout(&lookup, uap->data,
1471 sx_xunlock(&kld_sx);
1473 free(symstr, M_TEMP);
1478 * Preloaded module support
1482 modlist_lookup(const char *name, int ver)
1486 TAILQ_FOREACH(mod, &found_modules, link) {
1487 if (strcmp(mod->name, name) == 0 &&
1488 (ver == 0 || mod->version == ver))
1495 modlist_lookup2(const char *name, const struct mod_depend *verinfo)
1497 modlist_t mod, bestmod;
1500 if (verinfo == NULL)
1501 return (modlist_lookup(name, 0));
1503 TAILQ_FOREACH(mod, &found_modules, link) {
1504 if (strcmp(mod->name, name) != 0)
1507 if (ver == verinfo->md_ver_preferred)
1509 if (ver >= verinfo->md_ver_minimum &&
1510 ver <= verinfo->md_ver_maximum &&
1511 (bestmod == NULL || ver > bestmod->version))
1518 modlist_newmodule(const char *modname, int version, linker_file_t container)
1522 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1524 panic("no memory for module list");
1525 mod->container = container;
1526 mod->name = modname;
1527 mod->version = version;
1528 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1533 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1534 struct mod_metadata **stop, int preload)
1536 struct mod_metadata *mp, **mdp;
1537 const char *modname;
1540 for (mdp = start; mdp < stop; mdp++) {
1542 if (mp->md_type != MDT_VERSION)
1544 modname = mp->md_cval;
1545 ver = ((const struct mod_version *)mp->md_data)->mv_version;
1546 if (modlist_lookup(modname, ver) != NULL) {
1547 printf("module %s already present!\n", modname);
1548 /* XXX what can we do? this is a build error. :-( */
1551 modlist_newmodule(modname, ver, lf);
1556 linker_preload(void *arg)
1559 const char *modname, *nmodname;
1561 linker_file_t lf, nlf;
1564 linker_file_list_t loaded_files;
1565 linker_file_list_t depended_files;
1566 struct mod_metadata *mp, *nmp;
1567 struct mod_metadata **start, **stop, **mdp, **nmdp;
1568 const struct mod_depend *verinfo;
1572 struct sysinit **si_start, **si_stop;
1574 TAILQ_INIT(&loaded_files);
1575 TAILQ_INIT(&depended_files);
1576 TAILQ_INIT(&found_modules);
1581 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1582 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1583 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1584 if (modname == NULL) {
1585 printf("Preloaded module at %p does not have a"
1586 " name!\n", modptr);
1589 if (modtype == NULL) {
1590 printf("Preloaded module at %p does not have a type!\n",
1595 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1598 TAILQ_FOREACH(lc, &classes, link) {
1599 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1605 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1609 * First get a list of stuff in the kernel.
1611 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1613 linker_addmodules(linker_kernel_file, start, stop, 1);
1616 * This is a once-off kinky bubble sort to resolve relocation
1617 * dependency requirements.
1620 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1621 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1624 * First, look to see if we would successfully link with this
1627 resolves = 1; /* unless we know otherwise */
1629 for (mdp = start; mdp < stop; mdp++) {
1631 if (mp->md_type != MDT_DEPEND)
1633 modname = mp->md_cval;
1634 verinfo = mp->md_data;
1635 for (nmdp = start; nmdp < stop; nmdp++) {
1637 if (nmp->md_type != MDT_VERSION)
1639 nmodname = nmp->md_cval;
1640 if (strcmp(modname, nmodname) == 0)
1643 if (nmdp < stop) /* it's a self reference */
1647 * ok, the module isn't here yet, we
1650 if (modlist_lookup2(modname, verinfo) == NULL)
1655 * OK, if we found our modules, we can link. So, "provide"
1656 * the modules inside and add it to the end of the link order
1661 for (mdp = start; mdp < stop; mdp++) {
1663 if (mp->md_type != MDT_VERSION)
1665 modname = mp->md_cval;
1666 nver = ((const struct mod_version *)
1667 mp->md_data)->mv_version;
1668 if (modlist_lookup(modname,
1670 printf("module %s already"
1671 " present!\n", modname);
1672 TAILQ_REMOVE(&loaded_files,
1674 linker_file_unload(lf,
1675 LINKER_UNLOAD_FORCE);
1676 /* we changed tailq next ptr */
1679 modlist_newmodule(modname, nver, lf);
1682 TAILQ_REMOVE(&loaded_files, lf, loaded);
1683 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1685 * Since we provided modules, we need to restart the
1686 * sort so that the previous files that depend on us
1687 * have a chance. Also, we've busted the tailq next
1688 * pointer with the REMOVE.
1695 * At this point, we check to see what could not be resolved..
1697 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1698 TAILQ_REMOVE(&loaded_files, lf, loaded);
1699 printf("KLD file %s is missing dependencies\n", lf->filename);
1700 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1704 * We made it. Finish off the linking in the order we determined.
1706 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1707 if (linker_kernel_file) {
1708 linker_kernel_file->refs++;
1709 error = linker_file_add_dependency(lf,
1710 linker_kernel_file);
1712 panic("cannot add dependency");
1714 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1717 for (mdp = start; mdp < stop; mdp++) {
1719 if (mp->md_type != MDT_DEPEND)
1721 modname = mp->md_cval;
1722 verinfo = mp->md_data;
1723 mod = modlist_lookup2(modname, verinfo);
1725 printf("KLD file %s - cannot find "
1726 "dependency \"%s\"\n",
1727 lf->filename, modname);
1730 /* Don't count self-dependencies */
1731 if (lf == mod->container)
1733 mod->container->refs++;
1734 error = linker_file_add_dependency(lf,
1737 panic("cannot add dependency");
1741 * Now do relocation etc using the symbol search paths
1742 * established by the dependencies
1744 error = LINKER_LINK_PRELOAD_FINISH(lf);
1746 printf("KLD file %s - could not finalize loading\n",
1750 linker_file_register_modules(lf);
1751 if (!TAILQ_EMPTY(&lf->modules))
1752 lf->flags |= LINKER_FILE_MODULES;
1753 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1754 &si_stop, NULL) == 0)
1755 sysinit_add(si_start, si_stop);
1756 linker_file_register_sysctls(lf, true);
1757 lf->flags |= LINKER_FILE_LINKED;
1760 TAILQ_REMOVE(&depended_files, lf, loaded);
1761 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1763 sx_xunlock(&kld_sx);
1764 /* woohoo! we made it! */
1767 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, NULL);
1770 * Handle preload files that failed to load any modules.
1773 linker_preload_finish(void *arg)
1775 linker_file_t lf, nlf;
1778 TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) {
1780 * If all of the modules in this file failed to load, unload
1781 * the file and return an error of ENOEXEC. (Parity with
1782 * linker_load_file.)
1784 if ((lf->flags & LINKER_FILE_MODULES) != 0 &&
1785 TAILQ_EMPTY(&lf->modules)) {
1786 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1790 lf->flags &= ~LINKER_FILE_MODULES;
1791 lf->userrefs++; /* so we can (try to) kldunload it */
1793 sx_xunlock(&kld_sx);
1797 * Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be
1798 * scheduled at any subsystem and order, so run this as late as possible. init
1799 * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that.
1801 SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE,
1802 linker_preload_finish, NULL);
1805 * Search for a not-loaded module by name.
1807 * Modules may be found in the following locations:
1809 * - preloaded (result is just the module name) - on disk (result is full path
1812 * If the module name is qualified in any way (contains path, etc.) the we
1813 * simply return a copy of it.
1815 * The search path can be manipulated via sysctl. Note that we use the ';'
1816 * character as a separator to be consistent with the bootloader.
1819 static char linker_hintfile[] = "linker.hints";
1820 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1822 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path,
1823 sizeof(linker_path), "module load search path");
1825 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1827 static const char * const linker_ext_list[] = {
1834 * Check if file actually exists either with or without extension listed in
1835 * the linker_ext_list. (probably should be generic for the rest of the
1839 linker_lookup_file(const char *path, int pathlen, const char *name,
1840 int namelen, struct vattr *vap)
1842 struct nameidata nd;
1843 struct thread *td = curthread; /* XXX */
1844 const char * const *cpp, *sep;
1846 int error, len, extlen, reclen, flags;
1850 for (cpp = linker_ext_list; *cpp; cpp++) {
1855 extlen++; /* trailing '\0' */
1856 sep = (path[pathlen - 1] != '/') ? "/" : "";
1858 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1859 result = malloc(reclen, M_LINKER, M_WAITOK);
1860 for (cpp = linker_ext_list; *cpp; cpp++) {
1861 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1862 namelen, name, *cpp);
1864 * Attempt to open the file, and return the path if
1865 * we succeed and it's a regular file.
1867 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1869 error = vn_open(&nd, &flags, 0, NULL);
1871 NDFREE(&nd, NDF_ONLY_PNBUF);
1872 type = nd.ni_vp->v_type;
1874 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1875 VOP_UNLOCK(nd.ni_vp);
1876 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1881 free(result, M_LINKER);
1885 #define INT_ALIGN(base, ptr) ptr = \
1886 (base) + roundup2((ptr) - (base), sizeof(int))
1889 * Lookup KLD which contains requested module in the "linker.hints" file. If
1890 * version specification is available, then try to find the best KLD.
1891 * Otherwise just find the latest one.
1894 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1895 int modnamelen, const struct mod_depend *verinfo)
1897 struct thread *td = curthread; /* XXX */
1898 struct ucred *cred = td ? td->td_ucred : NULL;
1899 struct nameidata nd;
1900 struct vattr vattr, mattr;
1901 const char *best, *sep;
1902 u_char *hints = NULL;
1903 u_char *cp, *recptr, *bufend, *result, *pathbuf;
1904 int error, ival, bestver, *intp, found, flags, clen, blen;
1908 bestver = found = 0;
1910 sep = (path[pathlen - 1] != '/') ? "/" : "";
1911 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1913 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1914 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1917 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1919 error = vn_open(&nd, &flags, 0, NULL);
1922 NDFREE(&nd, NDF_ONLY_PNBUF);
1923 if (nd.ni_vp->v_type != VREG)
1926 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1930 * XXX: we need to limit this number to some reasonable value
1932 if (vattr.va_size > LINKER_HINTS_MAX) {
1933 printf("linker.hints file too large %ld\n", (long)vattr.va_size);
1936 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1937 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1938 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1941 VOP_UNLOCK(nd.ni_vp);
1942 vn_close(nd.ni_vp, FREAD, cred, td);
1945 printf("can't read %zd\n", reclen);
1948 intp = (int *)hints;
1950 if (ival != LINKER_HINTS_VERSION) {
1951 printf("linker.hints file version mismatch %d\n", ival);
1954 bufend = hints + vattr.va_size;
1955 recptr = (u_char *)intp;
1957 while (recptr < bufend && !found) {
1958 intp = (int *)recptr;
1965 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1968 INT_ALIGN(hints, cp);
1972 if (verinfo == NULL ||
1973 ival == verinfo->md_ver_preferred) {
1977 if (ival >= verinfo->md_ver_minimum &&
1978 ival <= verinfo->md_ver_maximum &&
1988 recptr += reclen + sizeof(int);
1991 * Finally check if KLD is in the place
1994 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1996 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1999 * KLD is newer than hints file. What we should do now?
2001 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
2002 printf("warning: KLD '%s' is newer than the linker.hints"
2005 free(pathbuf, M_LINKER);
2007 free(hints, M_TEMP);
2008 if (nd.ni_vp != NULL) {
2009 VOP_UNLOCK(nd.ni_vp);
2010 vn_close(nd.ni_vp, FREAD, cred, td);
2013 * If nothing found or hints is absent - fallback to the old
2014 * way by using "kldname[.ko]" as module name.
2016 if (!found && !bestver && result == NULL)
2017 result = linker_lookup_file(path, pathlen, modname,
2023 * Lookup KLD which contains requested module in the all directories.
2026 linker_search_module(const char *modname, int modnamelen,
2027 const struct mod_depend *verinfo)
2029 char *cp, *ep, *result;
2032 * traverse the linker path
2034 for (cp = linker_path; *cp; cp = ep + 1) {
2035 /* find the end of this component */
2036 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
2037 result = linker_hints_lookup(cp, ep - cp, modname,
2038 modnamelen, verinfo);
2048 * Search for module in all directories listed in the linker_path.
2051 linker_search_kld(const char *name)
2053 char *cp, *ep, *result;
2056 /* qualified at all? */
2057 if (strchr(name, '/'))
2058 return (strdup(name, M_LINKER));
2060 /* traverse the linker path */
2062 for (ep = linker_path; *ep; ep++) {
2064 /* find the end of this component */
2065 for (; *ep != 0 && *ep != ';'; ep++);
2066 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
2074 linker_basename(const char *path)
2076 const char *filename;
2078 filename = strrchr(path, '/');
2079 if (filename == NULL)
2088 * Inform hwpmc about the set of kernel modules currently loaded.
2091 linker_hwpmc_list_objects(void)
2094 struct pmckern_map_in *kobase;
2099 TAILQ_FOREACH(lf, &linker_files, link)
2102 /* Allocate nmappings + 1 entries. */
2103 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
2104 M_LINKER, M_WAITOK | M_ZERO);
2106 TAILQ_FOREACH(lf, &linker_files, link) {
2107 /* Save the info for this linker file. */
2108 kobase[i].pm_file = lf->filename;
2109 kobase[i].pm_address = (uintptr_t)lf->address;
2112 sx_sunlock(&kld_sx);
2114 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
2116 /* The last entry of the malloced area comprises of all zeros. */
2117 KASSERT(kobase[i].pm_file == NULL,
2118 ("linker_hwpmc_list_objects: last object not NULL"));
2120 return ((void *)kobase);
2124 /* check if root file system is not mounted */
2126 linker_root_mounted(void)
2131 if (rootvnode == NULL)
2134 pwd = pwd_hold(curthread);
2135 ret = pwd->pwd_rdir != NULL;
2141 * Find a file which contains given module and load it, if "parent" is not
2142 * NULL, register a reference to it.
2145 linker_load_module(const char *kldname, const char *modname,
2146 struct linker_file *parent, const struct mod_depend *verinfo,
2147 struct linker_file **lfpp)
2149 linker_file_t lfdep;
2150 const char *filename;
2154 sx_assert(&kld_sx, SA_XLOCKED);
2155 if (modname == NULL) {
2157 * We have to load KLD
2159 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2161 if (!linker_root_mounted())
2163 pathname = linker_search_kld(kldname);
2165 if (modlist_lookup2(modname, verinfo) != NULL)
2167 if (!linker_root_mounted())
2169 if (kldname != NULL)
2170 pathname = strdup(kldname, M_LINKER);
2173 * Need to find a KLD with required module
2175 pathname = linker_search_module(modname,
2176 strlen(modname), verinfo);
2178 if (pathname == NULL)
2182 * Can't load more than one file with the same basename XXX:
2183 * Actually it should be possible to have multiple KLDs with
2184 * the same basename but different path because they can
2185 * provide different versions of the same modules.
2187 filename = linker_basename(pathname);
2188 if (linker_find_file_by_name(filename))
2191 error = linker_load_file(pathname, &lfdep);
2194 if (modname && verinfo &&
2195 modlist_lookup2(modname, verinfo) == NULL) {
2196 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2201 error = linker_file_add_dependency(parent, lfdep);
2208 free(pathname, M_LINKER);
2213 * This routine is responsible for finding dependencies of userland initiated
2214 * kldload(2)'s of files.
2217 linker_load_dependencies(linker_file_t lf)
2219 linker_file_t lfdep;
2220 struct mod_metadata **start, **stop, **mdp, **nmdp;
2221 struct mod_metadata *mp, *nmp;
2222 const struct mod_depend *verinfo;
2224 const char *modname, *nmodname;
2228 * All files are dependent on /kernel.
2230 sx_assert(&kld_sx, SA_XLOCKED);
2231 if (linker_kernel_file) {
2232 linker_kernel_file->refs++;
2233 error = linker_file_add_dependency(lf, linker_kernel_file);
2237 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2240 for (mdp = start; mdp < stop; mdp++) {
2242 if (mp->md_type != MDT_VERSION)
2244 modname = mp->md_cval;
2245 ver = ((const struct mod_version *)mp->md_data)->mv_version;
2246 mod = modlist_lookup(modname, ver);
2248 printf("interface %s.%d already present in the KLD"
2249 " '%s'!\n", modname, ver,
2250 mod->container->filename);
2255 for (mdp = start; mdp < stop; mdp++) {
2257 if (mp->md_type != MDT_DEPEND)
2259 modname = mp->md_cval;
2260 verinfo = mp->md_data;
2262 for (nmdp = start; nmdp < stop; nmdp++) {
2264 if (nmp->md_type != MDT_VERSION)
2266 nmodname = nmp->md_cval;
2267 if (strcmp(modname, nmodname) == 0)
2270 if (nmdp < stop)/* early exit, it's a self reference */
2272 mod = modlist_lookup2(modname, verinfo);
2273 if (mod) { /* woohoo, it's loaded already */
2274 lfdep = mod->container;
2276 error = linker_file_add_dependency(lf, lfdep);
2281 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2283 printf("KLD %s: depends on %s - not available or"
2284 " version mismatch\n", lf->filename, modname);
2291 linker_addmodules(lf, start, stop, 0);
2296 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2298 struct sysctl_req *req;
2301 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2305 * Export a nul-separated, double-nul-terminated list of all function names
2309 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2315 error = mac_kld_check_stat(req->td->td_ucred);
2319 error = sysctl_wire_old_buffer(req, 0);
2323 TAILQ_FOREACH(lf, &linker_files, link) {
2324 error = LINKER_EACH_FUNCTION_NAME(lf,
2325 sysctl_kern_function_list_iterate, req);
2327 sx_xunlock(&kld_sx);
2331 sx_xunlock(&kld_sx);
2332 return (SYSCTL_OUT(req, "", 1));
2335 SYSCTL_PROC(_kern, OID_AUTO, function_list,
2336 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
2337 sysctl_kern_function_list, "",
2338 "kernel function list");