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 bool kld_busy;
112 * Load counter used by clients to determine if a linker file has been
113 * re-loaded. This counter is incremented for each file load.
117 static linker_class_list_t classes;
118 static linker_file_list_t linker_files;
119 static int next_file_id = 1;
120 static int linker_no_more_classes = 0;
122 #define LINKER_GET_NEXT_FILE_ID(a) do { \
123 linker_file_t lftmp; \
126 sx_assert(&kld_sx, SA_XLOCKED); \
128 TAILQ_FOREACH(lftmp, &linker_files, link) { \
129 if (next_file_id == lftmp->id) { \
134 (a) = next_file_id; \
137 /* XXX wrong name; we're looking at version provision tags here, not modules */
138 typedef TAILQ_HEAD(, modlist) modlisthead_t;
140 TAILQ_ENTRY(modlist) link; /* chain together all modules */
141 linker_file_t container;
145 typedef struct modlist *modlist_t;
146 static modlisthead_t found_modules;
148 static int linker_file_add_dependency(linker_file_t file,
150 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
151 const char* name, int deps);
152 static int linker_load_module(const char *kldname,
153 const char *modname, struct linker_file *parent,
154 const struct mod_depend *verinfo, struct linker_file **lfpp);
155 static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo);
158 linker_init(void *arg)
161 sx_init(&kld_sx, "kernel linker");
162 TAILQ_INIT(&classes);
163 TAILQ_INIT(&linker_files);
166 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, NULL);
169 linker_stop_class_add(void *arg)
172 linker_no_more_classes = 1;
175 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
178 linker_add_class(linker_class_t lc)
182 * We disallow any class registration past SI_ORDER_ANY
183 * of SI_SUB_KLD. We bump the reference count to keep the
184 * ops from being freed.
186 if (linker_no_more_classes == 1)
188 kobj_class_compile((kobj_class_t) lc);
189 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
190 TAILQ_INSERT_TAIL(&classes, lc, link);
195 linker_file_sysinit(linker_file_t lf)
197 struct sysinit **start, **stop, **sipp, **xipp, *save;
199 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
202 sx_assert(&kld_sx, SA_XLOCKED);
204 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
207 * Perform a bubble sort of the system initialization objects by
208 * their subsystem (primary key) and order (secondary key).
210 * Since some things care about execution order, this is the operation
211 * which ensures continued function.
213 for (sipp = start; sipp < stop; sipp++) {
214 for (xipp = sipp + 1; xipp < stop; xipp++) {
215 if ((*sipp)->subsystem < (*xipp)->subsystem ||
216 ((*sipp)->subsystem == (*xipp)->subsystem &&
217 (*sipp)->order <= (*xipp)->order))
226 * Traverse the (now) ordered list of system initialization tasks.
227 * Perform each task, and continue on to the next task.
231 for (sipp = start; sipp < stop; sipp++) {
232 if ((*sipp)->subsystem == SI_SUB_DUMMY)
233 continue; /* skip dummy task(s) */
236 (*((*sipp)->func)) ((*sipp)->udata);
243 linker_file_sysuninit(linker_file_t lf)
245 struct sysinit **start, **stop, **sipp, **xipp, *save;
247 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
250 sx_assert(&kld_sx, SA_XLOCKED);
252 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
257 * Perform a reverse bubble sort of the system initialization objects
258 * by their subsystem (primary key) and order (secondary key).
260 * Since some things care about execution order, this is the operation
261 * which ensures continued function.
263 for (sipp = start; sipp < stop; sipp++) {
264 for (xipp = sipp + 1; xipp < stop; xipp++) {
265 if ((*sipp)->subsystem > (*xipp)->subsystem ||
266 ((*sipp)->subsystem == (*xipp)->subsystem &&
267 (*sipp)->order >= (*xipp)->order))
276 * Traverse the (now) ordered list of system initialization tasks.
277 * Perform each task, and continue on to the next task.
281 for (sipp = start; sipp < stop; sipp++) {
282 if ((*sipp)->subsystem == SI_SUB_DUMMY)
283 continue; /* skip dummy task(s) */
286 (*((*sipp)->func)) ((*sipp)->udata);
293 linker_file_register_sysctls(linker_file_t lf, bool enable)
295 struct sysctl_oid **start, **stop, **oidp;
298 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
301 sx_assert(&kld_sx, SA_XLOCKED);
303 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
308 for (oidp = start; oidp < stop; oidp++) {
310 sysctl_register_oid(*oidp);
312 sysctl_register_disabled_oid(*oidp);
319 linker_file_enable_sysctls(linker_file_t lf)
321 struct sysctl_oid **start, **stop, **oidp;
324 ("linker_file_enable_sysctls: enable SYSCTLs for %s\n",
327 sx_assert(&kld_sx, SA_XLOCKED);
329 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
334 for (oidp = start; oidp < stop; oidp++)
335 sysctl_enable_oid(*oidp);
341 linker_file_unregister_sysctls(linker_file_t lf)
343 struct sysctl_oid **start, **stop, **oidp;
345 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
346 " for %s\n", lf->filename));
348 sx_assert(&kld_sx, SA_XLOCKED);
350 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
355 for (oidp = start; oidp < stop; oidp++)
356 sysctl_unregister_oid(*oidp);
362 linker_file_register_modules(linker_file_t lf)
364 struct mod_metadata **start, **stop, **mdp;
365 const moduledata_t *moddata;
366 int first_error, error;
368 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
369 " in %s\n", lf->filename));
371 sx_assert(&kld_sx, SA_XLOCKED);
373 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL) != 0) {
375 * This fallback should be unnecessary, but if we get booted
376 * from boot2 instead of loader and we are missing our
377 * metadata then we have to try the best we can.
379 if (lf == linker_kernel_file) {
380 start = SET_BEGIN(modmetadata_set);
381 stop = SET_LIMIT(modmetadata_set);
386 for (mdp = start; mdp < stop; mdp++) {
387 if ((*mdp)->md_type != MDT_MODULE)
389 moddata = (*mdp)->md_data;
390 KLD_DPF(FILE, ("Registering module %s in %s\n",
391 moddata->name, lf->filename));
392 error = module_register(moddata, lf);
394 printf("Module %s failed to register: %d\n",
395 moddata->name, error);
396 if (first_error == 0)
400 return (first_error);
404 linker_init_kernel_modules(void)
408 linker_file_register_modules(linker_kernel_file);
412 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
416 linker_load_file(const char *filename, linker_file_t *result)
420 int foundfile, error, modules;
422 /* Refuse to load modules if securelevel raised */
423 if (prison0.pr_securelevel > 0)
426 sx_assert(&kld_sx, SA_XLOCKED);
427 lf = linker_find_file_by_name(filename);
429 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
430 " incrementing refs\n", filename));
439 * We do not need to protect (lock) classes here because there is
440 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
441 * and there is no class deregistration mechanism at this time.
443 TAILQ_FOREACH(lc, &classes, link) {
444 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
446 error = LINKER_LOAD_FILE(lc, filename, &lf);
448 * If we got something other than ENOENT, then it exists but
449 * we cannot load it for some other reason.
454 error = linker_file_register_modules(lf);
455 if (error == EEXIST) {
456 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
459 modules = !TAILQ_EMPTY(&lf->modules);
460 linker_file_register_sysctls(lf, false);
461 linker_file_sysinit(lf);
462 lf->flags |= LINKER_FILE_LINKED;
465 * If all of the modules in this file failed
466 * to load, unload the file and return an
469 if (modules && TAILQ_EMPTY(&lf->modules)) {
470 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
473 linker_file_enable_sysctls(lf);
474 EVENTHANDLER_INVOKE(kld_load, lf);
480 * Less than ideal, but tells the user whether it failed to load or
481 * the module was not found.
485 * If the file type has not been recognized by the last try
486 * printout a message before to fail.
489 printf("%s: %s - unsupported file type\n",
493 * Format not recognized or otherwise unloadable.
494 * When loading a module that is statically built into
495 * the kernel EEXIST percolates back up as the return
496 * value. Preserve this so that apps like sysinstall
497 * can recognize this special case and not post bogus
503 error = ENOENT; /* Nothing found */
508 linker_reference_module(const char *modname, struct mod_depend *verinfo,
509 linker_file_t *result)
515 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
516 *result = mod->container;
522 error = linker_load_module(NULL, modname, NULL, verinfo, result);
528 linker_release_module(const char *modname, struct mod_depend *verinfo,
536 KASSERT(modname != NULL,
537 ("linker_release_module: no file or name"));
538 mod = modlist_lookup2(modname, verinfo);
545 KASSERT(modname == NULL && verinfo == NULL,
546 ("linker_release_module: both file and name"));
547 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
553 linker_find_file_by_name(const char *filename)
558 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
559 sprintf(koname, "%s.ko", filename);
561 sx_assert(&kld_sx, SA_XLOCKED);
562 TAILQ_FOREACH(lf, &linker_files, link) {
563 if (strcmp(lf->filename, koname) == 0)
565 if (strcmp(lf->filename, filename) == 0)
568 free(koname, M_LINKER);
573 linker_find_file_by_id(int fileid)
577 sx_assert(&kld_sx, SA_XLOCKED);
578 TAILQ_FOREACH(lf, &linker_files, link)
579 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
585 linker_file_foreach(linker_predicate_t *predicate, void *context)
591 TAILQ_FOREACH(lf, &linker_files, link) {
592 retval = predicate(lf, context);
601 linker_make_file(const char *pathname, linker_class_t lc)
604 const char *filename;
607 sx_assert(&kld_sx, SA_XLOCKED);
608 filename = linker_basename(pathname);
610 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
611 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
619 lf->filename = strdup(filename, M_LINKER);
620 lf->pathname = strdup(pathname, M_LINKER);
621 LINKER_GET_NEXT_FILE_ID(lf->id);
624 lf->loadcnt = ++loadcnt;
629 STAILQ_INIT(&lf->common);
630 TAILQ_INIT(&lf->modules);
631 TAILQ_INSERT_TAIL(&linker_files, lf, link);
636 linker_file_unload(linker_file_t file, int flags)
639 modlist_t ml, nextml;
640 struct common_symbol *cp;
643 /* Refuse to unload modules if securelevel raised. */
644 if (prison0.pr_securelevel > 0)
647 sx_assert(&kld_sx, SA_XLOCKED);
648 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
650 /* Easy case of just dropping a reference. */
651 if (file->refs > 1) {
656 /* Give eventhandlers a chance to prevent the unload. */
658 EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
662 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
663 " informing modules\n"));
666 * Quiesce all the modules to give them a chance to veto the unload.
669 for (mod = TAILQ_FIRST(&file->modules); mod;
670 mod = module_getfnext(mod)) {
671 error = module_quiesce(mod);
672 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
673 KLD_DPF(FILE, ("linker_file_unload: module %s"
674 " vetoed unload\n", module_getname(mod)));
676 * XXX: Do we need to tell all the quiesced modules
677 * that they can resume work now via a new module
687 * Inform any modules associated with this file that they are
691 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
692 next = module_getfnext(mod);
696 * Give the module a chance to veto the unload.
698 if ((error = module_unload(mod)) != 0) {
701 KLD_DPF(FILE, ("linker_file_unload: module %s"
702 " failed unload\n", module_getname(mod)));
712 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
713 if (ml->container == file) {
714 TAILQ_REMOVE(&found_modules, ml, link);
720 * Don't try to run SYSUNINITs if we are unloaded due to a
723 if (file->flags & LINKER_FILE_LINKED) {
724 file->flags &= ~LINKER_FILE_LINKED;
725 linker_file_unregister_sysctls(file);
726 linker_file_sysuninit(file);
728 TAILQ_REMOVE(&linker_files, file, link);
731 for (i = 0; i < file->ndeps; i++)
732 linker_file_unload(file->deps[i], flags);
733 free(file->deps, M_LINKER);
736 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
737 STAILQ_REMOVE_HEAD(&file->common, link);
743 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
746 if (file->filename) {
747 free(file->filename, M_LINKER);
748 file->filename = NULL;
750 if (file->pathname) {
751 free(file->pathname, M_LINKER);
752 file->pathname = NULL;
754 kobj_delete((kobj_t) file, M_LINKER);
759 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
761 return (LINKER_CTF_GET(file, lc));
765 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
767 linker_file_t *newdeps;
769 sx_assert(&kld_sx, SA_XLOCKED);
770 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
771 M_LINKER, M_WAITOK | M_ZERO);
772 file->deps[file->ndeps] = dep;
774 KLD_DPF(FILE, ("linker_file_add_dependency:"
775 " adding %s as dependency for %s\n",
776 dep->filename, file->filename));
781 * Locate a linker set and its contents. This is a helper function to avoid
782 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
783 * This function is used in this file so we can avoid having lots of (void **)
787 linker_file_lookup_set(linker_file_t file, const char *name,
788 void *firstp, void *lastp, int *countp)
791 sx_assert(&kld_sx, SA_LOCKED);
792 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
796 * List all functions in a file.
799 linker_file_function_listall(linker_file_t lf,
800 linker_function_nameval_callback_t callback_func, void *arg)
802 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
806 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
811 locked = sx_xlocked(&kld_sx);
814 sym = linker_file_lookup_symbol_internal(file, name, deps);
821 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
825 linker_symval_t symval;
827 size_t common_size = 0;
830 sx_assert(&kld_sx, SA_XLOCKED);
831 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
834 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
835 LINKER_SYMBOL_VALUES(file, sym, &symval);
836 if (symval.value == 0)
838 * For commons, first look them up in the
839 * dependencies and only allocate space if not found
842 common_size = symval.size;
844 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
845 ".value=%p\n", symval.value));
846 return (symval.value);
850 for (i = 0; i < file->ndeps; i++) {
851 address = linker_file_lookup_symbol_internal(
852 file->deps[i], name, 0);
854 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
855 " deps value=%p\n", address));
860 if (common_size > 0) {
862 * This is a common symbol which was not found in the
863 * dependencies. We maintain a simple common symbol table in
866 struct common_symbol *cp;
868 STAILQ_FOREACH(cp, &file->common, link) {
869 if (strcmp(cp->name, name) == 0) {
870 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
871 " old common value=%p\n", cp->address));
872 return (cp->address);
876 * Round the symbol size up to align.
878 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
879 cp = malloc(sizeof(struct common_symbol)
880 + common_size + strlen(name) + 1, M_LINKER,
882 cp->address = (caddr_t)(cp + 1);
883 cp->name = cp->address + common_size;
884 strcpy(cp->name, name);
885 bzero(cp->address, common_size);
886 STAILQ_INSERT_TAIL(&file->common, cp, link);
888 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
889 " value=%p\n", cp->address));
890 return (cp->address);
892 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
897 * Both DDB and stack(9) rely on the kernel linker to provide forward and
898 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
899 * do this in a lockfree manner. We provide a set of internal helper
900 * routines to perform these operations without locks, and then wrappers that
903 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
907 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
911 TAILQ_FOREACH(lf, &linker_files, link) {
912 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
920 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
923 c_linker_sym_t best, es;
924 u_long diff, bestdiff, off;
927 off = (uintptr_t)value;
929 TAILQ_FOREACH(lf, &linker_files, link) {
930 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
932 if (es != 0 && diff < bestdiff) {
951 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
955 TAILQ_FOREACH(lf, &linker_files, link) {
956 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
963 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
966 linker_symval_t symval;
971 error = linker_debug_search_symbol(value, &sym, offset);
974 error = linker_debug_symbol_values(sym, &symval);
977 strlcpy(buf, symval.name, buflen);
982 * DDB Helpers. DDB has to look across multiple files with their own symbol
983 * tables and string tables.
985 * Note that we do not obey list locking protocols here. We really don't need
986 * DDB to hang because somebody's got the lock held. We'll take the chance
987 * that the files list is inconsistent instead.
991 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
994 return (linker_debug_lookup(symstr, sym));
999 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
1002 return (linker_debug_search_symbol(value, sym, diffp));
1006 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
1009 return (linker_debug_symbol_values(sym, symval));
1013 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1017 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
1021 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
1022 * obey locking protocols, and offer a significantly less complex interface.
1025 linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen,
1026 long *offset, int flags)
1030 KASSERT((flags & (M_NOWAIT | M_WAITOK)) != 0 &&
1031 (flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
1032 ("%s: bad flags: 0x%x", __func__, flags));
1034 if (flags & M_NOWAIT) {
1035 if (!sx_try_slock(&kld_sx))
1036 return (EWOULDBLOCK);
1040 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
1041 sx_sunlock(&kld_sx);
1046 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1050 return (linker_search_symbol_name_flags(value, buf, buflen, offset,
1055 linker_kldload_busy(int flags)
1059 MPASS((flags & ~(LINKER_UB_UNLOCK | LINKER_UB_LOCKED |
1060 LINKER_UB_PCATCH)) == 0);
1061 if ((flags & LINKER_UB_LOCKED) != 0)
1062 sx_assert(&kld_sx, SA_XLOCKED);
1064 if ((flags & LINKER_UB_LOCKED) == 0)
1067 error = sx_sleep(&kld_busy, &kld_sx,
1068 (flags & LINKER_UB_PCATCH) != 0 ? PCATCH : 0,
1071 if ((flags & LINKER_UB_UNLOCK) != 0)
1072 sx_xunlock(&kld_sx);
1077 if ((flags & LINKER_UB_UNLOCK) != 0)
1078 sx_xunlock(&kld_sx);
1083 linker_kldload_unbusy(int flags)
1085 MPASS((flags & ~LINKER_UB_LOCKED) == 0);
1086 if ((flags & LINKER_UB_LOCKED) != 0)
1087 sx_assert(&kld_sx, SA_XLOCKED);
1089 if ((flags & LINKER_UB_LOCKED) == 0)
1094 sx_xunlock(&kld_sx);
1101 kern_kldload(struct thread *td, const char *file, int *fileid)
1103 const char *kldname, *modname;
1107 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1110 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1114 * If file does not contain a qualified name or any dot in it
1115 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1118 if (strchr(file, '/') || strchr(file, '.')) {
1126 error = linker_kldload_busy(LINKER_UB_PCATCH);
1128 sx_xunlock(&kld_sx);
1133 * It is possible that kldloaded module will attach a new ifnet,
1134 * so vnet context must be set when this ocurs.
1136 CURVNET_SET(TD_TO_VNET(td));
1138 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1146 linker_kldload_unbusy(LINKER_UB_LOCKED);
1151 sys_kldload(struct thread *td, struct kldload_args *uap)
1153 char *pathname = NULL;
1156 td->td_retval[0] = -1;
1158 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1159 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1161 error = kern_kldload(td, pathname, &fileid);
1163 td->td_retval[0] = fileid;
1165 free(pathname, M_TEMP);
1170 kern_kldunload(struct thread *td, int fileid, int flags)
1175 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1178 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1181 error = linker_kldload_busy(LINKER_UB_PCATCH);
1183 sx_xunlock(&kld_sx);
1187 CURVNET_SET(TD_TO_VNET(td));
1188 lf = linker_find_file_by_id(fileid);
1190 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1192 if (lf->userrefs == 0) {
1194 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1196 printf("kldunload: attempt to unload file that was"
1197 " loaded by the kernel\n");
1201 error = linker_file_unload(lf, flags);
1208 linker_kldload_unbusy(LINKER_UB_LOCKED);
1213 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1216 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1220 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1223 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1224 uap->flags != LINKER_UNLOAD_FORCE)
1226 return (kern_kldunload(td, uap->fileid, uap->flags));
1230 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1233 const char *filename;
1238 error = mac_kld_check_stat(td->td_ucred);
1243 td->td_retval[0] = -1;
1245 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1246 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1249 filename = linker_basename(pathname);
1251 lf = linker_find_file_by_name(filename);
1253 td->td_retval[0] = lf->id;
1256 sx_xunlock(&kld_sx);
1258 free(pathname, M_TEMP);
1263 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1269 error = mac_kld_check_stat(td->td_ucred);
1275 if (uap->fileid == 0)
1276 lf = TAILQ_FIRST(&linker_files);
1278 lf = linker_find_file_by_id(uap->fileid);
1283 lf = TAILQ_NEXT(lf, link);
1286 /* Skip partially loaded files. */
1287 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1288 lf = TAILQ_NEXT(lf, link);
1291 td->td_retval[0] = lf->id;
1293 td->td_retval[0] = 0;
1295 sx_xunlock(&kld_sx);
1300 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1302 struct kld_file_stat *stat;
1306 * Check the version of the user's structure.
1308 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1311 if (version != sizeof(struct kld_file_stat_1) &&
1312 version != sizeof(struct kld_file_stat))
1315 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
1316 error = kern_kldstat(td, uap->fileid, stat);
1318 error = copyout(stat, uap->stat, version);
1324 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1331 error = mac_kld_check_stat(td->td_ucred);
1337 lf = linker_find_file_by_id(fileid);
1339 sx_xunlock(&kld_sx);
1343 /* Version 1 fields: */
1344 namelen = strlen(lf->filename) + 1;
1345 if (namelen > sizeof(stat->name))
1346 namelen = sizeof(stat->name);
1347 bcopy(lf->filename, &stat->name[0], namelen);
1348 stat->refs = lf->refs;
1350 stat->address = lf->address;
1351 stat->size = lf->size;
1352 /* Version 2 fields: */
1353 namelen = strlen(lf->pathname) + 1;
1354 if (namelen > sizeof(stat->pathname))
1355 namelen = sizeof(stat->pathname);
1356 bcopy(lf->pathname, &stat->pathname[0], namelen);
1357 sx_xunlock(&kld_sx);
1359 td->td_retval[0] = 0;
1364 DB_COMMAND(kldstat, db_kldstat)
1368 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
1369 db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
1370 #undef POINTER_WIDTH
1371 TAILQ_FOREACH(lf, &linker_files, link) {
1374 db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs,
1375 lf->address, lf->size, lf->filename);
1381 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1388 error = mac_kld_check_stat(td->td_ucred);
1394 lf = linker_find_file_by_id(uap->fileid);
1397 mp = TAILQ_FIRST(&lf->modules);
1399 td->td_retval[0] = module_getid(mp);
1401 td->td_retval[0] = 0;
1405 sx_xunlock(&kld_sx);
1410 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1412 char *symstr = NULL;
1414 linker_symval_t symval;
1416 struct kld_sym_lookup lookup;
1420 error = mac_kld_check_stat(td->td_ucred);
1425 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1427 if (lookup.version != sizeof(lookup) ||
1428 uap->cmd != KLDSYM_LOOKUP)
1430 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1431 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1434 if (uap->fileid != 0) {
1435 lf = linker_find_file_by_id(uap->fileid);
1438 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1439 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1440 lookup.symvalue = (uintptr_t) symval.value;
1441 lookup.symsize = symval.size;
1442 error = copyout(&lookup, uap->data, sizeof(lookup));
1446 TAILQ_FOREACH(lf, &linker_files, link) {
1447 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1448 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1449 lookup.symvalue = (uintptr_t)symval.value;
1450 lookup.symsize = symval.size;
1451 error = copyout(&lookup, uap->data,
1459 sx_xunlock(&kld_sx);
1461 free(symstr, M_TEMP);
1466 * Preloaded module support
1470 modlist_lookup(const char *name, int ver)
1474 TAILQ_FOREACH(mod, &found_modules, link) {
1475 if (strcmp(mod->name, name) == 0 &&
1476 (ver == 0 || mod->version == ver))
1483 modlist_lookup2(const char *name, const struct mod_depend *verinfo)
1485 modlist_t mod, bestmod;
1488 if (verinfo == NULL)
1489 return (modlist_lookup(name, 0));
1491 TAILQ_FOREACH(mod, &found_modules, link) {
1492 if (strcmp(mod->name, name) != 0)
1495 if (ver == verinfo->md_ver_preferred)
1497 if (ver >= verinfo->md_ver_minimum &&
1498 ver <= verinfo->md_ver_maximum &&
1499 (bestmod == NULL || ver > bestmod->version))
1506 modlist_newmodule(const char *modname, int version, linker_file_t container)
1510 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1512 panic("no memory for module list");
1513 mod->container = container;
1514 mod->name = modname;
1515 mod->version = version;
1516 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1521 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1522 struct mod_metadata **stop, int preload)
1524 struct mod_metadata *mp, **mdp;
1525 const char *modname;
1528 for (mdp = start; mdp < stop; mdp++) {
1530 if (mp->md_type != MDT_VERSION)
1532 modname = mp->md_cval;
1533 ver = ((const struct mod_version *)mp->md_data)->mv_version;
1534 if (modlist_lookup(modname, ver) != NULL) {
1535 printf("module %s already present!\n", modname);
1536 /* XXX what can we do? this is a build error. :-( */
1539 modlist_newmodule(modname, ver, lf);
1544 linker_preload(void *arg)
1547 const char *modname, *nmodname;
1549 linker_file_t lf, nlf;
1552 linker_file_list_t loaded_files;
1553 linker_file_list_t depended_files;
1554 struct mod_metadata *mp, *nmp;
1555 struct mod_metadata **start, **stop, **mdp, **nmdp;
1556 const struct mod_depend *verinfo;
1560 struct sysinit **si_start, **si_stop;
1562 TAILQ_INIT(&loaded_files);
1563 TAILQ_INIT(&depended_files);
1564 TAILQ_INIT(&found_modules);
1569 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1570 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1571 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1572 if (modname == NULL) {
1573 printf("Preloaded module at %p does not have a"
1574 " name!\n", modptr);
1577 if (modtype == NULL) {
1578 printf("Preloaded module at %p does not have a type!\n",
1583 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1586 TAILQ_FOREACH(lc, &classes, link) {
1587 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1593 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1597 * First get a list of stuff in the kernel.
1599 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1601 linker_addmodules(linker_kernel_file, start, stop, 1);
1604 * This is a once-off kinky bubble sort to resolve relocation
1605 * dependency requirements.
1608 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1609 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1612 * First, look to see if we would successfully link with this
1615 resolves = 1; /* unless we know otherwise */
1617 for (mdp = start; mdp < stop; mdp++) {
1619 if (mp->md_type != MDT_DEPEND)
1621 modname = mp->md_cval;
1622 verinfo = mp->md_data;
1623 for (nmdp = start; nmdp < stop; nmdp++) {
1625 if (nmp->md_type != MDT_VERSION)
1627 nmodname = nmp->md_cval;
1628 if (strcmp(modname, nmodname) == 0)
1631 if (nmdp < stop) /* it's a self reference */
1635 * ok, the module isn't here yet, we
1638 if (modlist_lookup2(modname, verinfo) == NULL)
1643 * OK, if we found our modules, we can link. So, "provide"
1644 * the modules inside and add it to the end of the link order
1649 for (mdp = start; mdp < stop; mdp++) {
1651 if (mp->md_type != MDT_VERSION)
1653 modname = mp->md_cval;
1654 nver = ((const struct mod_version *)
1655 mp->md_data)->mv_version;
1656 if (modlist_lookup(modname,
1658 printf("module %s already"
1659 " present!\n", modname);
1660 TAILQ_REMOVE(&loaded_files,
1662 linker_file_unload(lf,
1663 LINKER_UNLOAD_FORCE);
1664 /* we changed tailq next ptr */
1667 modlist_newmodule(modname, nver, lf);
1670 TAILQ_REMOVE(&loaded_files, lf, loaded);
1671 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1673 * Since we provided modules, we need to restart the
1674 * sort so that the previous files that depend on us
1675 * have a chance. Also, we've busted the tailq next
1676 * pointer with the REMOVE.
1683 * At this point, we check to see what could not be resolved..
1685 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1686 TAILQ_REMOVE(&loaded_files, lf, loaded);
1687 printf("KLD file %s is missing dependencies\n", lf->filename);
1688 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1692 * We made it. Finish off the linking in the order we determined.
1694 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1695 if (linker_kernel_file) {
1696 linker_kernel_file->refs++;
1697 error = linker_file_add_dependency(lf,
1698 linker_kernel_file);
1700 panic("cannot add dependency");
1702 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1705 for (mdp = start; mdp < stop; mdp++) {
1707 if (mp->md_type != MDT_DEPEND)
1709 modname = mp->md_cval;
1710 verinfo = mp->md_data;
1711 mod = modlist_lookup2(modname, verinfo);
1713 printf("KLD file %s - cannot find "
1714 "dependency \"%s\"\n",
1715 lf->filename, modname);
1718 /* Don't count self-dependencies */
1719 if (lf == mod->container)
1721 mod->container->refs++;
1722 error = linker_file_add_dependency(lf,
1725 panic("cannot add dependency");
1729 * Now do relocation etc using the symbol search paths
1730 * established by the dependencies
1732 error = LINKER_LINK_PRELOAD_FINISH(lf);
1734 printf("KLD file %s - could not finalize loading\n",
1738 linker_file_register_modules(lf);
1739 if (!TAILQ_EMPTY(&lf->modules))
1740 lf->flags |= LINKER_FILE_MODULES;
1741 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1742 &si_stop, NULL) == 0)
1743 sysinit_add(si_start, si_stop);
1744 linker_file_register_sysctls(lf, true);
1745 lf->flags |= LINKER_FILE_LINKED;
1748 TAILQ_REMOVE(&depended_files, lf, loaded);
1749 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1751 sx_xunlock(&kld_sx);
1752 /* woohoo! we made it! */
1755 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, NULL);
1758 * Handle preload files that failed to load any modules.
1761 linker_preload_finish(void *arg)
1763 linker_file_t lf, nlf;
1766 TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) {
1768 * If all of the modules in this file failed to load, unload
1769 * the file and return an error of ENOEXEC. (Parity with
1770 * linker_load_file.)
1772 if ((lf->flags & LINKER_FILE_MODULES) != 0 &&
1773 TAILQ_EMPTY(&lf->modules)) {
1774 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1778 lf->flags &= ~LINKER_FILE_MODULES;
1779 lf->userrefs++; /* so we can (try to) kldunload it */
1781 sx_xunlock(&kld_sx);
1785 * Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be
1786 * scheduled at any subsystem and order, so run this as late as possible. init
1787 * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that.
1789 SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE,
1790 linker_preload_finish, NULL);
1793 * Search for a not-loaded module by name.
1795 * Modules may be found in the following locations:
1797 * - preloaded (result is just the module name) - on disk (result is full path
1800 * If the module name is qualified in any way (contains path, etc.) the we
1801 * simply return a copy of it.
1803 * The search path can be manipulated via sysctl. Note that we use the ';'
1804 * character as a separator to be consistent with the bootloader.
1807 static char linker_hintfile[] = "linker.hints";
1808 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1810 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path,
1811 sizeof(linker_path), "module load search path");
1813 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1815 static const char * const linker_ext_list[] = {
1822 * Check if file actually exists either with or without extension listed in
1823 * the linker_ext_list. (probably should be generic for the rest of the
1827 linker_lookup_file(const char *path, int pathlen, const char *name,
1828 int namelen, struct vattr *vap)
1830 struct nameidata nd;
1831 struct thread *td = curthread; /* XXX */
1832 const char * const *cpp, *sep;
1834 int error, len, extlen, reclen, flags;
1838 for (cpp = linker_ext_list; *cpp; cpp++) {
1843 extlen++; /* trailing '\0' */
1844 sep = (path[pathlen - 1] != '/') ? "/" : "";
1846 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1847 result = malloc(reclen, M_LINKER, M_WAITOK);
1848 for (cpp = linker_ext_list; *cpp; cpp++) {
1849 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1850 namelen, name, *cpp);
1852 * Attempt to open the file, and return the path if
1853 * we succeed and it's a regular file.
1855 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1857 error = vn_open(&nd, &flags, 0, NULL);
1859 NDFREE(&nd, NDF_ONLY_PNBUF);
1860 type = nd.ni_vp->v_type;
1862 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1863 VOP_UNLOCK(nd.ni_vp);
1864 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1869 free(result, M_LINKER);
1873 #define INT_ALIGN(base, ptr) ptr = \
1874 (base) + roundup2((ptr) - (base), sizeof(int))
1877 * Lookup KLD which contains requested module in the "linker.hints" file. If
1878 * version specification is available, then try to find the best KLD.
1879 * Otherwise just find the latest one.
1882 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1883 int modnamelen, const struct mod_depend *verinfo)
1885 struct thread *td = curthread; /* XXX */
1886 struct ucred *cred = td ? td->td_ucred : NULL;
1887 struct nameidata nd;
1888 struct vattr vattr, mattr;
1889 const char *best, *sep;
1890 u_char *hints = NULL;
1891 u_char *cp, *recptr, *bufend, *result, *pathbuf;
1892 int error, ival, bestver, *intp, found, flags, clen, blen;
1896 bestver = found = 0;
1898 sep = (path[pathlen - 1] != '/') ? "/" : "";
1899 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1901 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1902 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1905 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1907 error = vn_open(&nd, &flags, 0, NULL);
1910 NDFREE(&nd, NDF_ONLY_PNBUF);
1911 if (nd.ni_vp->v_type != VREG)
1914 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1918 * XXX: we need to limit this number to some reasonable value
1920 if (vattr.va_size > LINKER_HINTS_MAX) {
1921 printf("linker.hints file too large %ld\n", (long)vattr.va_size);
1924 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1925 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1926 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1929 VOP_UNLOCK(nd.ni_vp);
1930 vn_close(nd.ni_vp, FREAD, cred, td);
1933 printf("can't read %zd\n", reclen);
1936 intp = (int *)hints;
1938 if (ival != LINKER_HINTS_VERSION) {
1939 printf("linker.hints file version mismatch %d\n", ival);
1942 bufend = hints + vattr.va_size;
1943 recptr = (u_char *)intp;
1945 while (recptr < bufend && !found) {
1946 intp = (int *)recptr;
1953 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1956 INT_ALIGN(hints, cp);
1960 if (verinfo == NULL ||
1961 ival == verinfo->md_ver_preferred) {
1965 if (ival >= verinfo->md_ver_minimum &&
1966 ival <= verinfo->md_ver_maximum &&
1976 recptr += reclen + sizeof(int);
1979 * Finally check if KLD is in the place
1982 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1984 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1987 * KLD is newer than hints file. What we should do now?
1989 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1990 printf("warning: KLD '%s' is newer than the linker.hints"
1993 free(pathbuf, M_LINKER);
1995 free(hints, M_TEMP);
1996 if (nd.ni_vp != NULL) {
1997 VOP_UNLOCK(nd.ni_vp);
1998 vn_close(nd.ni_vp, FREAD, cred, td);
2001 * If nothing found or hints is absent - fallback to the old
2002 * way by using "kldname[.ko]" as module name.
2004 if (!found && !bestver && result == NULL)
2005 result = linker_lookup_file(path, pathlen, modname,
2011 * Lookup KLD which contains requested module in the all directories.
2014 linker_search_module(const char *modname, int modnamelen,
2015 const struct mod_depend *verinfo)
2017 char *cp, *ep, *result;
2020 * traverse the linker path
2022 for (cp = linker_path; *cp; cp = ep + 1) {
2023 /* find the end of this component */
2024 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
2025 result = linker_hints_lookup(cp, ep - cp, modname,
2026 modnamelen, verinfo);
2036 * Search for module in all directories listed in the linker_path.
2039 linker_search_kld(const char *name)
2041 char *cp, *ep, *result;
2044 /* qualified at all? */
2045 if (strchr(name, '/'))
2046 return (strdup(name, M_LINKER));
2048 /* traverse the linker path */
2050 for (ep = linker_path; *ep; ep++) {
2052 /* find the end of this component */
2053 for (; *ep != 0 && *ep != ';'; ep++);
2054 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
2062 linker_basename(const char *path)
2064 const char *filename;
2066 filename = strrchr(path, '/');
2067 if (filename == NULL)
2076 * Inform hwpmc about the set of kernel modules currently loaded.
2079 linker_hwpmc_list_objects(void)
2082 struct pmckern_map_in *kobase;
2087 TAILQ_FOREACH(lf, &linker_files, link)
2090 /* Allocate nmappings + 1 entries. */
2091 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
2092 M_LINKER, M_WAITOK | M_ZERO);
2094 TAILQ_FOREACH(lf, &linker_files, link) {
2095 /* Save the info for this linker file. */
2096 kobase[i].pm_file = lf->filename;
2097 kobase[i].pm_address = (uintptr_t)lf->address;
2100 sx_sunlock(&kld_sx);
2102 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
2104 /* The last entry of the malloced area comprises of all zeros. */
2105 KASSERT(kobase[i].pm_file == NULL,
2106 ("linker_hwpmc_list_objects: last object not NULL"));
2108 return ((void *)kobase);
2112 /* check if root file system is not mounted */
2114 linker_root_mounted(void)
2119 if (rootvnode == NULL)
2122 pwd = pwd_hold(curthread);
2123 ret = pwd->pwd_rdir != NULL;
2129 * Find a file which contains given module and load it, if "parent" is not
2130 * NULL, register a reference to it.
2133 linker_load_module(const char *kldname, const char *modname,
2134 struct linker_file *parent, const struct mod_depend *verinfo,
2135 struct linker_file **lfpp)
2137 linker_file_t lfdep;
2138 const char *filename;
2142 sx_assert(&kld_sx, SA_XLOCKED);
2143 if (modname == NULL) {
2145 * We have to load KLD
2147 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2149 if (!linker_root_mounted())
2151 pathname = linker_search_kld(kldname);
2153 if (modlist_lookup2(modname, verinfo) != NULL)
2155 if (!linker_root_mounted())
2157 if (kldname != NULL)
2158 pathname = strdup(kldname, M_LINKER);
2161 * Need to find a KLD with required module
2163 pathname = linker_search_module(modname,
2164 strlen(modname), verinfo);
2166 if (pathname == NULL)
2170 * Can't load more than one file with the same basename XXX:
2171 * Actually it should be possible to have multiple KLDs with
2172 * the same basename but different path because they can
2173 * provide different versions of the same modules.
2175 filename = linker_basename(pathname);
2176 if (linker_find_file_by_name(filename))
2179 error = linker_load_file(pathname, &lfdep);
2182 if (modname && verinfo &&
2183 modlist_lookup2(modname, verinfo) == NULL) {
2184 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2189 error = linker_file_add_dependency(parent, lfdep);
2196 free(pathname, M_LINKER);
2201 * This routine is responsible for finding dependencies of userland initiated
2202 * kldload(2)'s of files.
2205 linker_load_dependencies(linker_file_t lf)
2207 linker_file_t lfdep;
2208 struct mod_metadata **start, **stop, **mdp, **nmdp;
2209 struct mod_metadata *mp, *nmp;
2210 const struct mod_depend *verinfo;
2212 const char *modname, *nmodname;
2216 * All files are dependent on /kernel.
2218 sx_assert(&kld_sx, SA_XLOCKED);
2219 if (linker_kernel_file) {
2220 linker_kernel_file->refs++;
2221 error = linker_file_add_dependency(lf, linker_kernel_file);
2225 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2228 for (mdp = start; mdp < stop; mdp++) {
2230 if (mp->md_type != MDT_VERSION)
2232 modname = mp->md_cval;
2233 ver = ((const struct mod_version *)mp->md_data)->mv_version;
2234 mod = modlist_lookup(modname, ver);
2236 printf("interface %s.%d already present in the KLD"
2237 " '%s'!\n", modname, ver,
2238 mod->container->filename);
2243 for (mdp = start; mdp < stop; mdp++) {
2245 if (mp->md_type != MDT_DEPEND)
2247 modname = mp->md_cval;
2248 verinfo = mp->md_data;
2250 for (nmdp = start; nmdp < stop; nmdp++) {
2252 if (nmp->md_type != MDT_VERSION)
2254 nmodname = nmp->md_cval;
2255 if (strcmp(modname, nmodname) == 0)
2258 if (nmdp < stop)/* early exit, it's a self reference */
2260 mod = modlist_lookup2(modname, verinfo);
2261 if (mod) { /* woohoo, it's loaded already */
2262 lfdep = mod->container;
2264 error = linker_file_add_dependency(lf, lfdep);
2269 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2271 printf("KLD %s: depends on %s - not available or"
2272 " version mismatch\n", lf->filename, modname);
2279 linker_addmodules(lf, start, stop, 0);
2284 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2286 struct sysctl_req *req;
2289 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2293 * Export a nul-separated, double-nul-terminated list of all function names
2297 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2303 error = mac_kld_check_stat(req->td->td_ucred);
2307 error = sysctl_wire_old_buffer(req, 0);
2311 TAILQ_FOREACH(lf, &linker_files, link) {
2312 error = LINKER_EACH_FUNCTION_NAME(lf,
2313 sysctl_kern_function_list_iterate, req);
2315 sx_xunlock(&kld_sx);
2319 sx_xunlock(&kld_sx);
2320 return (SYSCTL_OUT(req, "", 1));
2323 SYSCTL_PROC(_kern, OID_AUTO, function_list,
2324 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
2325 sysctl_kern_function_list, "",
2326 "kernel function list");