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/sysent.h>
57 #include <sys/sysproto.h>
58 #include <sys/vnode.h>
66 #include <security/mac/mac_framework.h>
68 #include "linker_if.h"
71 #include <sys/pmckern.h>
76 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RWTUN,
77 &kld_debug, 0, "Set various levels of KLD debug");
80 /* These variables are used by kernel debuggers to enumerate loaded files. */
81 const int kld_off_address = offsetof(struct linker_file, address);
82 const int kld_off_filename = offsetof(struct linker_file, filename);
83 const int kld_off_pathname = offsetof(struct linker_file, pathname);
84 const int kld_off_next = offsetof(struct linker_file, link.tqe_next);
87 * static char *linker_search_path(const char *name, struct mod_depend
90 static const char *linker_basename(const char *path);
93 * Find a currently loaded file given its filename.
95 static linker_file_t linker_find_file_by_name(const char* _filename);
98 * Find a currently loaded file given its file id.
100 static linker_file_t linker_find_file_by_id(int _fileid);
102 /* Metadata from the static kernel */
103 SET_DECLARE(modmetadata_set, struct mod_metadata);
105 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
107 linker_file_t linker_kernel_file;
109 static struct sx kld_sx; /* kernel linker lock */
110 static u_int kld_busy;
111 static struct thread *kld_busy_owner;
114 * Load counter used by clients to determine if a linker file has been
115 * re-loaded. This counter is incremented for each file load.
119 static linker_class_list_t classes;
120 static linker_file_list_t linker_files;
121 static int next_file_id = 1;
122 static int linker_no_more_classes = 0;
124 #define LINKER_GET_NEXT_FILE_ID(a) do { \
125 linker_file_t lftmp; \
128 sx_assert(&kld_sx, SA_XLOCKED); \
130 TAILQ_FOREACH(lftmp, &linker_files, link) { \
131 if (next_file_id == lftmp->id) { \
136 (a) = next_file_id; \
139 /* XXX wrong name; we're looking at version provision tags here, not modules */
140 typedef TAILQ_HEAD(, modlist) modlisthead_t;
142 TAILQ_ENTRY(modlist) link; /* chain together all modules */
143 linker_file_t container;
147 typedef struct modlist *modlist_t;
148 static modlisthead_t found_modules;
150 static int linker_file_add_dependency(linker_file_t file,
152 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
153 const char* name, int deps);
154 static int linker_load_module(const char *kldname,
155 const char *modname, struct linker_file *parent,
156 const struct mod_depend *verinfo, struct linker_file **lfpp);
157 static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo);
160 linker_init(void *arg)
163 sx_init(&kld_sx, "kernel linker");
164 TAILQ_INIT(&classes);
165 TAILQ_INIT(&linker_files);
168 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, NULL);
171 linker_stop_class_add(void *arg)
174 linker_no_more_classes = 1;
177 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
180 linker_add_class(linker_class_t lc)
184 * We disallow any class registration past SI_ORDER_ANY
185 * of SI_SUB_KLD. We bump the reference count to keep the
186 * ops from being freed.
188 if (linker_no_more_classes == 1)
190 kobj_class_compile((kobj_class_t) lc);
191 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
192 TAILQ_INSERT_TAIL(&classes, lc, link);
197 linker_file_sysinit(linker_file_t lf)
199 struct sysinit **start, **stop, **sipp, **xipp, *save;
202 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
205 sx_assert(&kld_sx, SA_XLOCKED);
207 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
210 * Perform a bubble sort of the system initialization objects by
211 * their subsystem (primary key) and order (secondary key).
213 * Since some things care about execution order, this is the operation
214 * which ensures continued function.
216 for (sipp = start; sipp < stop; sipp++) {
217 for (xipp = sipp + 1; xipp < stop; xipp++) {
218 if ((*sipp)->subsystem < (*xipp)->subsystem ||
219 ((*sipp)->subsystem == (*xipp)->subsystem &&
220 (*sipp)->order <= (*xipp)->order))
229 * Traverse the (now) ordered list of system initialization tasks.
230 * Perform each task, and continue on to the next task.
235 for (sipp = start; sipp < stop; sipp++) {
236 if ((*sipp)->subsystem == SI_SUB_DUMMY)
237 continue; /* skip dummy task(s) */
239 if ((*sipp)->subsystem > last)
240 BOOTTRACE("%s: sysinit 0x%7x", lf->filename,
244 (*((*sipp)->func)) ((*sipp)->udata);
245 last = (*sipp)->subsystem;
252 linker_file_sysuninit(linker_file_t lf)
254 struct sysinit **start, **stop, **sipp, **xipp, *save;
257 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
260 sx_assert(&kld_sx, SA_XLOCKED);
262 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
267 * Perform a reverse bubble sort of the system initialization objects
268 * by their subsystem (primary key) and order (secondary key).
270 * Since some things care about execution order, this is the operation
271 * which ensures continued function.
273 for (sipp = start; sipp < stop; sipp++) {
274 for (xipp = sipp + 1; xipp < stop; xipp++) {
275 if ((*sipp)->subsystem > (*xipp)->subsystem ||
276 ((*sipp)->subsystem == (*xipp)->subsystem &&
277 (*sipp)->order >= (*xipp)->order))
286 * Traverse the (now) ordered list of system initialization tasks.
287 * Perform each task, and continue on to the next task.
292 for (sipp = start; sipp < stop; sipp++) {
293 if ((*sipp)->subsystem == SI_SUB_DUMMY)
294 continue; /* skip dummy task(s) */
296 if ((*sipp)->subsystem > last)
297 BOOTTRACE("%s: sysuninit 0x%7x", lf->filename,
301 (*((*sipp)->func)) ((*sipp)->udata);
302 last = (*sipp)->subsystem;
309 linker_file_register_sysctls(linker_file_t lf, bool enable)
311 struct sysctl_oid **start, **stop, **oidp;
314 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
317 sx_assert(&kld_sx, SA_XLOCKED);
319 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
324 for (oidp = start; oidp < stop; oidp++) {
326 sysctl_register_oid(*oidp);
328 sysctl_register_disabled_oid(*oidp);
335 linker_file_enable_sysctls(linker_file_t lf)
337 struct sysctl_oid **start, **stop, **oidp;
340 ("linker_file_enable_sysctls: enable SYSCTLs for %s\n",
343 sx_assert(&kld_sx, SA_XLOCKED);
345 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
350 for (oidp = start; oidp < stop; oidp++)
351 sysctl_enable_oid(*oidp);
357 linker_file_unregister_sysctls(linker_file_t lf)
359 struct sysctl_oid **start, **stop, **oidp;
361 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
362 " for %s\n", lf->filename));
364 sx_assert(&kld_sx, SA_XLOCKED);
366 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
371 for (oidp = start; oidp < stop; oidp++)
372 sysctl_unregister_oid(*oidp);
378 linker_file_register_modules(linker_file_t lf)
380 struct mod_metadata **start, **stop, **mdp;
381 const moduledata_t *moddata;
382 int first_error, error;
384 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
385 " in %s\n", lf->filename));
387 sx_assert(&kld_sx, SA_XLOCKED);
389 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL) != 0) {
391 * This fallback should be unnecessary, but if we get booted
392 * from boot2 instead of loader and we are missing our
393 * metadata then we have to try the best we can.
395 if (lf == linker_kernel_file) {
396 start = SET_BEGIN(modmetadata_set);
397 stop = SET_LIMIT(modmetadata_set);
402 for (mdp = start; mdp < stop; mdp++) {
403 if ((*mdp)->md_type != MDT_MODULE)
405 moddata = (*mdp)->md_data;
406 KLD_DPF(FILE, ("Registering module %s in %s\n",
407 moddata->name, lf->filename));
408 error = module_register(moddata, lf);
410 printf("Module %s failed to register: %d\n",
411 moddata->name, error);
412 if (first_error == 0)
416 return (first_error);
420 linker_init_kernel_modules(void)
424 linker_file_register_modules(linker_kernel_file);
428 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
432 linker_load_file(const char *filename, linker_file_t *result)
436 int foundfile, error, modules;
438 /* Refuse to load modules if securelevel raised */
439 if (prison0.pr_securelevel > 0)
442 sx_assert(&kld_sx, SA_XLOCKED);
443 lf = linker_find_file_by_name(filename);
445 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
446 " incrementing refs\n", filename));
455 * We do not need to protect (lock) classes here because there is
456 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
457 * and there is no class deregistration mechanism at this time.
459 TAILQ_FOREACH(lc, &classes, link) {
460 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
462 error = LINKER_LOAD_FILE(lc, filename, &lf);
464 * If we got something other than ENOENT, then it exists but
465 * we cannot load it for some other reason.
470 error = linker_file_register_modules(lf);
471 if (error == EEXIST) {
472 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
475 modules = !TAILQ_EMPTY(&lf->modules);
476 linker_file_register_sysctls(lf, false);
477 linker_file_sysinit(lf);
478 lf->flags |= LINKER_FILE_LINKED;
481 * If all of the modules in this file failed
482 * to load, unload the file and return an
485 if (modules && TAILQ_EMPTY(&lf->modules)) {
486 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
489 linker_file_enable_sysctls(lf);
490 EVENTHANDLER_INVOKE(kld_load, lf);
496 * Less than ideal, but tells the user whether it failed to load or
497 * the module was not found.
501 * If the file type has not been recognized by the last try
502 * printout a message before to fail.
505 printf("%s: %s - unsupported file type\n",
509 * Format not recognized or otherwise unloadable.
510 * When loading a module that is statically built into
511 * the kernel EEXIST percolates back up as the return
512 * value. Preserve this so that apps like sysinstall
513 * can recognize this special case and not post bogus
519 error = ENOENT; /* Nothing found */
524 linker_reference_module(const char *modname, struct mod_depend *verinfo,
525 linker_file_t *result)
531 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
532 *result = mod->container;
538 error = linker_load_module(NULL, modname, NULL, verinfo, result);
544 linker_release_module(const char *modname, struct mod_depend *verinfo,
552 KASSERT(modname != NULL,
553 ("linker_release_module: no file or name"));
554 mod = modlist_lookup2(modname, verinfo);
561 KASSERT(modname == NULL && verinfo == NULL,
562 ("linker_release_module: both file and name"));
563 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
569 linker_find_file_by_name(const char *filename)
574 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
575 sprintf(koname, "%s.ko", filename);
577 sx_assert(&kld_sx, SA_XLOCKED);
578 TAILQ_FOREACH(lf, &linker_files, link) {
579 if (strcmp(lf->filename, koname) == 0)
581 if (strcmp(lf->filename, filename) == 0)
584 free(koname, M_LINKER);
589 linker_find_file_by_id(int fileid)
593 sx_assert(&kld_sx, SA_XLOCKED);
594 TAILQ_FOREACH(lf, &linker_files, link)
595 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
601 linker_file_foreach(linker_predicate_t *predicate, void *context)
607 TAILQ_FOREACH(lf, &linker_files, link) {
608 retval = predicate(lf, context);
617 linker_make_file(const char *pathname, linker_class_t lc)
620 const char *filename;
623 sx_assert(&kld_sx, SA_XLOCKED);
624 filename = linker_basename(pathname);
626 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
627 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
637 lf->filename = strdup(filename, M_LINKER);
638 lf->pathname = strdup(pathname, M_LINKER);
639 LINKER_GET_NEXT_FILE_ID(lf->id);
642 lf->loadcnt = ++loadcnt;
647 STAILQ_INIT(&lf->common);
648 TAILQ_INIT(&lf->modules);
649 TAILQ_INSERT_TAIL(&linker_files, lf, link);
654 linker_file_unload(linker_file_t file, int flags)
657 modlist_t ml, nextml;
658 struct common_symbol *cp;
661 /* Refuse to unload modules if securelevel raised. */
662 if (prison0.pr_securelevel > 0)
665 sx_assert(&kld_sx, SA_XLOCKED);
666 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
668 /* Easy case of just dropping a reference. */
669 if (file->refs > 1) {
674 /* Give eventhandlers a chance to prevent the unload. */
676 EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
680 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
681 " informing modules\n"));
684 * Quiesce all the modules to give them a chance to veto the unload.
687 for (mod = TAILQ_FIRST(&file->modules); mod;
688 mod = module_getfnext(mod)) {
689 error = module_quiesce(mod);
690 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
691 KLD_DPF(FILE, ("linker_file_unload: module %s"
692 " vetoed unload\n", module_getname(mod)));
694 * XXX: Do we need to tell all the quiesced modules
695 * that they can resume work now via a new module
705 * Inform any modules associated with this file that they are
709 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
710 next = module_getfnext(mod);
714 * Give the module a chance to veto the unload.
716 if ((error = module_unload(mod)) != 0) {
719 KLD_DPF(FILE, ("linker_file_unload: module %s"
720 " failed unload\n", module_getname(mod)));
730 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
731 if (ml->container == file) {
732 TAILQ_REMOVE(&found_modules, ml, link);
738 * Don't try to run SYSUNINITs if we are unloaded due to a
741 if (file->flags & LINKER_FILE_LINKED) {
742 file->flags &= ~LINKER_FILE_LINKED;
743 linker_file_unregister_sysctls(file);
744 linker_file_sysuninit(file);
746 TAILQ_REMOVE(&linker_files, file, link);
749 for (i = 0; i < file->ndeps; i++)
750 linker_file_unload(file->deps[i], flags);
751 free(file->deps, M_LINKER);
754 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
755 STAILQ_REMOVE_HEAD(&file->common, link);
761 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
764 if (file->filename) {
765 free(file->filename, M_LINKER);
766 file->filename = NULL;
768 if (file->pathname) {
769 free(file->pathname, M_LINKER);
770 file->pathname = NULL;
772 kobj_delete((kobj_t) file, M_LINKER);
777 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
779 return (LINKER_CTF_GET(file, lc));
783 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
785 linker_file_t *newdeps;
787 sx_assert(&kld_sx, SA_XLOCKED);
788 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
789 M_LINKER, M_WAITOK | M_ZERO);
790 file->deps[file->ndeps] = dep;
792 KLD_DPF(FILE, ("linker_file_add_dependency:"
793 " adding %s as dependency for %s\n",
794 dep->filename, file->filename));
799 * Locate a linker set and its contents. This is a helper function to avoid
800 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
801 * This function is used in this file so we can avoid having lots of (void **)
805 linker_file_lookup_set(linker_file_t file, const char *name,
806 void *firstp, void *lastp, int *countp)
809 sx_assert(&kld_sx, SA_LOCKED);
810 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
814 * List all functions in a file.
817 linker_file_function_listall(linker_file_t lf,
818 linker_function_nameval_callback_t callback_func, void *arg)
820 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
824 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
829 locked = sx_xlocked(&kld_sx);
832 sym = linker_file_lookup_symbol_internal(file, name, deps);
839 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
843 linker_symval_t symval;
845 size_t common_size = 0;
848 sx_assert(&kld_sx, SA_XLOCKED);
849 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
852 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
853 LINKER_SYMBOL_VALUES(file, sym, &symval);
854 if (symval.value == 0)
856 * For commons, first look them up in the
857 * dependencies and only allocate space if not found
860 common_size = symval.size;
862 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
863 ".value=%p\n", symval.value));
864 return (symval.value);
868 for (i = 0; i < file->ndeps; i++) {
869 address = linker_file_lookup_symbol_internal(
870 file->deps[i], name, 0);
872 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
873 " deps value=%p\n", address));
878 if (common_size > 0) {
880 * This is a common symbol which was not found in the
881 * dependencies. We maintain a simple common symbol table in
884 struct common_symbol *cp;
886 STAILQ_FOREACH(cp, &file->common, link) {
887 if (strcmp(cp->name, name) == 0) {
888 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
889 " old common value=%p\n", cp->address));
890 return (cp->address);
894 * Round the symbol size up to align.
896 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
897 cp = malloc(sizeof(struct common_symbol)
898 + common_size + strlen(name) + 1, M_LINKER,
900 cp->address = (caddr_t)(cp + 1);
901 cp->name = cp->address + common_size;
902 strcpy(cp->name, name);
903 bzero(cp->address, common_size);
904 STAILQ_INSERT_TAIL(&file->common, cp, link);
906 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
907 " value=%p\n", cp->address));
908 return (cp->address);
910 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
915 * Both DDB and stack(9) rely on the kernel linker to provide forward and
916 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
917 * do this in a lockfree manner. We provide a set of internal helper
918 * routines to perform these operations without locks, and then wrappers that
921 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
925 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
929 TAILQ_FOREACH(lf, &linker_files, link) {
930 if (LINKER_LOOKUP_DEBUG_SYMBOL(lf, symstr, sym) == 0)
938 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
941 c_linker_sym_t best, es;
942 u_long diff, bestdiff, off;
945 off = (uintptr_t)value;
947 TAILQ_FOREACH(lf, &linker_files, link) {
948 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
950 if (es != 0 && diff < bestdiff) {
969 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
973 TAILQ_FOREACH(lf, &linker_files, link) {
974 if (LINKER_DEBUG_SYMBOL_VALUES(lf, sym, symval) == 0)
981 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
984 linker_symval_t symval;
989 error = linker_debug_search_symbol(value, &sym, offset);
992 error = linker_debug_symbol_values(sym, &symval);
995 strlcpy(buf, symval.name, buflen);
1000 * DDB Helpers. DDB has to look across multiple files with their own symbol
1001 * tables and string tables.
1003 * Note that we do not obey list locking protocols here. We really don't need
1004 * DDB to hang because somebody's got the lock held. We'll take the chance
1005 * that the files list is inconsistent instead.
1009 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
1012 return (linker_debug_lookup(symstr, sym));
1017 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
1020 return (linker_debug_search_symbol(value, sym, diffp));
1024 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
1027 return (linker_debug_symbol_values(sym, symval));
1031 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1035 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
1039 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
1040 * obey locking protocols, and offer a significantly less complex interface.
1043 linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen,
1044 long *offset, int flags)
1048 KASSERT((flags & (M_NOWAIT | M_WAITOK)) != 0 &&
1049 (flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
1050 ("%s: bad flags: 0x%x", __func__, flags));
1052 if (flags & M_NOWAIT) {
1053 if (!sx_try_slock(&kld_sx))
1054 return (EWOULDBLOCK);
1058 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
1059 sx_sunlock(&kld_sx);
1064 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1068 return (linker_search_symbol_name_flags(value, buf, buflen, offset,
1073 linker_kldload_busy(int flags)
1077 MPASS((flags & ~(LINKER_UB_UNLOCK | LINKER_UB_LOCKED |
1078 LINKER_UB_PCATCH)) == 0);
1079 if ((flags & LINKER_UB_LOCKED) != 0)
1080 sx_assert(&kld_sx, SA_XLOCKED);
1082 if ((flags & LINKER_UB_LOCKED) == 0)
1084 while (kld_busy > 0) {
1085 if (kld_busy_owner == curthread)
1087 error = sx_sleep(&kld_busy, &kld_sx,
1088 (flags & LINKER_UB_PCATCH) != 0 ? PCATCH : 0,
1091 if ((flags & LINKER_UB_UNLOCK) != 0)
1092 sx_xunlock(&kld_sx);
1097 kld_busy_owner = curthread;
1098 if ((flags & LINKER_UB_UNLOCK) != 0)
1099 sx_xunlock(&kld_sx);
1104 linker_kldload_unbusy(int flags)
1106 MPASS((flags & ~LINKER_UB_LOCKED) == 0);
1107 if ((flags & LINKER_UB_LOCKED) != 0)
1108 sx_assert(&kld_sx, SA_XLOCKED);
1110 if ((flags & LINKER_UB_LOCKED) == 0)
1112 MPASS(kld_busy > 0);
1113 if (kld_busy_owner != curthread)
1114 panic("linker_kldload_unbusy done by not owning thread %p",
1117 if (kld_busy == 0) {
1118 kld_busy_owner = NULL;
1121 sx_xunlock(&kld_sx);
1128 kern_kldload(struct thread *td, const char *file, int *fileid)
1130 const char *kldname, *modname;
1134 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1137 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1141 * If file does not contain a qualified name or any dot in it
1142 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1145 if (strchr(file, '/') || strchr(file, '.')) {
1153 error = linker_kldload_busy(LINKER_UB_PCATCH);
1155 sx_xunlock(&kld_sx);
1160 * It is possible that kldloaded module will attach a new ifnet,
1161 * so vnet context must be set when this ocurs.
1163 CURVNET_SET(TD_TO_VNET(td));
1165 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1173 linker_kldload_unbusy(LINKER_UB_LOCKED);
1178 sys_kldload(struct thread *td, struct kldload_args *uap)
1180 char *pathname = NULL;
1183 td->td_retval[0] = -1;
1185 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1186 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1188 error = kern_kldload(td, pathname, &fileid);
1190 td->td_retval[0] = fileid;
1192 free(pathname, M_TEMP);
1197 kern_kldunload(struct thread *td, int fileid, int flags)
1202 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1205 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1208 error = linker_kldload_busy(LINKER_UB_PCATCH);
1210 sx_xunlock(&kld_sx);
1214 CURVNET_SET(TD_TO_VNET(td));
1215 lf = linker_find_file_by_id(fileid);
1217 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1219 if (lf->userrefs == 0) {
1221 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1223 printf("kldunload: attempt to unload file that was"
1224 " loaded by the kernel\n");
1228 error = linker_file_unload(lf, flags);
1235 linker_kldload_unbusy(LINKER_UB_LOCKED);
1240 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1243 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1247 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1250 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1251 uap->flags != LINKER_UNLOAD_FORCE)
1253 return (kern_kldunload(td, uap->fileid, uap->flags));
1257 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1260 const char *filename;
1265 error = mac_kld_check_stat(td->td_ucred);
1270 td->td_retval[0] = -1;
1272 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1273 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1276 filename = linker_basename(pathname);
1278 lf = linker_find_file_by_name(filename);
1280 td->td_retval[0] = lf->id;
1283 sx_xunlock(&kld_sx);
1285 free(pathname, M_TEMP);
1290 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1296 error = mac_kld_check_stat(td->td_ucred);
1302 if (uap->fileid == 0)
1303 lf = TAILQ_FIRST(&linker_files);
1305 lf = linker_find_file_by_id(uap->fileid);
1310 lf = TAILQ_NEXT(lf, link);
1313 /* Skip partially loaded files. */
1314 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1315 lf = TAILQ_NEXT(lf, link);
1318 td->td_retval[0] = lf->id;
1320 td->td_retval[0] = 0;
1322 sx_xunlock(&kld_sx);
1327 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1329 struct kld_file_stat *stat;
1333 * Check the version of the user's structure.
1335 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1338 if (version != sizeof(struct kld_file_stat_1) &&
1339 version != sizeof(struct kld_file_stat))
1342 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
1343 error = kern_kldstat(td, uap->fileid, stat);
1345 error = copyout(stat, uap->stat, version);
1351 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1358 error = mac_kld_check_stat(td->td_ucred);
1364 lf = linker_find_file_by_id(fileid);
1366 sx_xunlock(&kld_sx);
1370 /* Version 1 fields: */
1371 namelen = strlen(lf->filename) + 1;
1372 if (namelen > sizeof(stat->name))
1373 namelen = sizeof(stat->name);
1374 bcopy(lf->filename, &stat->name[0], namelen);
1375 stat->refs = lf->refs;
1377 stat->address = lf->address;
1378 stat->size = lf->size;
1379 /* Version 2 fields: */
1380 namelen = strlen(lf->pathname) + 1;
1381 if (namelen > sizeof(stat->pathname))
1382 namelen = sizeof(stat->pathname);
1383 bcopy(lf->pathname, &stat->pathname[0], namelen);
1384 sx_xunlock(&kld_sx);
1386 td->td_retval[0] = 0;
1391 DB_COMMAND(kldstat, db_kldstat)
1395 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
1396 db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
1397 #undef POINTER_WIDTH
1398 TAILQ_FOREACH(lf, &linker_files, link) {
1401 db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs,
1402 lf->address, lf->size, lf->filename);
1408 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1415 error = mac_kld_check_stat(td->td_ucred);
1421 lf = linker_find_file_by_id(uap->fileid);
1424 mp = TAILQ_FIRST(&lf->modules);
1426 td->td_retval[0] = module_getid(mp);
1428 td->td_retval[0] = 0;
1432 sx_xunlock(&kld_sx);
1437 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1439 char *symstr = NULL;
1441 linker_symval_t symval;
1443 struct kld_sym_lookup lookup;
1447 error = mac_kld_check_stat(td->td_ucred);
1452 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1454 if (lookup.version != sizeof(lookup) ||
1455 uap->cmd != KLDSYM_LOOKUP)
1457 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1458 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1461 if (uap->fileid != 0) {
1462 lf = linker_find_file_by_id(uap->fileid);
1465 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1466 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1467 lookup.symvalue = (uintptr_t) symval.value;
1468 lookup.symsize = symval.size;
1469 error = copyout(&lookup, uap->data, sizeof(lookup));
1473 TAILQ_FOREACH(lf, &linker_files, link) {
1474 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1475 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1476 lookup.symvalue = (uintptr_t)symval.value;
1477 lookup.symsize = symval.size;
1478 error = copyout(&lookup, uap->data,
1486 sx_xunlock(&kld_sx);
1488 free(symstr, M_TEMP);
1493 * Preloaded module support
1497 modlist_lookup(const char *name, int ver)
1501 TAILQ_FOREACH(mod, &found_modules, link) {
1502 if (strcmp(mod->name, name) == 0 &&
1503 (ver == 0 || mod->version == ver))
1510 modlist_lookup2(const char *name, const struct mod_depend *verinfo)
1512 modlist_t mod, bestmod;
1515 if (verinfo == NULL)
1516 return (modlist_lookup(name, 0));
1518 TAILQ_FOREACH(mod, &found_modules, link) {
1519 if (strcmp(mod->name, name) != 0)
1522 if (ver == verinfo->md_ver_preferred)
1524 if (ver >= verinfo->md_ver_minimum &&
1525 ver <= verinfo->md_ver_maximum &&
1526 (bestmod == NULL || ver > bestmod->version))
1533 modlist_newmodule(const char *modname, int version, linker_file_t container)
1537 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1539 panic("no memory for module list");
1540 mod->container = container;
1541 mod->name = modname;
1542 mod->version = version;
1543 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1548 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1549 struct mod_metadata **stop, int preload)
1551 struct mod_metadata *mp, **mdp;
1552 const char *modname;
1555 for (mdp = start; mdp < stop; mdp++) {
1557 if (mp->md_type != MDT_VERSION)
1559 modname = mp->md_cval;
1560 ver = ((const struct mod_version *)mp->md_data)->mv_version;
1561 if (modlist_lookup(modname, ver) != NULL) {
1562 printf("module %s already present!\n", modname);
1563 /* XXX what can we do? this is a build error. :-( */
1566 modlist_newmodule(modname, ver, lf);
1571 linker_preload(void *arg)
1574 const char *modname, *nmodname;
1576 linker_file_t lf, nlf;
1579 linker_file_list_t loaded_files;
1580 linker_file_list_t depended_files;
1581 struct mod_metadata *mp, *nmp;
1582 struct mod_metadata **start, **stop, **mdp, **nmdp;
1583 const struct mod_depend *verinfo;
1587 struct sysinit **si_start, **si_stop;
1589 TAILQ_INIT(&loaded_files);
1590 TAILQ_INIT(&depended_files);
1591 TAILQ_INIT(&found_modules);
1596 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1597 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1598 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1599 if (modname == NULL) {
1600 printf("Preloaded module at %p does not have a"
1601 " name!\n", modptr);
1604 if (modtype == NULL) {
1605 printf("Preloaded module at %p does not have a type!\n",
1610 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1613 TAILQ_FOREACH(lc, &classes, link) {
1614 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1620 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1624 * First get a list of stuff in the kernel.
1626 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1628 linker_addmodules(linker_kernel_file, start, stop, 1);
1631 * This is a once-off kinky bubble sort to resolve relocation
1632 * dependency requirements.
1635 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1636 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1639 * First, look to see if we would successfully link with this
1642 resolves = 1; /* unless we know otherwise */
1644 for (mdp = start; mdp < stop; mdp++) {
1646 if (mp->md_type != MDT_DEPEND)
1648 modname = mp->md_cval;
1649 verinfo = mp->md_data;
1650 for (nmdp = start; nmdp < stop; nmdp++) {
1652 if (nmp->md_type != MDT_VERSION)
1654 nmodname = nmp->md_cval;
1655 if (strcmp(modname, nmodname) == 0)
1658 if (nmdp < stop) /* it's a self reference */
1662 * ok, the module isn't here yet, we
1665 if (modlist_lookup2(modname, verinfo) == NULL)
1670 * OK, if we found our modules, we can link. So, "provide"
1671 * the modules inside and add it to the end of the link order
1676 for (mdp = start; mdp < stop; mdp++) {
1678 if (mp->md_type != MDT_VERSION)
1680 modname = mp->md_cval;
1681 nver = ((const struct mod_version *)
1682 mp->md_data)->mv_version;
1683 if (modlist_lookup(modname,
1685 printf("module %s already"
1686 " present!\n", modname);
1687 TAILQ_REMOVE(&loaded_files,
1689 linker_file_unload(lf,
1690 LINKER_UNLOAD_FORCE);
1691 /* we changed tailq next ptr */
1694 modlist_newmodule(modname, nver, lf);
1697 TAILQ_REMOVE(&loaded_files, lf, loaded);
1698 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1700 * Since we provided modules, we need to restart the
1701 * sort so that the previous files that depend on us
1702 * have a chance. Also, we've busted the tailq next
1703 * pointer with the REMOVE.
1710 * At this point, we check to see what could not be resolved..
1712 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1713 TAILQ_REMOVE(&loaded_files, lf, loaded);
1714 printf("KLD file %s is missing dependencies\n", lf->filename);
1715 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1719 * We made it. Finish off the linking in the order we determined.
1721 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1722 if (linker_kernel_file) {
1723 linker_kernel_file->refs++;
1724 error = linker_file_add_dependency(lf,
1725 linker_kernel_file);
1727 panic("cannot add dependency");
1729 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1732 for (mdp = start; mdp < stop; mdp++) {
1734 if (mp->md_type != MDT_DEPEND)
1736 modname = mp->md_cval;
1737 verinfo = mp->md_data;
1738 mod = modlist_lookup2(modname, verinfo);
1740 printf("KLD file %s - cannot find "
1741 "dependency \"%s\"\n",
1742 lf->filename, modname);
1745 /* Don't count self-dependencies */
1746 if (lf == mod->container)
1748 mod->container->refs++;
1749 error = linker_file_add_dependency(lf,
1752 panic("cannot add dependency");
1756 * Now do relocation etc using the symbol search paths
1757 * established by the dependencies
1759 error = LINKER_LINK_PRELOAD_FINISH(lf);
1761 printf("KLD file %s - could not finalize loading\n",
1765 linker_file_register_modules(lf);
1766 if (!TAILQ_EMPTY(&lf->modules))
1767 lf->flags |= LINKER_FILE_MODULES;
1768 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1769 &si_stop, NULL) == 0)
1770 sysinit_add(si_start, si_stop);
1771 linker_file_register_sysctls(lf, true);
1772 lf->flags |= LINKER_FILE_LINKED;
1775 TAILQ_REMOVE(&depended_files, lf, loaded);
1776 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1778 sx_xunlock(&kld_sx);
1779 /* woohoo! we made it! */
1782 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, NULL);
1785 * Handle preload files that failed to load any modules.
1788 linker_preload_finish(void *arg)
1790 linker_file_t lf, nlf;
1793 TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) {
1795 * If all of the modules in this file failed to load, unload
1796 * the file and return an error of ENOEXEC. (Parity with
1797 * linker_load_file.)
1799 if ((lf->flags & LINKER_FILE_MODULES) != 0 &&
1800 TAILQ_EMPTY(&lf->modules)) {
1801 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1805 lf->flags &= ~LINKER_FILE_MODULES;
1806 lf->userrefs++; /* so we can (try to) kldunload it */
1808 sx_xunlock(&kld_sx);
1812 * Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be
1813 * scheduled at any subsystem and order, so run this as late as possible. init
1814 * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that.
1816 SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE,
1817 linker_preload_finish, NULL);
1820 * Search for a not-loaded module by name.
1822 * Modules may be found in the following locations:
1824 * - preloaded (result is just the module name) - on disk (result is full path
1827 * If the module name is qualified in any way (contains path, etc.) the we
1828 * simply return a copy of it.
1830 * The search path can be manipulated via sysctl. Note that we use the ';'
1831 * character as a separator to be consistent with the bootloader.
1834 static char linker_hintfile[] = "linker.hints";
1835 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1837 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path,
1838 sizeof(linker_path), "module load search path");
1840 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1842 static const char * const linker_ext_list[] = {
1849 * Check if file actually exists either with or without extension listed in
1850 * the linker_ext_list. (probably should be generic for the rest of the
1854 linker_lookup_file(const char *path, int pathlen, const char *name,
1855 int namelen, struct vattr *vap)
1857 struct nameidata nd;
1858 struct thread *td = curthread; /* XXX */
1859 const char * const *cpp, *sep;
1861 int error, len, extlen, reclen, flags;
1865 for (cpp = linker_ext_list; *cpp; cpp++) {
1870 extlen++; /* trailing '\0' */
1871 sep = (path[pathlen - 1] != '/') ? "/" : "";
1873 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1874 result = malloc(reclen, M_LINKER, M_WAITOK);
1875 for (cpp = linker_ext_list; *cpp; cpp++) {
1876 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1877 namelen, name, *cpp);
1879 * Attempt to open the file, and return the path if
1880 * we succeed and it's a regular file.
1882 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result);
1884 error = vn_open(&nd, &flags, 0, NULL);
1886 NDFREE(&nd, NDF_ONLY_PNBUF);
1887 type = nd.ni_vp->v_type;
1889 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1890 VOP_UNLOCK(nd.ni_vp);
1891 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1896 free(result, M_LINKER);
1900 #define INT_ALIGN(base, ptr) ptr = \
1901 (base) + roundup2((ptr) - (base), sizeof(int))
1904 * Lookup KLD which contains requested module in the "linker.hints" file. If
1905 * version specification is available, then try to find the best KLD.
1906 * Otherwise just find the latest one.
1909 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1910 int modnamelen, const struct mod_depend *verinfo)
1912 struct thread *td = curthread; /* XXX */
1913 struct ucred *cred = td ? td->td_ucred : NULL;
1914 struct nameidata nd;
1915 struct vattr vattr, mattr;
1916 const char *best, *sep;
1917 u_char *hints = NULL;
1918 u_char *cp, *recptr, *bufend, *result, *pathbuf;
1919 int error, ival, bestver, *intp, found, flags, clen, blen;
1923 bestver = found = 0;
1925 sep = (path[pathlen - 1] != '/') ? "/" : "";
1926 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1928 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1929 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1932 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf);
1934 error = vn_open(&nd, &flags, 0, NULL);
1937 NDFREE(&nd, NDF_ONLY_PNBUF);
1938 if (nd.ni_vp->v_type != VREG)
1941 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1945 * XXX: we need to limit this number to some reasonable value
1947 if (vattr.va_size > LINKER_HINTS_MAX) {
1948 printf("linker.hints file too large %ld\n", (long)vattr.va_size);
1951 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1952 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1953 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1956 VOP_UNLOCK(nd.ni_vp);
1957 vn_close(nd.ni_vp, FREAD, cred, td);
1960 printf("can't read %zd\n", reclen);
1963 intp = (int *)hints;
1965 if (ival != LINKER_HINTS_VERSION) {
1966 printf("linker.hints file version mismatch %d\n", ival);
1969 bufend = hints + vattr.va_size;
1970 recptr = (u_char *)intp;
1972 while (recptr < bufend && !found) {
1973 intp = (int *)recptr;
1980 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1983 INT_ALIGN(hints, cp);
1987 if (verinfo == NULL ||
1988 ival == verinfo->md_ver_preferred) {
1992 if (ival >= verinfo->md_ver_minimum &&
1993 ival <= verinfo->md_ver_maximum &&
2003 recptr += reclen + sizeof(int);
2006 * Finally check if KLD is in the place
2009 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
2011 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
2014 * KLD is newer than hints file. What we should do now?
2016 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
2017 printf("warning: KLD '%s' is newer than the linker.hints"
2020 free(pathbuf, M_LINKER);
2022 free(hints, M_TEMP);
2023 if (nd.ni_vp != NULL) {
2024 VOP_UNLOCK(nd.ni_vp);
2025 vn_close(nd.ni_vp, FREAD, cred, td);
2028 * If nothing found or hints is absent - fallback to the old
2029 * way by using "kldname[.ko]" as module name.
2031 if (!found && !bestver && result == NULL)
2032 result = linker_lookup_file(path, pathlen, modname,
2038 * Lookup KLD which contains requested module in the all directories.
2041 linker_search_module(const char *modname, int modnamelen,
2042 const struct mod_depend *verinfo)
2044 char *cp, *ep, *result;
2047 * traverse the linker path
2049 for (cp = linker_path; *cp; cp = ep + 1) {
2050 /* find the end of this component */
2051 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
2052 result = linker_hints_lookup(cp, ep - cp, modname,
2053 modnamelen, verinfo);
2063 * Search for module in all directories listed in the linker_path.
2066 linker_search_kld(const char *name)
2068 char *cp, *ep, *result;
2071 /* qualified at all? */
2072 if (strchr(name, '/'))
2073 return (strdup(name, M_LINKER));
2075 /* traverse the linker path */
2077 for (ep = linker_path; *ep; ep++) {
2079 /* find the end of this component */
2080 for (; *ep != 0 && *ep != ';'; ep++);
2081 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
2089 linker_basename(const char *path)
2091 const char *filename;
2093 filename = strrchr(path, '/');
2094 if (filename == NULL)
2103 * Inform hwpmc about the set of kernel modules currently loaded.
2106 linker_hwpmc_list_objects(void)
2109 struct pmckern_map_in *kobase;
2114 TAILQ_FOREACH(lf, &linker_files, link)
2117 /* Allocate nmappings + 1 entries. */
2118 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
2119 M_LINKER, M_WAITOK | M_ZERO);
2121 TAILQ_FOREACH(lf, &linker_files, link) {
2122 /* Save the info for this linker file. */
2123 kobase[i].pm_file = lf->filename;
2124 kobase[i].pm_address = (uintptr_t)lf->address;
2127 sx_sunlock(&kld_sx);
2129 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
2131 /* The last entry of the malloced area comprises of all zeros. */
2132 KASSERT(kobase[i].pm_file == NULL,
2133 ("linker_hwpmc_list_objects: last object not NULL"));
2135 return ((void *)kobase);
2139 /* check if root file system is not mounted */
2141 linker_root_mounted(void)
2146 if (rootvnode == NULL)
2149 pwd = pwd_hold(curthread);
2150 ret = pwd->pwd_rdir != NULL;
2156 * Find a file which contains given module and load it, if "parent" is not
2157 * NULL, register a reference to it.
2160 linker_load_module(const char *kldname, const char *modname,
2161 struct linker_file *parent, const struct mod_depend *verinfo,
2162 struct linker_file **lfpp)
2164 linker_file_t lfdep;
2165 const char *filename;
2169 sx_assert(&kld_sx, SA_XLOCKED);
2170 if (modname == NULL) {
2172 * We have to load KLD
2174 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2176 if (!linker_root_mounted())
2178 pathname = linker_search_kld(kldname);
2180 if (modlist_lookup2(modname, verinfo) != NULL)
2182 if (!linker_root_mounted())
2184 if (kldname != NULL)
2185 pathname = strdup(kldname, M_LINKER);
2188 * Need to find a KLD with required module
2190 pathname = linker_search_module(modname,
2191 strlen(modname), verinfo);
2193 if (pathname == NULL)
2197 * Can't load more than one file with the same basename XXX:
2198 * Actually it should be possible to have multiple KLDs with
2199 * the same basename but different path because they can
2200 * provide different versions of the same modules.
2202 filename = linker_basename(pathname);
2203 if (linker_find_file_by_name(filename))
2206 error = linker_load_file(pathname, &lfdep);
2209 if (modname && verinfo &&
2210 modlist_lookup2(modname, verinfo) == NULL) {
2211 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2216 error = linker_file_add_dependency(parent, lfdep);
2223 free(pathname, M_LINKER);
2228 * This routine is responsible for finding dependencies of userland initiated
2229 * kldload(2)'s of files.
2232 linker_load_dependencies(linker_file_t lf)
2234 linker_file_t lfdep;
2235 struct mod_metadata **start, **stop, **mdp, **nmdp;
2236 struct mod_metadata *mp, *nmp;
2237 const struct mod_depend *verinfo;
2239 const char *modname, *nmodname;
2243 * All files are dependent on /kernel.
2245 sx_assert(&kld_sx, SA_XLOCKED);
2246 if (linker_kernel_file) {
2247 linker_kernel_file->refs++;
2248 error = linker_file_add_dependency(lf, linker_kernel_file);
2252 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2255 for (mdp = start; mdp < stop; mdp++) {
2257 if (mp->md_type != MDT_VERSION)
2259 modname = mp->md_cval;
2260 ver = ((const struct mod_version *)mp->md_data)->mv_version;
2261 mod = modlist_lookup(modname, ver);
2263 printf("interface %s.%d already present in the KLD"
2264 " '%s'!\n", modname, ver,
2265 mod->container->filename);
2270 for (mdp = start; mdp < stop; mdp++) {
2272 if (mp->md_type != MDT_DEPEND)
2274 modname = mp->md_cval;
2275 verinfo = mp->md_data;
2277 for (nmdp = start; nmdp < stop; nmdp++) {
2279 if (nmp->md_type != MDT_VERSION)
2281 nmodname = nmp->md_cval;
2282 if (strcmp(modname, nmodname) == 0)
2285 if (nmdp < stop)/* early exit, it's a self reference */
2287 mod = modlist_lookup2(modname, verinfo);
2288 if (mod) { /* woohoo, it's loaded already */
2289 lfdep = mod->container;
2291 error = linker_file_add_dependency(lf, lfdep);
2296 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2298 printf("KLD %s: depends on %s - not available or"
2299 " version mismatch\n", lf->filename, modname);
2306 linker_addmodules(lf, start, stop, 0);
2311 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2313 struct sysctl_req *req;
2316 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2320 * Export a nul-separated, double-nul-terminated list of all function names
2324 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2330 error = mac_kld_check_stat(req->td->td_ucred);
2334 error = sysctl_wire_old_buffer(req, 0);
2338 TAILQ_FOREACH(lf, &linker_files, link) {
2339 error = LINKER_EACH_FUNCTION_NAME(lf,
2340 sysctl_kern_function_list_iterate, req);
2342 sx_xunlock(&kld_sx);
2346 sx_xunlock(&kld_sx);
2347 return (SYSCTL_OUT(req, "", 1));
2350 SYSCTL_PROC(_kern, OID_AUTO, function_list,
2351 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
2352 sysctl_kern_function_list, "",
2353 "kernel function list");