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/systm.h>
38 #include <sys/boottrace.h>
39 #include <sys/eventhandler.h>
40 #include <sys/fcntl.h>
42 #include <sys/kernel.h>
43 #include <sys/libkern.h>
44 #include <sys/linker.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysctl.h>
56 #include <sys/sysproto.h>
57 #include <sys/vnode.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;
201 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
204 sx_assert(&kld_sx, SA_XLOCKED);
206 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
209 * Perform a bubble sort of the system initialization objects by
210 * their subsystem (primary key) and order (secondary key).
212 * Since some things care about execution order, this is the operation
213 * which ensures continued function.
215 for (sipp = start; sipp < stop; sipp++) {
216 for (xipp = sipp + 1; xipp < stop; xipp++) {
217 if ((*sipp)->subsystem < (*xipp)->subsystem ||
218 ((*sipp)->subsystem == (*xipp)->subsystem &&
219 (*sipp)->order <= (*xipp)->order))
228 * Traverse the (now) ordered list of system initialization tasks.
229 * Perform each task, and continue on to the next task.
234 for (sipp = start; sipp < stop; sipp++) {
235 if ((*sipp)->subsystem == SI_SUB_DUMMY)
236 continue; /* skip dummy task(s) */
238 if ((*sipp)->subsystem > last)
239 BOOTTRACE("%s: sysinit 0x%7x", lf->filename,
243 (*((*sipp)->func)) ((*sipp)->udata);
244 last = (*sipp)->subsystem;
251 linker_file_sysuninit(linker_file_t lf)
253 struct sysinit **start, **stop, **sipp, **xipp, *save;
256 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
259 sx_assert(&kld_sx, SA_XLOCKED);
261 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
266 * Perform a reverse bubble sort of the system initialization objects
267 * by their subsystem (primary key) and order (secondary key).
269 * Since some things care about execution order, this is the operation
270 * which ensures continued function.
272 for (sipp = start; sipp < stop; sipp++) {
273 for (xipp = sipp + 1; xipp < stop; xipp++) {
274 if ((*sipp)->subsystem > (*xipp)->subsystem ||
275 ((*sipp)->subsystem == (*xipp)->subsystem &&
276 (*sipp)->order >= (*xipp)->order))
285 * Traverse the (now) ordered list of system initialization tasks.
286 * Perform each task, and continue on to the next task.
291 for (sipp = start; sipp < stop; sipp++) {
292 if ((*sipp)->subsystem == SI_SUB_DUMMY)
293 continue; /* skip dummy task(s) */
295 if ((*sipp)->subsystem > last)
296 BOOTTRACE("%s: sysuninit 0x%7x", lf->filename,
300 (*((*sipp)->func)) ((*sipp)->udata);
301 last = (*sipp)->subsystem;
308 linker_file_register_sysctls(linker_file_t lf, bool enable)
310 struct sysctl_oid **start, **stop, **oidp;
313 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
316 sx_assert(&kld_sx, SA_XLOCKED);
318 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
323 for (oidp = start; oidp < stop; oidp++) {
325 sysctl_register_oid(*oidp);
327 sysctl_register_disabled_oid(*oidp);
334 linker_file_enable_sysctls(linker_file_t lf)
336 struct sysctl_oid **start, **stop, **oidp;
339 ("linker_file_enable_sysctls: enable SYSCTLs for %s\n",
342 sx_assert(&kld_sx, SA_XLOCKED);
344 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
349 for (oidp = start; oidp < stop; oidp++)
350 sysctl_enable_oid(*oidp);
356 linker_file_unregister_sysctls(linker_file_t lf)
358 struct sysctl_oid **start, **stop, **oidp;
360 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
361 " for %s\n", lf->filename));
363 sx_assert(&kld_sx, SA_XLOCKED);
365 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
370 for (oidp = start; oidp < stop; oidp++)
371 sysctl_unregister_oid(*oidp);
377 linker_file_register_modules(linker_file_t lf)
379 struct mod_metadata **start, **stop, **mdp;
380 const moduledata_t *moddata;
381 int first_error, error;
383 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
384 " in %s\n", lf->filename));
386 sx_assert(&kld_sx, SA_XLOCKED);
388 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL) != 0) {
390 * This fallback should be unnecessary, but if we get booted
391 * from boot2 instead of loader and we are missing our
392 * metadata then we have to try the best we can.
394 if (lf == linker_kernel_file) {
395 start = SET_BEGIN(modmetadata_set);
396 stop = SET_LIMIT(modmetadata_set);
401 for (mdp = start; mdp < stop; mdp++) {
402 if ((*mdp)->md_type != MDT_MODULE)
404 moddata = (*mdp)->md_data;
405 KLD_DPF(FILE, ("Registering module %s in %s\n",
406 moddata->name, lf->filename));
407 error = module_register(moddata, lf);
409 printf("Module %s failed to register: %d\n",
410 moddata->name, error);
411 if (first_error == 0)
415 return (first_error);
419 linker_init_kernel_modules(void)
423 linker_file_register_modules(linker_kernel_file);
427 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
431 linker_load_file(const char *filename, linker_file_t *result)
435 int foundfile, error, modules;
437 /* Refuse to load modules if securelevel raised */
438 if (prison0.pr_securelevel > 0)
441 sx_assert(&kld_sx, SA_XLOCKED);
442 lf = linker_find_file_by_name(filename);
444 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
445 " incrementing refs\n", filename));
454 * We do not need to protect (lock) classes here because there is
455 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
456 * and there is no class deregistration mechanism at this time.
458 TAILQ_FOREACH(lc, &classes, link) {
459 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
461 error = LINKER_LOAD_FILE(lc, filename, &lf);
463 * If we got something other than ENOENT, then it exists but
464 * we cannot load it for some other reason.
469 error = linker_file_register_modules(lf);
470 if (error == EEXIST) {
471 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
474 modules = !TAILQ_EMPTY(&lf->modules);
475 linker_file_register_sysctls(lf, false);
476 linker_file_sysinit(lf);
477 lf->flags |= LINKER_FILE_LINKED;
480 * If all of the modules in this file failed
481 * to load, unload the file and return an
484 if (modules && TAILQ_EMPTY(&lf->modules)) {
485 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
488 linker_file_enable_sysctls(lf);
489 EVENTHANDLER_INVOKE(kld_load, lf);
495 * Less than ideal, but tells the user whether it failed to load or
496 * the module was not found.
500 * If the file type has not been recognized by the last try
501 * printout a message before to fail.
504 printf("%s: %s - unsupported file type\n",
508 * Format not recognized or otherwise unloadable.
509 * When loading a module that is statically built into
510 * the kernel EEXIST percolates back up as the return
511 * value. Preserve this so that apps like sysinstall
512 * can recognize this special case and not post bogus
518 error = ENOENT; /* Nothing found */
523 linker_reference_module(const char *modname, struct mod_depend *verinfo,
524 linker_file_t *result)
530 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
531 *result = mod->container;
537 error = linker_load_module(NULL, modname, NULL, verinfo, result);
543 linker_release_module(const char *modname, struct mod_depend *verinfo,
551 KASSERT(modname != NULL,
552 ("linker_release_module: no file or name"));
553 mod = modlist_lookup2(modname, verinfo);
560 KASSERT(modname == NULL && verinfo == NULL,
561 ("linker_release_module: both file and name"));
562 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
568 linker_find_file_by_name(const char *filename)
573 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
574 sprintf(koname, "%s.ko", filename);
576 sx_assert(&kld_sx, SA_XLOCKED);
577 TAILQ_FOREACH(lf, &linker_files, link) {
578 if (strcmp(lf->filename, koname) == 0)
580 if (strcmp(lf->filename, filename) == 0)
583 free(koname, M_LINKER);
588 linker_find_file_by_id(int fileid)
592 sx_assert(&kld_sx, SA_XLOCKED);
593 TAILQ_FOREACH(lf, &linker_files, link)
594 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
600 linker_file_foreach(linker_predicate_t *predicate, void *context)
606 TAILQ_FOREACH(lf, &linker_files, link) {
607 retval = predicate(lf, context);
616 linker_make_file(const char *pathname, linker_class_t lc)
619 const char *filename;
622 sx_assert(&kld_sx, SA_XLOCKED);
623 filename = linker_basename(pathname);
625 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
626 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
636 lf->filename = strdup(filename, M_LINKER);
637 lf->pathname = strdup(pathname, M_LINKER);
638 LINKER_GET_NEXT_FILE_ID(lf->id);
641 lf->loadcnt = ++loadcnt;
646 STAILQ_INIT(&lf->common);
647 TAILQ_INIT(&lf->modules);
648 TAILQ_INSERT_TAIL(&linker_files, lf, link);
653 linker_file_unload(linker_file_t file, int flags)
656 modlist_t ml, nextml;
657 struct common_symbol *cp;
660 /* Refuse to unload modules if securelevel raised. */
661 if (prison0.pr_securelevel > 0)
664 sx_assert(&kld_sx, SA_XLOCKED);
665 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
667 /* Easy case of just dropping a reference. */
668 if (file->refs > 1) {
673 /* Give eventhandlers a chance to prevent the unload. */
675 EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
679 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
680 " informing modules\n"));
683 * Quiesce all the modules to give them a chance to veto the unload.
686 for (mod = TAILQ_FIRST(&file->modules); mod;
687 mod = module_getfnext(mod)) {
688 error = module_quiesce(mod);
689 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
690 KLD_DPF(FILE, ("linker_file_unload: module %s"
691 " vetoed unload\n", module_getname(mod)));
693 * XXX: Do we need to tell all the quiesced modules
694 * that they can resume work now via a new module
704 * Inform any modules associated with this file that they are
708 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
709 next = module_getfnext(mod);
713 * Give the module a chance to veto the unload.
715 if ((error = module_unload(mod)) != 0) {
718 KLD_DPF(FILE, ("linker_file_unload: module %s"
719 " failed unload\n", module_getname(mod)));
729 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
730 if (ml->container == file) {
731 TAILQ_REMOVE(&found_modules, ml, link);
737 * Don't try to run SYSUNINITs if we are unloaded due to a
740 if (file->flags & LINKER_FILE_LINKED) {
741 file->flags &= ~LINKER_FILE_LINKED;
742 linker_file_unregister_sysctls(file);
743 linker_file_sysuninit(file);
745 TAILQ_REMOVE(&linker_files, file, link);
748 for (i = 0; i < file->ndeps; i++)
749 linker_file_unload(file->deps[i], flags);
750 free(file->deps, M_LINKER);
753 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
754 STAILQ_REMOVE_HEAD(&file->common, link);
760 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
763 if (file->filename) {
764 free(file->filename, M_LINKER);
765 file->filename = NULL;
767 if (file->pathname) {
768 free(file->pathname, M_LINKER);
769 file->pathname = NULL;
771 kobj_delete((kobj_t) file, M_LINKER);
776 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
778 return (LINKER_CTF_GET(file, lc));
782 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
784 linker_file_t *newdeps;
786 sx_assert(&kld_sx, SA_XLOCKED);
787 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
788 M_LINKER, M_WAITOK | M_ZERO);
789 file->deps[file->ndeps] = dep;
791 KLD_DPF(FILE, ("linker_file_add_dependency:"
792 " adding %s as dependency for %s\n",
793 dep->filename, file->filename));
798 * Locate a linker set and its contents. This is a helper function to avoid
799 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
800 * This function is used in this file so we can avoid having lots of (void **)
804 linker_file_lookup_set(linker_file_t file, const char *name,
805 void *firstp, void *lastp, int *countp)
808 sx_assert(&kld_sx, SA_LOCKED);
809 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
813 * List all functions in a file.
816 linker_file_function_listall(linker_file_t lf,
817 linker_function_nameval_callback_t callback_func, void *arg)
819 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
823 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
828 locked = sx_xlocked(&kld_sx);
831 sym = linker_file_lookup_symbol_internal(file, name, deps);
838 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
842 linker_symval_t symval;
844 size_t common_size = 0;
847 sx_assert(&kld_sx, SA_XLOCKED);
848 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
851 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
852 LINKER_SYMBOL_VALUES(file, sym, &symval);
853 if (symval.value == 0)
855 * For commons, first look them up in the
856 * dependencies and only allocate space if not found
859 common_size = symval.size;
861 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
862 ".value=%p\n", symval.value));
863 return (symval.value);
867 for (i = 0; i < file->ndeps; i++) {
868 address = linker_file_lookup_symbol_internal(
869 file->deps[i], name, 0);
871 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
872 " deps value=%p\n", address));
877 if (common_size > 0) {
879 * This is a common symbol which was not found in the
880 * dependencies. We maintain a simple common symbol table in
883 struct common_symbol *cp;
885 STAILQ_FOREACH(cp, &file->common, link) {
886 if (strcmp(cp->name, name) == 0) {
887 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
888 " old common value=%p\n", cp->address));
889 return (cp->address);
893 * Round the symbol size up to align.
895 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
896 cp = malloc(sizeof(struct common_symbol)
897 + common_size + strlen(name) + 1, M_LINKER,
899 cp->address = (caddr_t)(cp + 1);
900 cp->name = cp->address + common_size;
901 strcpy(cp->name, name);
902 bzero(cp->address, common_size);
903 STAILQ_INSERT_TAIL(&file->common, cp, link);
905 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
906 " value=%p\n", cp->address));
907 return (cp->address);
909 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
914 * Both DDB and stack(9) rely on the kernel linker to provide forward and
915 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
916 * do this in a lockfree manner. We provide a set of internal helper
917 * routines to perform these operations without locks, and then wrappers that
920 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
924 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
928 TAILQ_FOREACH(lf, &linker_files, link) {
929 if (LINKER_LOOKUP_DEBUG_SYMBOL(lf, symstr, sym) == 0)
937 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
940 c_linker_sym_t best, es;
941 u_long diff, bestdiff, off;
944 off = (uintptr_t)value;
946 TAILQ_FOREACH(lf, &linker_files, link) {
947 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
949 if (es != 0 && diff < bestdiff) {
968 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
972 TAILQ_FOREACH(lf, &linker_files, link) {
973 if (LINKER_DEBUG_SYMBOL_VALUES(lf, sym, symval) == 0)
980 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
983 linker_symval_t symval;
988 error = linker_debug_search_symbol(value, &sym, offset);
991 error = linker_debug_symbol_values(sym, &symval);
994 strlcpy(buf, symval.name, buflen);
999 * DDB Helpers. DDB has to look across multiple files with their own symbol
1000 * tables and string tables.
1002 * Note that we do not obey list locking protocols here. We really don't need
1003 * DDB to hang because somebody's got the lock held. We'll take the chance
1004 * that the files list is inconsistent instead.
1008 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
1011 return (linker_debug_lookup(symstr, sym));
1016 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
1019 return (linker_debug_search_symbol(value, sym, diffp));
1023 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
1026 return (linker_debug_symbol_values(sym, symval));
1030 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1034 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
1038 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
1039 * obey locking protocols, and offer a significantly less complex interface.
1042 linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen,
1043 long *offset, int flags)
1047 KASSERT((flags & (M_NOWAIT | M_WAITOK)) != 0 &&
1048 (flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
1049 ("%s: bad flags: 0x%x", __func__, flags));
1051 if (flags & M_NOWAIT) {
1052 if (!sx_try_slock(&kld_sx))
1053 return (EWOULDBLOCK);
1057 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
1058 sx_sunlock(&kld_sx);
1063 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1067 return (linker_search_symbol_name_flags(value, buf, buflen, offset,
1072 linker_kldload_busy(int flags)
1076 MPASS((flags & ~(LINKER_UB_UNLOCK | LINKER_UB_LOCKED |
1077 LINKER_UB_PCATCH)) == 0);
1078 if ((flags & LINKER_UB_LOCKED) != 0)
1079 sx_assert(&kld_sx, SA_XLOCKED);
1081 if ((flags & LINKER_UB_LOCKED) == 0)
1083 while (kld_busy > 0) {
1084 if (kld_busy_owner == curthread)
1086 error = sx_sleep(&kld_busy, &kld_sx,
1087 (flags & LINKER_UB_PCATCH) != 0 ? PCATCH : 0,
1090 if ((flags & LINKER_UB_UNLOCK) != 0)
1091 sx_xunlock(&kld_sx);
1096 kld_busy_owner = curthread;
1097 if ((flags & LINKER_UB_UNLOCK) != 0)
1098 sx_xunlock(&kld_sx);
1103 linker_kldload_unbusy(int flags)
1105 MPASS((flags & ~LINKER_UB_LOCKED) == 0);
1106 if ((flags & LINKER_UB_LOCKED) != 0)
1107 sx_assert(&kld_sx, SA_XLOCKED);
1109 if ((flags & LINKER_UB_LOCKED) == 0)
1111 MPASS(kld_busy > 0);
1112 if (kld_busy_owner != curthread)
1113 panic("linker_kldload_unbusy done by not owning thread %p",
1116 if (kld_busy == 0) {
1117 kld_busy_owner = NULL;
1120 sx_xunlock(&kld_sx);
1127 kern_kldload(struct thread *td, const char *file, int *fileid)
1129 const char *kldname, *modname;
1133 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1136 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1140 * If file does not contain a qualified name or any dot in it
1141 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1144 if (strchr(file, '/') || strchr(file, '.')) {
1152 error = linker_kldload_busy(LINKER_UB_PCATCH);
1154 sx_xunlock(&kld_sx);
1159 * It is possible that kldloaded module will attach a new ifnet,
1160 * so vnet context must be set when this ocurs.
1162 CURVNET_SET(TD_TO_VNET(td));
1164 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1172 linker_kldload_unbusy(LINKER_UB_LOCKED);
1177 sys_kldload(struct thread *td, struct kldload_args *uap)
1179 char *pathname = NULL;
1182 td->td_retval[0] = -1;
1184 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1185 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1187 error = kern_kldload(td, pathname, &fileid);
1189 td->td_retval[0] = fileid;
1191 free(pathname, M_TEMP);
1196 kern_kldunload(struct thread *td, int fileid, int flags)
1201 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1204 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1207 error = linker_kldload_busy(LINKER_UB_PCATCH);
1209 sx_xunlock(&kld_sx);
1213 CURVNET_SET(TD_TO_VNET(td));
1214 lf = linker_find_file_by_id(fileid);
1216 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1218 if (lf->userrefs == 0) {
1220 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1222 printf("kldunload: attempt to unload file that was"
1223 " loaded by the kernel\n");
1227 error = linker_file_unload(lf, flags);
1234 linker_kldload_unbusy(LINKER_UB_LOCKED);
1239 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1242 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1246 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1249 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1250 uap->flags != LINKER_UNLOAD_FORCE)
1252 return (kern_kldunload(td, uap->fileid, uap->flags));
1256 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1259 const char *filename;
1264 error = mac_kld_check_stat(td->td_ucred);
1269 td->td_retval[0] = -1;
1271 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1272 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1275 filename = linker_basename(pathname);
1277 lf = linker_find_file_by_name(filename);
1279 td->td_retval[0] = lf->id;
1282 sx_xunlock(&kld_sx);
1284 free(pathname, M_TEMP);
1289 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1295 error = mac_kld_check_stat(td->td_ucred);
1301 if (uap->fileid == 0)
1302 lf = TAILQ_FIRST(&linker_files);
1304 lf = linker_find_file_by_id(uap->fileid);
1309 lf = TAILQ_NEXT(lf, link);
1312 /* Skip partially loaded files. */
1313 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1314 lf = TAILQ_NEXT(lf, link);
1317 td->td_retval[0] = lf->id;
1319 td->td_retval[0] = 0;
1321 sx_xunlock(&kld_sx);
1326 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1328 struct kld_file_stat *stat;
1332 * Check the version of the user's structure.
1334 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1337 if (version != sizeof(struct kld_file_stat_1) &&
1338 version != sizeof(struct kld_file_stat))
1341 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
1342 error = kern_kldstat(td, uap->fileid, stat);
1344 error = copyout(stat, uap->stat, version);
1350 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1357 error = mac_kld_check_stat(td->td_ucred);
1363 lf = linker_find_file_by_id(fileid);
1365 sx_xunlock(&kld_sx);
1369 /* Version 1 fields: */
1370 namelen = strlen(lf->filename) + 1;
1371 if (namelen > sizeof(stat->name))
1372 namelen = sizeof(stat->name);
1373 bcopy(lf->filename, &stat->name[0], namelen);
1374 stat->refs = lf->refs;
1376 stat->address = lf->address;
1377 stat->size = lf->size;
1378 /* Version 2 fields: */
1379 namelen = strlen(lf->pathname) + 1;
1380 if (namelen > sizeof(stat->pathname))
1381 namelen = sizeof(stat->pathname);
1382 bcopy(lf->pathname, &stat->pathname[0], namelen);
1383 sx_xunlock(&kld_sx);
1385 td->td_retval[0] = 0;
1390 DB_COMMAND_FLAGS(kldstat, db_kldstat, DB_CMD_MEMSAFE)
1394 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
1395 db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
1396 #undef POINTER_WIDTH
1397 TAILQ_FOREACH(lf, &linker_files, link) {
1400 db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs,
1401 lf->address, lf->size, lf->filename);
1407 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1414 error = mac_kld_check_stat(td->td_ucred);
1420 lf = linker_find_file_by_id(uap->fileid);
1423 mp = TAILQ_FIRST(&lf->modules);
1425 td->td_retval[0] = module_getid(mp);
1427 td->td_retval[0] = 0;
1431 sx_xunlock(&kld_sx);
1436 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1438 char *symstr = NULL;
1440 linker_symval_t symval;
1442 struct kld_sym_lookup lookup;
1446 error = mac_kld_check_stat(td->td_ucred);
1451 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1453 if (lookup.version != sizeof(lookup) ||
1454 uap->cmd != KLDSYM_LOOKUP)
1456 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1457 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1460 if (uap->fileid != 0) {
1461 lf = linker_find_file_by_id(uap->fileid);
1464 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1465 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1466 lookup.symvalue = (uintptr_t) symval.value;
1467 lookup.symsize = symval.size;
1468 error = copyout(&lookup, uap->data, sizeof(lookup));
1472 TAILQ_FOREACH(lf, &linker_files, link) {
1473 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1474 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1475 lookup.symvalue = (uintptr_t)symval.value;
1476 lookup.symsize = symval.size;
1477 error = copyout(&lookup, uap->data,
1485 sx_xunlock(&kld_sx);
1487 free(symstr, M_TEMP);
1492 * Preloaded module support
1496 modlist_lookup(const char *name, int ver)
1500 TAILQ_FOREACH(mod, &found_modules, link) {
1501 if (strcmp(mod->name, name) == 0 &&
1502 (ver == 0 || mod->version == ver))
1509 modlist_lookup2(const char *name, const struct mod_depend *verinfo)
1511 modlist_t mod, bestmod;
1514 if (verinfo == NULL)
1515 return (modlist_lookup(name, 0));
1517 TAILQ_FOREACH(mod, &found_modules, link) {
1518 if (strcmp(mod->name, name) != 0)
1521 if (ver == verinfo->md_ver_preferred)
1523 if (ver >= verinfo->md_ver_minimum &&
1524 ver <= verinfo->md_ver_maximum &&
1525 (bestmod == NULL || ver > bestmod->version))
1532 modlist_newmodule(const char *modname, int version, linker_file_t container)
1536 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1538 panic("no memory for module list");
1539 mod->container = container;
1540 mod->name = modname;
1541 mod->version = version;
1542 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1547 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1548 struct mod_metadata **stop, int preload)
1550 struct mod_metadata *mp, **mdp;
1551 const char *modname;
1554 for (mdp = start; mdp < stop; mdp++) {
1556 if (mp->md_type != MDT_VERSION)
1558 modname = mp->md_cval;
1559 ver = ((const struct mod_version *)mp->md_data)->mv_version;
1560 if (modlist_lookup(modname, ver) != NULL) {
1561 printf("module %s already present!\n", modname);
1562 /* XXX what can we do? this is a build error. :-( */
1565 modlist_newmodule(modname, ver, lf);
1570 linker_preload(void *arg)
1573 const char *modname, *nmodname;
1575 linker_file_t lf, nlf;
1578 linker_file_list_t loaded_files;
1579 linker_file_list_t depended_files;
1580 struct mod_metadata *mp, *nmp;
1581 struct mod_metadata **start, **stop, **mdp, **nmdp;
1582 const struct mod_depend *verinfo;
1586 struct sysinit **si_start, **si_stop;
1588 TAILQ_INIT(&loaded_files);
1589 TAILQ_INIT(&depended_files);
1590 TAILQ_INIT(&found_modules);
1595 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1596 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1597 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1598 if (modname == NULL) {
1599 printf("Preloaded module at %p does not have a"
1600 " name!\n", modptr);
1603 if (modtype == NULL) {
1604 printf("Preloaded module at %p does not have a type!\n",
1609 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1612 TAILQ_FOREACH(lc, &classes, link) {
1613 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1619 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1623 * First get a list of stuff in the kernel.
1625 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1627 linker_addmodules(linker_kernel_file, start, stop, 1);
1630 * This is a once-off kinky bubble sort to resolve relocation
1631 * dependency requirements.
1634 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1635 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1638 * First, look to see if we would successfully link with this
1641 resolves = 1; /* unless we know otherwise */
1643 for (mdp = start; mdp < stop; mdp++) {
1645 if (mp->md_type != MDT_DEPEND)
1647 modname = mp->md_cval;
1648 verinfo = mp->md_data;
1649 for (nmdp = start; nmdp < stop; nmdp++) {
1651 if (nmp->md_type != MDT_VERSION)
1653 nmodname = nmp->md_cval;
1654 if (strcmp(modname, nmodname) == 0)
1657 if (nmdp < stop) /* it's a self reference */
1661 * ok, the module isn't here yet, we
1664 if (modlist_lookup2(modname, verinfo) == NULL)
1669 * OK, if we found our modules, we can link. So, "provide"
1670 * the modules inside and add it to the end of the link order
1675 for (mdp = start; mdp < stop; mdp++) {
1677 if (mp->md_type != MDT_VERSION)
1679 modname = mp->md_cval;
1680 nver = ((const struct mod_version *)
1681 mp->md_data)->mv_version;
1682 if (modlist_lookup(modname,
1684 printf("module %s already"
1685 " present!\n", modname);
1686 TAILQ_REMOVE(&loaded_files,
1688 linker_file_unload(lf,
1689 LINKER_UNLOAD_FORCE);
1690 /* we changed tailq next ptr */
1693 modlist_newmodule(modname, nver, lf);
1696 TAILQ_REMOVE(&loaded_files, lf, loaded);
1697 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1699 * Since we provided modules, we need to restart the
1700 * sort so that the previous files that depend on us
1701 * have a chance. Also, we've busted the tailq next
1702 * pointer with the REMOVE.
1709 * At this point, we check to see what could not be resolved..
1711 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1712 TAILQ_REMOVE(&loaded_files, lf, loaded);
1713 printf("KLD file %s is missing dependencies\n", lf->filename);
1714 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1718 * We made it. Finish off the linking in the order we determined.
1720 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1721 if (linker_kernel_file) {
1722 linker_kernel_file->refs++;
1723 error = linker_file_add_dependency(lf,
1724 linker_kernel_file);
1726 panic("cannot add dependency");
1728 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1731 for (mdp = start; mdp < stop; mdp++) {
1733 if (mp->md_type != MDT_DEPEND)
1735 modname = mp->md_cval;
1736 verinfo = mp->md_data;
1737 mod = modlist_lookup2(modname, verinfo);
1739 printf("KLD file %s - cannot find "
1740 "dependency \"%s\"\n",
1741 lf->filename, modname);
1744 /* Don't count self-dependencies */
1745 if (lf == mod->container)
1747 mod->container->refs++;
1748 error = linker_file_add_dependency(lf,
1751 panic("cannot add dependency");
1755 * Now do relocation etc using the symbol search paths
1756 * established by the dependencies
1758 error = LINKER_LINK_PRELOAD_FINISH(lf);
1760 printf("KLD file %s - could not finalize loading\n",
1764 linker_file_register_modules(lf);
1765 if (!TAILQ_EMPTY(&lf->modules))
1766 lf->flags |= LINKER_FILE_MODULES;
1767 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1768 &si_stop, NULL) == 0)
1769 sysinit_add(si_start, si_stop);
1770 linker_file_register_sysctls(lf, true);
1771 lf->flags |= LINKER_FILE_LINKED;
1774 TAILQ_REMOVE(&depended_files, lf, loaded);
1775 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1777 sx_xunlock(&kld_sx);
1778 /* woohoo! we made it! */
1781 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, NULL);
1784 * Handle preload files that failed to load any modules.
1787 linker_preload_finish(void *arg)
1789 linker_file_t lf, nlf;
1792 TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) {
1794 * If all of the modules in this file failed to load, unload
1795 * the file and return an error of ENOEXEC. (Parity with
1796 * linker_load_file.)
1798 if ((lf->flags & LINKER_FILE_MODULES) != 0 &&
1799 TAILQ_EMPTY(&lf->modules)) {
1800 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1804 lf->flags &= ~LINKER_FILE_MODULES;
1805 lf->userrefs++; /* so we can (try to) kldunload it */
1807 sx_xunlock(&kld_sx);
1811 * Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be
1812 * scheduled at any subsystem and order, so run this as late as possible. init
1813 * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that.
1815 SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE,
1816 linker_preload_finish, NULL);
1819 * Search for a not-loaded module by name.
1821 * Modules may be found in the following locations:
1823 * - preloaded (result is just the module name) - on disk (result is full path
1826 * If the module name is qualified in any way (contains path, etc.) the we
1827 * simply return a copy of it.
1829 * The search path can be manipulated via sysctl. Note that we use the ';'
1830 * character as a separator to be consistent with the bootloader.
1833 static char linker_hintfile[] = "linker.hints";
1834 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1836 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path,
1837 sizeof(linker_path), "module load search path");
1839 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1841 static const char * const linker_ext_list[] = {
1848 * Check if file actually exists either with or without extension listed in
1849 * the linker_ext_list. (probably should be generic for the rest of the
1853 linker_lookup_file(const char *path, int pathlen, const char *name,
1854 int namelen, struct vattr *vap)
1856 struct nameidata nd;
1857 struct thread *td = curthread; /* XXX */
1858 const char * const *cpp, *sep;
1860 int error, len, extlen, reclen, flags;
1864 for (cpp = linker_ext_list; *cpp; cpp++) {
1869 extlen++; /* trailing '\0' */
1870 sep = (path[pathlen - 1] != '/') ? "/" : "";
1872 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1873 result = malloc(reclen, M_LINKER, M_WAITOK);
1874 for (cpp = linker_ext_list; *cpp; cpp++) {
1875 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1876 namelen, name, *cpp);
1878 * Attempt to open the file, and return the path if
1879 * we succeed and it's a regular file.
1881 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result);
1883 error = vn_open(&nd, &flags, 0, NULL);
1886 type = nd.ni_vp->v_type;
1888 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1889 VOP_UNLOCK(nd.ni_vp);
1890 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1895 free(result, M_LINKER);
1899 #define INT_ALIGN(base, ptr) ptr = \
1900 (base) + roundup2((ptr) - (base), sizeof(int))
1903 * Lookup KLD which contains requested module in the "linker.hints" file. If
1904 * version specification is available, then try to find the best KLD.
1905 * Otherwise just find the latest one.
1908 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1909 int modnamelen, const struct mod_depend *verinfo)
1911 struct thread *td = curthread; /* XXX */
1912 struct ucred *cred = td ? td->td_ucred : NULL;
1913 struct nameidata nd;
1914 struct vattr vattr, mattr;
1915 const char *best, *sep;
1916 u_char *hints = NULL;
1917 u_char *cp, *recptr, *bufend, *result, *pathbuf;
1918 int error, ival, bestver, *intp, found, flags, clen, blen;
1922 bestver = found = 0;
1924 sep = (path[pathlen - 1] != '/') ? "/" : "";
1925 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1927 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1928 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1931 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf);
1933 error = vn_open(&nd, &flags, 0, NULL);
1937 if (nd.ni_vp->v_type != VREG)
1940 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1944 * XXX: we need to limit this number to some reasonable value
1946 if (vattr.va_size > LINKER_HINTS_MAX) {
1947 printf("linker.hints file too large %ld\n", (long)vattr.va_size);
1950 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1951 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1952 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1955 VOP_UNLOCK(nd.ni_vp);
1956 vn_close(nd.ni_vp, FREAD, cred, td);
1959 printf("can't read %zd\n", reclen);
1962 intp = (int *)hints;
1964 if (ival != LINKER_HINTS_VERSION) {
1965 printf("linker.hints file version mismatch %d\n", ival);
1968 bufend = hints + vattr.va_size;
1969 recptr = (u_char *)intp;
1971 while (recptr < bufend && !found) {
1972 intp = (int *)recptr;
1979 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1982 INT_ALIGN(hints, cp);
1986 if (verinfo == NULL ||
1987 ival == verinfo->md_ver_preferred) {
1991 if (ival >= verinfo->md_ver_minimum &&
1992 ival <= verinfo->md_ver_maximum &&
2002 recptr += reclen + sizeof(int);
2005 * Finally check if KLD is in the place
2008 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
2010 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
2013 * KLD is newer than hints file. What we should do now?
2015 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
2016 printf("warning: KLD '%s' is newer than the linker.hints"
2019 free(pathbuf, M_LINKER);
2021 free(hints, M_TEMP);
2022 if (nd.ni_vp != NULL) {
2023 VOP_UNLOCK(nd.ni_vp);
2024 vn_close(nd.ni_vp, FREAD, cred, td);
2027 * If nothing found or hints is absent - fallback to the old
2028 * way by using "kldname[.ko]" as module name.
2030 if (!found && !bestver && result == NULL)
2031 result = linker_lookup_file(path, pathlen, modname,
2037 * Lookup KLD which contains requested module in the all directories.
2040 linker_search_module(const char *modname, int modnamelen,
2041 const struct mod_depend *verinfo)
2043 char *cp, *ep, *result;
2046 * traverse the linker path
2048 for (cp = linker_path; *cp; cp = ep + 1) {
2049 /* find the end of this component */
2050 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
2051 result = linker_hints_lookup(cp, ep - cp, modname,
2052 modnamelen, verinfo);
2062 * Search for module in all directories listed in the linker_path.
2065 linker_search_kld(const char *name)
2067 char *cp, *ep, *result;
2070 /* qualified at all? */
2071 if (strchr(name, '/'))
2072 return (strdup(name, M_LINKER));
2074 /* traverse the linker path */
2076 for (ep = linker_path; *ep; ep++) {
2078 /* find the end of this component */
2079 for (; *ep != 0 && *ep != ';'; ep++);
2080 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
2088 linker_basename(const char *path)
2090 const char *filename;
2092 filename = strrchr(path, '/');
2093 if (filename == NULL)
2102 * Inform hwpmc about the set of kernel modules currently loaded.
2105 linker_hwpmc_list_objects(void)
2108 struct pmckern_map_in *kobase;
2113 TAILQ_FOREACH(lf, &linker_files, link)
2116 /* Allocate nmappings + 1 entries. */
2117 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
2118 M_LINKER, M_WAITOK | M_ZERO);
2120 TAILQ_FOREACH(lf, &linker_files, link) {
2121 /* Save the info for this linker file. */
2122 kobase[i].pm_file = lf->filename;
2123 kobase[i].pm_address = (uintptr_t)lf->address;
2126 sx_sunlock(&kld_sx);
2128 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
2130 /* The last entry of the malloced area comprises of all zeros. */
2131 KASSERT(kobase[i].pm_file == NULL,
2132 ("linker_hwpmc_list_objects: last object not NULL"));
2134 return ((void *)kobase);
2138 /* check if root file system is not mounted */
2140 linker_root_mounted(void)
2145 if (rootvnode == NULL)
2148 pwd = pwd_hold(curthread);
2149 ret = pwd->pwd_rdir != NULL;
2155 * Find a file which contains given module and load it, if "parent" is not
2156 * NULL, register a reference to it.
2159 linker_load_module(const char *kldname, const char *modname,
2160 struct linker_file *parent, const struct mod_depend *verinfo,
2161 struct linker_file **lfpp)
2163 linker_file_t lfdep;
2164 const char *filename;
2168 sx_assert(&kld_sx, SA_XLOCKED);
2169 if (modname == NULL) {
2171 * We have to load KLD
2173 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2175 if (!linker_root_mounted())
2177 pathname = linker_search_kld(kldname);
2179 if (modlist_lookup2(modname, verinfo) != NULL)
2181 if (!linker_root_mounted())
2183 if (kldname != NULL)
2184 pathname = strdup(kldname, M_LINKER);
2187 * Need to find a KLD with required module
2189 pathname = linker_search_module(modname,
2190 strlen(modname), verinfo);
2192 if (pathname == NULL)
2196 * Can't load more than one file with the same basename XXX:
2197 * Actually it should be possible to have multiple KLDs with
2198 * the same basename but different path because they can
2199 * provide different versions of the same modules.
2201 filename = linker_basename(pathname);
2202 if (linker_find_file_by_name(filename))
2205 error = linker_load_file(pathname, &lfdep);
2208 if (modname && verinfo &&
2209 modlist_lookup2(modname, verinfo) == NULL) {
2210 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2215 error = linker_file_add_dependency(parent, lfdep);
2222 free(pathname, M_LINKER);
2227 * This routine is responsible for finding dependencies of userland initiated
2228 * kldload(2)'s of files.
2231 linker_load_dependencies(linker_file_t lf)
2233 linker_file_t lfdep;
2234 struct mod_metadata **start, **stop, **mdp, **nmdp;
2235 struct mod_metadata *mp, *nmp;
2236 const struct mod_depend *verinfo;
2238 const char *modname, *nmodname;
2242 * All files are dependent on /kernel.
2244 sx_assert(&kld_sx, SA_XLOCKED);
2245 if (linker_kernel_file) {
2246 linker_kernel_file->refs++;
2247 error = linker_file_add_dependency(lf, linker_kernel_file);
2251 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2254 for (mdp = start; mdp < stop; mdp++) {
2256 if (mp->md_type != MDT_VERSION)
2258 modname = mp->md_cval;
2259 ver = ((const struct mod_version *)mp->md_data)->mv_version;
2260 mod = modlist_lookup(modname, ver);
2262 printf("interface %s.%d already present in the KLD"
2263 " '%s'!\n", modname, ver,
2264 mod->container->filename);
2269 for (mdp = start; mdp < stop; mdp++) {
2271 if (mp->md_type != MDT_DEPEND)
2273 modname = mp->md_cval;
2274 verinfo = mp->md_data;
2276 for (nmdp = start; nmdp < stop; nmdp++) {
2278 if (nmp->md_type != MDT_VERSION)
2280 nmodname = nmp->md_cval;
2281 if (strcmp(modname, nmodname) == 0)
2284 if (nmdp < stop)/* early exit, it's a self reference */
2286 mod = modlist_lookup2(modname, verinfo);
2287 if (mod) { /* woohoo, it's loaded already */
2288 lfdep = mod->container;
2290 error = linker_file_add_dependency(lf, lfdep);
2295 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2297 printf("KLD %s: depends on %s - not available or"
2298 " version mismatch\n", lf->filename, modname);
2305 linker_addmodules(lf, start, stop, 0);
2310 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2312 struct sysctl_req *req;
2315 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2319 * Export a nul-separated, double-nul-terminated list of all function names
2323 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2329 error = mac_kld_check_stat(req->td->td_ucred);
2333 error = sysctl_wire_old_buffer(req, 0);
2337 TAILQ_FOREACH(lf, &linker_files, link) {
2338 error = LINKER_EACH_FUNCTION_NAME(lf,
2339 sysctl_kern_function_list_iterate, req);
2341 sx_xunlock(&kld_sx);
2345 sx_xunlock(&kld_sx);
2346 return (SYSCTL_OUT(req, "", 1));
2349 SYSCTL_PROC(_kern, OID_AUTO, function_list,
2350 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
2351 sysctl_kern_function_list, "",
2352 "kernel function list");