2 * SPDX-License-Identifier: BSD-2-Clause
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>
32 #include "opt_hwpmc_hooks.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/boottrace.h>
37 #include <sys/eventhandler.h>
38 #include <sys/fcntl.h>
40 #include <sys/kernel.h>
41 #include <sys/libkern.h>
42 #include <sys/linker.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mount.h>
47 #include <sys/mutex.h>
48 #include <sys/namei.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysproto.h>
55 #include <sys/vnode.h>
63 #include <security/mac/mac_framework.h>
65 #include "linker_if.h"
68 #include <sys/pmckern.h>
73 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RWTUN,
74 &kld_debug, 0, "Set various levels of KLD debug");
77 /* These variables are used by kernel debuggers to enumerate loaded files. */
78 const int kld_off_address = offsetof(struct linker_file, address);
79 const int kld_off_filename = offsetof(struct linker_file, filename);
80 const int kld_off_pathname = offsetof(struct linker_file, pathname);
81 const int kld_off_next = offsetof(struct linker_file, link.tqe_next);
84 * static char *linker_search_path(const char *name, struct mod_depend
87 static const char *linker_basename(const char *path);
90 * Find a currently loaded file given its filename.
92 static linker_file_t linker_find_file_by_name(const char* _filename);
95 * Find a currently loaded file given its file id.
97 static linker_file_t linker_find_file_by_id(int _fileid);
99 /* Metadata from the static kernel */
100 SET_DECLARE(modmetadata_set, struct mod_metadata);
102 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
104 linker_file_t linker_kernel_file;
106 static struct sx kld_sx; /* kernel linker lock */
107 static u_int kld_busy;
108 static struct thread *kld_busy_owner;
111 * Load counter used by clients to determine if a linker file has been
112 * re-loaded. This counter is incremented for each file load.
116 static linker_class_list_t classes;
117 static linker_file_list_t linker_files;
118 static int next_file_id = 1;
119 static int linker_no_more_classes = 0;
121 #define LINKER_GET_NEXT_FILE_ID(a) do { \
122 linker_file_t lftmp; \
125 sx_assert(&kld_sx, SA_XLOCKED); \
127 TAILQ_FOREACH(lftmp, &linker_files, link) { \
128 if (next_file_id == lftmp->id) { \
133 (a) = next_file_id; \
136 /* XXX wrong name; we're looking at version provision tags here, not modules */
137 typedef TAILQ_HEAD(, modlist) modlisthead_t;
139 TAILQ_ENTRY(modlist) link; /* chain together all modules */
140 linker_file_t container;
144 typedef struct modlist *modlist_t;
145 static modlisthead_t found_modules;
147 static void linker_file_add_dependency(linker_file_t file,
149 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
150 const char* name, int deps);
151 static int linker_load_module(const char *kldname,
152 const char *modname, struct linker_file *parent,
153 const struct mod_depend *verinfo, struct linker_file **lfpp);
154 static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo);
157 linker_init(void *arg)
160 sx_init(&kld_sx, "kernel linker");
161 TAILQ_INIT(&classes);
162 TAILQ_INIT(&linker_files);
165 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, NULL);
168 linker_stop_class_add(void *arg)
171 linker_no_more_classes = 1;
174 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
177 linker_add_class(linker_class_t lc)
181 * We disallow any class registration past SI_ORDER_ANY
182 * of SI_SUB_KLD. We bump the reference count to keep the
183 * ops from being freed.
185 if (linker_no_more_classes == 1)
187 kobj_class_compile((kobj_class_t) lc);
188 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
189 TAILQ_INSERT_TAIL(&classes, lc, link);
194 linker_file_sysinit(linker_file_t lf)
196 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.
232 for (sipp = start; sipp < stop; sipp++) {
233 if ((*sipp)->subsystem == SI_SUB_DUMMY)
234 continue; /* skip dummy task(s) */
236 if ((*sipp)->subsystem > last)
237 BOOTTRACE("%s: sysinit 0x%7x", lf->filename,
241 (*((*sipp)->func)) ((*sipp)->udata);
242 last = (*sipp)->subsystem;
249 linker_file_sysuninit(linker_file_t lf)
251 struct sysinit **start, **stop, **sipp, **xipp, *save;
254 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
257 sx_assert(&kld_sx, SA_XLOCKED);
259 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
264 * Perform a reverse bubble sort of the system initialization objects
265 * by their subsystem (primary key) and order (secondary key).
267 * Since some things care about execution order, this is the operation
268 * which ensures continued function.
270 for (sipp = start; sipp < stop; sipp++) {
271 for (xipp = sipp + 1; xipp < stop; xipp++) {
272 if ((*sipp)->subsystem > (*xipp)->subsystem ||
273 ((*sipp)->subsystem == (*xipp)->subsystem &&
274 (*sipp)->order >= (*xipp)->order))
283 * Traverse the (now) ordered list of system initialization tasks.
284 * Perform each task, and continue on to the next task.
289 for (sipp = start; sipp < stop; sipp++) {
290 if ((*sipp)->subsystem == SI_SUB_DUMMY)
291 continue; /* skip dummy task(s) */
293 if ((*sipp)->subsystem > last)
294 BOOTTRACE("%s: sysuninit 0x%7x", lf->filename,
298 (*((*sipp)->func)) ((*sipp)->udata);
299 last = (*sipp)->subsystem;
306 linker_file_register_sysctls(linker_file_t lf, bool enable)
308 struct sysctl_oid **start, **stop, **oidp;
311 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
314 sx_assert(&kld_sx, SA_XLOCKED);
316 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
321 for (oidp = start; oidp < stop; oidp++) {
323 sysctl_register_oid(*oidp);
325 sysctl_register_disabled_oid(*oidp);
332 * Invoke the LINKER_CTF_GET implementation for this file. Existing
333 * implementations will load CTF info from the filesystem upon the first call
334 * and cache it in the kernel thereafter.
337 linker_ctf_load_file(linker_file_t file)
342 error = linker_ctf_get(file, &lc);
346 printf("failed to load CTF for %s: %d\n", file->filename,
352 linker_file_enable_sysctls(linker_file_t lf)
354 struct sysctl_oid **start, **stop, **oidp;
357 ("linker_file_enable_sysctls: enable SYSCTLs for %s\n",
360 sx_assert(&kld_sx, SA_XLOCKED);
362 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
367 for (oidp = start; oidp < stop; oidp++)
368 sysctl_enable_oid(*oidp);
374 linker_file_unregister_sysctls(linker_file_t lf)
376 struct sysctl_oid **start, **stop, **oidp;
378 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
379 " for %s\n", lf->filename));
381 sx_assert(&kld_sx, SA_XLOCKED);
383 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
388 for (oidp = start; oidp < stop; oidp++)
389 sysctl_unregister_oid(*oidp);
395 linker_file_register_modules(linker_file_t lf)
397 struct mod_metadata **start, **stop, **mdp;
398 const moduledata_t *moddata;
399 int first_error, error;
401 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
402 " in %s\n", lf->filename));
404 sx_assert(&kld_sx, SA_XLOCKED);
406 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL) != 0) {
408 * This fallback should be unnecessary, but if we get booted
409 * from boot2 instead of loader and we are missing our
410 * metadata then we have to try the best we can.
412 if (lf == linker_kernel_file) {
413 start = SET_BEGIN(modmetadata_set);
414 stop = SET_LIMIT(modmetadata_set);
419 for (mdp = start; mdp < stop; mdp++) {
420 if ((*mdp)->md_type != MDT_MODULE)
422 moddata = (*mdp)->md_data;
423 KLD_DPF(FILE, ("Registering module %s in %s\n",
424 moddata->name, lf->filename));
425 error = module_register(moddata, lf);
427 printf("Module %s failed to register: %d\n",
428 moddata->name, error);
429 if (first_error == 0)
433 return (first_error);
437 linker_init_kernel_modules(void)
441 linker_file_register_modules(linker_kernel_file);
445 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
449 linker_load_file(const char *filename, linker_file_t *result)
453 int foundfile, error, modules;
455 /* Refuse to load modules if securelevel raised */
456 if (prison0.pr_securelevel > 0)
459 sx_assert(&kld_sx, SA_XLOCKED);
460 lf = linker_find_file_by_name(filename);
462 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
463 " incrementing refs\n", filename));
472 * We do not need to protect (lock) classes here because there is
473 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
474 * and there is no class deregistration mechanism at this time.
476 TAILQ_FOREACH(lc, &classes, link) {
477 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
479 error = LINKER_LOAD_FILE(lc, filename, &lf);
481 * If we got something other than ENOENT, then it exists but
482 * we cannot load it for some other reason.
484 if (error != ENOENT) {
490 error = linker_file_register_modules(lf);
491 if (error == EEXIST) {
492 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
495 modules = !TAILQ_EMPTY(&lf->modules);
496 linker_file_register_sysctls(lf, false);
498 LINKER_PROPAGATE_VNETS(lf);
500 linker_file_sysinit(lf);
501 lf->flags |= LINKER_FILE_LINKED;
504 * If all of the modules in this file failed
505 * to load, unload the file and return an
508 if (modules && TAILQ_EMPTY(&lf->modules)) {
509 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
512 linker_file_enable_sysctls(lf);
515 * Ask the linker to load CTF data for this file.
517 linker_ctf_load_file(lf);
518 EVENTHANDLER_INVOKE(kld_load, lf);
524 * Less than ideal, but tells the user whether it failed to load or
525 * the module was not found.
529 * If the file type has not been recognized by the last try
530 * printout a message before to fail.
533 printf("%s: %s - unsupported file type\n",
537 * Format not recognized or otherwise unloadable.
538 * When loading a module that is statically built into
539 * the kernel EEXIST percolates back up as the return
540 * value. Preserve this so that apps like sysinstall
541 * can recognize this special case and not post bogus
547 error = ENOENT; /* Nothing found */
552 linker_reference_module(const char *modname, struct mod_depend *verinfo,
553 linker_file_t *result)
559 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
560 *result = mod->container;
566 error = linker_load_module(NULL, modname, NULL, verinfo, result);
572 linker_release_module(const char *modname, struct mod_depend *verinfo,
580 KASSERT(modname != NULL,
581 ("linker_release_module: no file or name"));
582 mod = modlist_lookup2(modname, verinfo);
589 KASSERT(modname == NULL && verinfo == NULL,
590 ("linker_release_module: both file and name"));
591 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
597 linker_find_file_by_name(const char *filename)
602 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
603 sprintf(koname, "%s.ko", filename);
605 sx_assert(&kld_sx, SA_XLOCKED);
606 TAILQ_FOREACH(lf, &linker_files, link) {
607 if (strcmp(lf->filename, koname) == 0)
609 if (strcmp(lf->filename, filename) == 0)
612 free(koname, M_LINKER);
617 linker_find_file_by_id(int fileid)
621 sx_assert(&kld_sx, SA_XLOCKED);
622 TAILQ_FOREACH(lf, &linker_files, link)
623 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
629 linker_file_foreach(linker_predicate_t *predicate, void *context)
635 TAILQ_FOREACH(lf, &linker_files, link) {
636 retval = predicate(lf, context);
645 linker_make_file(const char *pathname, linker_class_t lc)
648 const char *filename;
651 sx_assert(&kld_sx, SA_XLOCKED);
652 filename = linker_basename(pathname);
654 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
655 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
660 lf->ctors_invoked = LF_NONE;
666 lf->filename = strdup(filename, M_LINKER);
667 lf->pathname = strdup(pathname, M_LINKER);
668 LINKER_GET_NEXT_FILE_ID(lf->id);
671 lf->loadcnt = ++loadcnt;
676 STAILQ_INIT(&lf->common);
677 TAILQ_INIT(&lf->modules);
678 TAILQ_INSERT_TAIL(&linker_files, lf, link);
683 linker_file_unload(linker_file_t file, int flags)
686 modlist_t ml, nextml;
687 struct common_symbol *cp;
690 /* Refuse to unload modules if securelevel raised. */
691 if (prison0.pr_securelevel > 0)
694 sx_assert(&kld_sx, SA_XLOCKED);
695 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
697 /* Easy case of just dropping a reference. */
698 if (file->refs > 1) {
703 /* Give eventhandlers a chance to prevent the unload. */
705 EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
709 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
710 " informing modules\n"));
713 * Quiesce all the modules to give them a chance to veto the unload.
716 for (mod = TAILQ_FIRST(&file->modules); mod;
717 mod = module_getfnext(mod)) {
718 error = module_quiesce(mod);
719 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
720 KLD_DPF(FILE, ("linker_file_unload: module %s"
721 " vetoed unload\n", module_getname(mod)));
723 * XXX: Do we need to tell all the quiesced modules
724 * that they can resume work now via a new module
734 * Inform any modules associated with this file that they are
738 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
739 next = module_getfnext(mod);
743 * Give the module a chance to veto the unload.
745 if ((error = module_unload(mod)) != 0) {
748 KLD_DPF(FILE, ("linker_file_unload: module %s"
749 " failed unload\n", module_getname(mod)));
759 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
760 if (ml->container == file) {
761 TAILQ_REMOVE(&found_modules, ml, link);
767 * Don't try to run SYSUNINITs if we are unloaded due to a
770 if (file->flags & LINKER_FILE_LINKED) {
771 file->flags &= ~LINKER_FILE_LINKED;
772 linker_file_unregister_sysctls(file);
773 linker_file_sysuninit(file);
775 TAILQ_REMOVE(&linker_files, file, link);
778 for (i = 0; i < file->ndeps; i++)
779 linker_file_unload(file->deps[i], flags);
780 free(file->deps, M_LINKER);
783 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
784 STAILQ_REMOVE_HEAD(&file->common, link);
790 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
793 if (file->filename) {
794 free(file->filename, M_LINKER);
795 file->filename = NULL;
797 if (file->pathname) {
798 free(file->pathname, M_LINKER);
799 file->pathname = NULL;
801 kobj_delete((kobj_t) file, M_LINKER);
806 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
808 return (LINKER_CTF_GET(file, lc));
812 linker_ctf_lookup_typename_ddb(linker_ctf_t *lc, const char *typename)
817 TAILQ_FOREACH (lf, &linker_files, link){
818 if (LINKER_CTF_LOOKUP_TYPENAME(lf, lc, typename) == 0)
826 linker_ctf_lookup_sym_ddb(const char *symname, c_linker_sym_t *sym,
832 TAILQ_FOREACH (lf, &linker_files, link){
833 if (LINKER_LOOKUP_DEBUG_SYMBOL_CTF(lf, symname, sym, lc) == 0)
841 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
843 linker_file_t *newdeps;
845 sx_assert(&kld_sx, SA_XLOCKED);
846 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
847 M_LINKER, M_WAITOK | M_ZERO);
848 file->deps[file->ndeps] = dep;
850 KLD_DPF(FILE, ("linker_file_add_dependency:"
851 " adding %s as dependency for %s\n",
852 dep->filename, file->filename));
856 * Locate a linker set and its contents. This is a helper function to avoid
857 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
858 * This function is used in this file so we can avoid having lots of (void **)
862 linker_file_lookup_set(linker_file_t file, const char *name,
863 void *firstp, void *lastp, int *countp)
866 sx_assert(&kld_sx, SA_LOCKED);
867 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
871 * List all functions in a file.
874 linker_file_function_listall(linker_file_t lf,
875 linker_function_nameval_callback_t callback_func, void *arg)
877 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
881 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
886 locked = sx_xlocked(&kld_sx);
889 sym = linker_file_lookup_symbol_internal(file, name, deps);
896 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
900 linker_symval_t symval;
902 size_t common_size = 0;
905 sx_assert(&kld_sx, SA_XLOCKED);
906 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
909 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
910 LINKER_SYMBOL_VALUES(file, sym, &symval);
911 if (symval.value == 0)
913 * For commons, first look them up in the
914 * dependencies and only allocate space if not found
917 common_size = symval.size;
919 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
920 ".value=%p\n", symval.value));
921 return (symval.value);
925 for (i = 0; i < file->ndeps; i++) {
926 address = linker_file_lookup_symbol_internal(
927 file->deps[i], name, 0);
929 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
930 " deps value=%p\n", address));
935 if (common_size > 0) {
937 * This is a common symbol which was not found in the
938 * dependencies. We maintain a simple common symbol table in
941 struct common_symbol *cp;
943 STAILQ_FOREACH(cp, &file->common, link) {
944 if (strcmp(cp->name, name) == 0) {
945 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
946 " old common value=%p\n", cp->address));
947 return (cp->address);
951 * Round the symbol size up to align.
953 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
954 cp = malloc(sizeof(struct common_symbol)
955 + common_size + strlen(name) + 1, M_LINKER,
957 cp->address = (caddr_t)(cp + 1);
958 cp->name = cp->address + common_size;
959 strcpy(cp->name, name);
960 bzero(cp->address, common_size);
961 STAILQ_INSERT_TAIL(&file->common, cp, link);
963 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
964 " value=%p\n", cp->address));
965 return (cp->address);
967 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
972 * Both DDB and stack(9) rely on the kernel linker to provide forward and
973 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
974 * do this in a lockfree manner. We provide a set of internal helper
975 * routines to perform these operations without locks, and then wrappers that
978 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
982 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
986 TAILQ_FOREACH(lf, &linker_files, link) {
987 if (LINKER_LOOKUP_DEBUG_SYMBOL(lf, symstr, sym) == 0)
995 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
998 c_linker_sym_t best, es;
999 u_long diff, bestdiff, off;
1002 off = (uintptr_t)value;
1004 TAILQ_FOREACH(lf, &linker_files, link) {
1005 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
1007 if (es != 0 && diff < bestdiff) {
1026 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
1030 TAILQ_FOREACH(lf, &linker_files, link) {
1031 if (LINKER_DEBUG_SYMBOL_VALUES(lf, sym, symval) == 0)
1038 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1041 linker_symval_t symval;
1046 error = linker_debug_search_symbol(value, &sym, offset);
1049 error = linker_debug_symbol_values(sym, &symval);
1052 strlcpy(buf, symval.name, buflen);
1057 * DDB Helpers. DDB has to look across multiple files with their own symbol
1058 * tables and string tables.
1060 * Note that we do not obey list locking protocols here. We really don't need
1061 * DDB to hang because somebody's got the lock held. We'll take the chance
1062 * that the files list is inconsistent instead.
1066 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
1069 return (linker_debug_lookup(symstr, sym));
1074 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
1077 return (linker_debug_search_symbol(value, sym, diffp));
1081 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
1084 return (linker_debug_symbol_values(sym, symval));
1088 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1092 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
1096 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
1097 * obey locking protocols, and offer a significantly less complex interface.
1100 linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen,
1101 long *offset, int flags)
1105 KASSERT((flags & (M_NOWAIT | M_WAITOK)) != 0 &&
1106 (flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
1107 ("%s: bad flags: 0x%x", __func__, flags));
1109 if (flags & M_NOWAIT) {
1110 if (!sx_try_slock(&kld_sx))
1111 return (EWOULDBLOCK);
1115 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
1116 sx_sunlock(&kld_sx);
1121 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1125 return (linker_search_symbol_name_flags(value, buf, buflen, offset,
1130 linker_kldload_busy(int flags)
1134 MPASS((flags & ~(LINKER_UB_UNLOCK | LINKER_UB_LOCKED |
1135 LINKER_UB_PCATCH)) == 0);
1136 if ((flags & LINKER_UB_LOCKED) != 0)
1137 sx_assert(&kld_sx, SA_XLOCKED);
1139 if ((flags & LINKER_UB_LOCKED) == 0)
1141 while (kld_busy > 0) {
1142 if (kld_busy_owner == curthread)
1144 error = sx_sleep(&kld_busy, &kld_sx,
1145 (flags & LINKER_UB_PCATCH) != 0 ? PCATCH : 0,
1148 if ((flags & LINKER_UB_UNLOCK) != 0)
1149 sx_xunlock(&kld_sx);
1154 kld_busy_owner = curthread;
1155 if ((flags & LINKER_UB_UNLOCK) != 0)
1156 sx_xunlock(&kld_sx);
1161 linker_kldload_unbusy(int flags)
1163 MPASS((flags & ~LINKER_UB_LOCKED) == 0);
1164 if ((flags & LINKER_UB_LOCKED) != 0)
1165 sx_assert(&kld_sx, SA_XLOCKED);
1167 if ((flags & LINKER_UB_LOCKED) == 0)
1169 MPASS(kld_busy > 0);
1170 if (kld_busy_owner != curthread)
1171 panic("linker_kldload_unbusy done by not owning thread %p",
1174 if (kld_busy == 0) {
1175 kld_busy_owner = NULL;
1178 sx_xunlock(&kld_sx);
1185 kern_kldload(struct thread *td, const char *file, int *fileid)
1187 const char *kldname, *modname;
1191 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1194 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1198 * If file does not contain a qualified name or any dot in it
1199 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1202 if (strchr(file, '/') || strchr(file, '.')) {
1210 error = linker_kldload_busy(LINKER_UB_PCATCH);
1212 sx_xunlock(&kld_sx);
1217 * It is possible that kldloaded module will attach a new ifnet,
1218 * so vnet context must be set when this ocurs.
1220 CURVNET_SET(TD_TO_VNET(td));
1222 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1230 linker_kldload_unbusy(LINKER_UB_LOCKED);
1235 sys_kldload(struct thread *td, struct kldload_args *uap)
1237 char *pathname = NULL;
1240 td->td_retval[0] = -1;
1242 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1243 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1245 error = kern_kldload(td, pathname, &fileid);
1247 td->td_retval[0] = fileid;
1249 free(pathname, M_TEMP);
1254 kern_kldunload(struct thread *td, int fileid, int flags)
1259 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1262 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1265 error = linker_kldload_busy(LINKER_UB_PCATCH);
1267 sx_xunlock(&kld_sx);
1271 CURVNET_SET(TD_TO_VNET(td));
1272 lf = linker_find_file_by_id(fileid);
1274 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1276 if (lf->userrefs == 0) {
1278 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1280 printf("kldunload: attempt to unload file that was"
1281 " loaded by the kernel\n");
1283 } else if (lf->refs > 1) {
1287 error = linker_file_unload(lf, flags);
1294 linker_kldload_unbusy(LINKER_UB_LOCKED);
1299 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1302 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1306 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1309 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1310 uap->flags != LINKER_UNLOAD_FORCE)
1312 return (kern_kldunload(td, uap->fileid, uap->flags));
1316 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1319 const char *filename;
1324 error = mac_kld_check_stat(td->td_ucred);
1329 td->td_retval[0] = -1;
1331 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1332 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1335 filename = linker_basename(pathname);
1337 lf = linker_find_file_by_name(filename);
1339 td->td_retval[0] = lf->id;
1342 sx_xunlock(&kld_sx);
1344 free(pathname, M_TEMP);
1349 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1355 error = mac_kld_check_stat(td->td_ucred);
1361 if (uap->fileid == 0)
1362 lf = TAILQ_FIRST(&linker_files);
1364 lf = linker_find_file_by_id(uap->fileid);
1369 lf = TAILQ_NEXT(lf, link);
1372 /* Skip partially loaded files. */
1373 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1374 lf = TAILQ_NEXT(lf, link);
1377 td->td_retval[0] = lf->id;
1379 td->td_retval[0] = 0;
1381 sx_xunlock(&kld_sx);
1386 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1388 struct kld_file_stat *stat;
1392 * Check the version of the user's structure.
1394 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1397 if (version != sizeof(struct kld_file_stat_1) &&
1398 version != sizeof(struct kld_file_stat))
1401 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
1402 error = kern_kldstat(td, uap->fileid, stat);
1404 error = copyout(stat, uap->stat, version);
1410 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1417 error = mac_kld_check_stat(td->td_ucred);
1423 lf = linker_find_file_by_id(fileid);
1425 sx_xunlock(&kld_sx);
1429 /* Version 1 fields: */
1430 namelen = strlen(lf->filename) + 1;
1431 if (namelen > sizeof(stat->name))
1432 namelen = sizeof(stat->name);
1433 bcopy(lf->filename, &stat->name[0], namelen);
1434 stat->refs = lf->refs;
1436 stat->address = lf->address;
1437 stat->size = lf->size;
1438 /* Version 2 fields: */
1439 namelen = strlen(lf->pathname) + 1;
1440 if (namelen > sizeof(stat->pathname))
1441 namelen = sizeof(stat->pathname);
1442 bcopy(lf->pathname, &stat->pathname[0], namelen);
1443 sx_xunlock(&kld_sx);
1445 td->td_retval[0] = 0;
1450 DB_COMMAND_FLAGS(kldstat, db_kldstat, DB_CMD_MEMSAFE)
1454 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
1455 db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
1456 #undef POINTER_WIDTH
1457 TAILQ_FOREACH(lf, &linker_files, link) {
1460 db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs,
1461 lf->address, lf->size, lf->filename);
1467 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1474 error = mac_kld_check_stat(td->td_ucred);
1480 lf = linker_find_file_by_id(uap->fileid);
1483 mp = TAILQ_FIRST(&lf->modules);
1485 td->td_retval[0] = module_getid(mp);
1487 td->td_retval[0] = 0;
1491 sx_xunlock(&kld_sx);
1496 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1498 char *symstr = NULL;
1500 linker_symval_t symval;
1502 struct kld_sym_lookup lookup;
1506 error = mac_kld_check_stat(td->td_ucred);
1511 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1513 if (lookup.version != sizeof(lookup) ||
1514 uap->cmd != KLDSYM_LOOKUP)
1516 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1517 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1520 if (uap->fileid != 0) {
1521 lf = linker_find_file_by_id(uap->fileid);
1524 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1525 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1526 lookup.symvalue = (uintptr_t) symval.value;
1527 lookup.symsize = symval.size;
1528 error = copyout(&lookup, uap->data, sizeof(lookup));
1532 TAILQ_FOREACH(lf, &linker_files, link) {
1533 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1534 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1535 lookup.symvalue = (uintptr_t)symval.value;
1536 lookup.symsize = symval.size;
1537 error = copyout(&lookup, uap->data,
1545 sx_xunlock(&kld_sx);
1547 free(symstr, M_TEMP);
1552 * Preloaded module support
1556 modlist_lookup(const char *name, int ver)
1560 TAILQ_FOREACH(mod, &found_modules, link) {
1561 if (strcmp(mod->name, name) == 0 &&
1562 (ver == 0 || mod->version == ver))
1569 modlist_lookup2(const char *name, const struct mod_depend *verinfo)
1571 modlist_t mod, bestmod;
1574 if (verinfo == NULL)
1575 return (modlist_lookup(name, 0));
1577 TAILQ_FOREACH(mod, &found_modules, link) {
1578 if (strcmp(mod->name, name) != 0)
1581 if (ver == verinfo->md_ver_preferred)
1583 if (ver >= verinfo->md_ver_minimum &&
1584 ver <= verinfo->md_ver_maximum &&
1585 (bestmod == NULL || ver > bestmod->version))
1592 modlist_newmodule(const char *modname, int version, linker_file_t container)
1596 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1598 panic("no memory for module list");
1599 mod->container = container;
1600 mod->name = modname;
1601 mod->version = version;
1602 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1607 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1608 struct mod_metadata **stop, int preload)
1610 struct mod_metadata *mp, **mdp;
1611 const char *modname;
1614 for (mdp = start; mdp < stop; mdp++) {
1616 if (mp->md_type != MDT_VERSION)
1618 modname = mp->md_cval;
1619 ver = ((const struct mod_version *)mp->md_data)->mv_version;
1620 if (modlist_lookup(modname, ver) != NULL) {
1621 printf("module %s already present!\n", modname);
1622 /* XXX what can we do? this is a build error. :-( */
1625 modlist_newmodule(modname, ver, lf);
1630 linker_preload(void *arg)
1633 const char *modname, *nmodname;
1635 linker_file_t lf, nlf;
1638 linker_file_list_t loaded_files;
1639 linker_file_list_t depended_files;
1640 struct mod_metadata *mp, *nmp;
1641 struct mod_metadata **start, **stop, **mdp, **nmdp;
1642 const struct mod_depend *verinfo;
1646 struct sysinit **si_start, **si_stop;
1648 TAILQ_INIT(&loaded_files);
1649 TAILQ_INIT(&depended_files);
1650 TAILQ_INIT(&found_modules);
1655 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1656 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1657 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1658 if (modname == NULL) {
1659 printf("Preloaded module at %p does not have a"
1660 " name!\n", modptr);
1663 if (modtype == NULL) {
1664 printf("Preloaded module at %p does not have a type!\n",
1669 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1672 TAILQ_FOREACH(lc, &classes, link) {
1673 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1679 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1683 * First get a list of stuff in the kernel.
1685 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1687 linker_addmodules(linker_kernel_file, start, stop, 1);
1690 * This is a once-off kinky bubble sort to resolve relocation
1691 * dependency requirements.
1694 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1695 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1698 * First, look to see if we would successfully link with this
1701 resolves = 1; /* unless we know otherwise */
1703 for (mdp = start; mdp < stop; mdp++) {
1705 if (mp->md_type != MDT_DEPEND)
1707 modname = mp->md_cval;
1708 verinfo = mp->md_data;
1709 for (nmdp = start; nmdp < stop; nmdp++) {
1711 if (nmp->md_type != MDT_VERSION)
1713 nmodname = nmp->md_cval;
1714 if (strcmp(modname, nmodname) == 0)
1717 if (nmdp < stop) /* it's a self reference */
1721 * ok, the module isn't here yet, we
1724 if (modlist_lookup2(modname, verinfo) == NULL)
1729 * OK, if we found our modules, we can link. So, "provide"
1730 * the modules inside and add it to the end of the link order
1735 for (mdp = start; mdp < stop; mdp++) {
1737 if (mp->md_type != MDT_VERSION)
1739 modname = mp->md_cval;
1740 nver = ((const struct mod_version *)
1741 mp->md_data)->mv_version;
1742 if (modlist_lookup(modname,
1744 printf("module %s already"
1745 " present!\n", modname);
1746 TAILQ_REMOVE(&loaded_files,
1748 linker_file_unload(lf,
1749 LINKER_UNLOAD_FORCE);
1750 /* we changed tailq next ptr */
1753 modlist_newmodule(modname, nver, lf);
1756 TAILQ_REMOVE(&loaded_files, lf, loaded);
1757 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1759 * Since we provided modules, we need to restart the
1760 * sort so that the previous files that depend on us
1761 * have a chance. Also, we've busted the tailq next
1762 * pointer with the REMOVE.
1769 * At this point, we check to see what could not be resolved..
1771 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1772 TAILQ_REMOVE(&loaded_files, lf, loaded);
1773 printf("KLD file %s is missing dependencies\n", lf->filename);
1774 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1778 * We made it. Finish off the linking in the order we determined.
1780 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1781 if (linker_kernel_file) {
1782 linker_kernel_file->refs++;
1783 linker_file_add_dependency(lf, linker_kernel_file);
1785 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1788 for (mdp = start; mdp < stop; mdp++) {
1790 if (mp->md_type != MDT_DEPEND)
1792 modname = mp->md_cval;
1793 verinfo = mp->md_data;
1794 mod = modlist_lookup2(modname, verinfo);
1796 printf("KLD file %s - cannot find "
1797 "dependency \"%s\"\n",
1798 lf->filename, modname);
1801 /* Don't count self-dependencies */
1802 if (lf == mod->container)
1804 mod->container->refs++;
1805 linker_file_add_dependency(lf, mod->container);
1809 * Now do relocation etc using the symbol search paths
1810 * established by the dependencies
1812 error = LINKER_LINK_PRELOAD_FINISH(lf);
1814 printf("KLD file %s - could not finalize loading\n",
1818 linker_file_register_modules(lf);
1819 if (!TAILQ_EMPTY(&lf->modules))
1820 lf->flags |= LINKER_FILE_MODULES;
1821 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1822 &si_stop, NULL) == 0)
1823 sysinit_add(si_start, si_stop);
1824 linker_file_register_sysctls(lf, true);
1825 lf->flags |= LINKER_FILE_LINKED;
1828 TAILQ_REMOVE(&depended_files, lf, loaded);
1829 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1831 sx_xunlock(&kld_sx);
1832 /* woohoo! we made it! */
1834 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, NULL);
1837 linker_mountroot(void *arg __unused)
1842 TAILQ_FOREACH (lf, &linker_files, link) {
1843 linker_ctf_load_file(lf);
1845 sx_xunlock(&kld_sx);
1847 EVENTHANDLER_DEFINE(mountroot, linker_mountroot, NULL, 0);
1850 * Handle preload files that failed to load any modules.
1853 linker_preload_finish(void *arg)
1855 linker_file_t lf, nlf;
1858 TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) {
1859 if (lf == linker_kernel_file)
1863 * If all of the modules in this file failed to load, unload
1864 * the file and return an error of ENOEXEC. (Parity with
1865 * linker_load_file.)
1867 if ((lf->flags & LINKER_FILE_MODULES) != 0 &&
1868 TAILQ_EMPTY(&lf->modules)) {
1869 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1873 lf->flags &= ~LINKER_FILE_MODULES;
1874 lf->userrefs++; /* so we can (try to) kldunload it */
1876 sx_xunlock(&kld_sx);
1880 * Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be
1881 * scheduled at any subsystem and order, so run this as late as possible. init
1882 * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that.
1884 SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE,
1885 linker_preload_finish, NULL);
1888 * Search for a not-loaded module by name.
1890 * Modules may be found in the following locations:
1892 * - preloaded (result is just the module name) - on disk (result is full path
1895 * If the module name is qualified in any way (contains path, etc.) the we
1896 * simply return a copy of it.
1898 * The search path can be manipulated via sysctl. Note that we use the ';'
1899 * character as a separator to be consistent with the bootloader.
1902 static char linker_hintfile[] = "linker.hints";
1903 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1905 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path,
1906 sizeof(linker_path), "module load search path");
1908 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1910 static const char * const linker_ext_list[] = {
1917 * Check if file actually exists either with or without extension listed in
1918 * the linker_ext_list. (probably should be generic for the rest of the
1922 linker_lookup_file(const char *path, int pathlen, const char *name,
1923 int namelen, struct vattr *vap)
1925 struct nameidata nd;
1926 struct thread *td = curthread; /* XXX */
1927 const char * const *cpp, *sep;
1929 int error, len, extlen, reclen, flags;
1930 __enum_uint8(vtype) type;
1933 for (cpp = linker_ext_list; *cpp; cpp++) {
1938 extlen++; /* trailing '\0' */
1939 sep = (path[pathlen - 1] != '/') ? "/" : "";
1941 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1942 result = malloc(reclen, M_LINKER, M_WAITOK);
1943 for (cpp = linker_ext_list; *cpp; cpp++) {
1944 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1945 namelen, name, *cpp);
1947 * Attempt to open the file, and return the path if
1948 * we succeed and it's a regular file.
1950 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result);
1952 error = vn_open(&nd, &flags, 0, NULL);
1955 type = nd.ni_vp->v_type;
1957 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1958 VOP_UNLOCK(nd.ni_vp);
1959 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1964 free(result, M_LINKER);
1968 #define INT_ALIGN(base, ptr) ptr = \
1969 (base) + roundup2((ptr) - (base), sizeof(int))
1972 * Lookup KLD which contains requested module in the "linker.hints" file. If
1973 * version specification is available, then try to find the best KLD.
1974 * Otherwise just find the latest one.
1977 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1978 int modnamelen, const struct mod_depend *verinfo)
1980 struct thread *td = curthread; /* XXX */
1981 struct ucred *cred = td ? td->td_ucred : NULL;
1982 struct nameidata nd;
1983 struct vattr vattr, mattr;
1984 const char *best, *sep;
1985 u_char *hints = NULL;
1986 u_char *cp, *recptr, *bufend, *result, *pathbuf;
1987 int error, ival, bestver, *intp, found, flags, clen, blen;
1991 bestver = found = 0;
1993 sep = (path[pathlen - 1] != '/') ? "/" : "";
1994 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1996 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1997 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
2000 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf);
2002 error = vn_open(&nd, &flags, 0, NULL);
2006 if (nd.ni_vp->v_type != VREG)
2009 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
2013 * XXX: we need to limit this number to some reasonable value
2015 if (vattr.va_size > LINKER_HINTS_MAX) {
2016 printf("linker.hints file too large %ld\n", (long)vattr.va_size);
2019 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
2020 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
2021 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
2024 VOP_UNLOCK(nd.ni_vp);
2025 vn_close(nd.ni_vp, FREAD, cred, td);
2028 printf("can't read %zd\n", reclen);
2031 intp = (int *)hints;
2033 if (ival != LINKER_HINTS_VERSION) {
2034 printf("linker.hints file version mismatch %d\n", ival);
2037 bufend = hints + vattr.va_size;
2038 recptr = (u_char *)intp;
2040 while (recptr < bufend && !found) {
2041 intp = (int *)recptr;
2048 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
2051 INT_ALIGN(hints, cp);
2055 if (verinfo == NULL ||
2056 ival == verinfo->md_ver_preferred) {
2060 if (ival >= verinfo->md_ver_minimum &&
2061 ival <= verinfo->md_ver_maximum &&
2071 recptr += reclen + sizeof(int);
2074 * Finally check if KLD is in the place
2077 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
2079 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
2082 * KLD is newer than hints file. What we should do now?
2084 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
2085 printf("warning: KLD '%s' is newer than the linker.hints"
2088 free(pathbuf, M_LINKER);
2090 free(hints, M_TEMP);
2091 if (nd.ni_vp != NULL) {
2092 VOP_UNLOCK(nd.ni_vp);
2093 vn_close(nd.ni_vp, FREAD, cred, td);
2096 * If nothing found or hints is absent - fallback to the old
2097 * way by using "kldname[.ko]" as module name.
2099 if (!found && !bestver && result == NULL)
2100 result = linker_lookup_file(path, pathlen, modname,
2106 * Lookup KLD which contains requested module in the all directories.
2109 linker_search_module(const char *modname, int modnamelen,
2110 const struct mod_depend *verinfo)
2112 char *cp, *ep, *result;
2115 * traverse the linker path
2117 for (cp = linker_path; *cp; cp = ep + 1) {
2118 /* find the end of this component */
2119 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
2120 result = linker_hints_lookup(cp, ep - cp, modname,
2121 modnamelen, verinfo);
2131 * Search for module in all directories listed in the linker_path.
2134 linker_search_kld(const char *name)
2136 char *cp, *ep, *result;
2139 /* qualified at all? */
2140 if (strchr(name, '/'))
2141 return (strdup(name, M_LINKER));
2143 /* traverse the linker path */
2145 for (ep = linker_path; *ep; ep++) {
2147 /* find the end of this component */
2148 for (; *ep != 0 && *ep != ';'; ep++);
2149 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
2157 linker_basename(const char *path)
2159 const char *filename;
2161 filename = strrchr(path, '/');
2162 if (filename == NULL)
2171 * Inform hwpmc about the set of kernel modules currently loaded.
2174 linker_hwpmc_list_objects(void)
2177 struct pmckern_map_in *kobase;
2182 TAILQ_FOREACH(lf, &linker_files, link)
2185 /* Allocate nmappings + 1 entries. */
2186 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
2187 M_LINKER, M_WAITOK | M_ZERO);
2189 TAILQ_FOREACH(lf, &linker_files, link) {
2190 /* Save the info for this linker file. */
2191 kobase[i].pm_file = lf->pathname;
2192 kobase[i].pm_address = (uintptr_t)lf->address;
2195 sx_sunlock(&kld_sx);
2197 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
2199 /* The last entry of the malloced area comprises of all zeros. */
2200 KASSERT(kobase[i].pm_file == NULL,
2201 ("linker_hwpmc_list_objects: last object not NULL"));
2203 return ((void *)kobase);
2207 /* check if root file system is not mounted */
2209 linker_root_mounted(void)
2214 if (rootvnode == NULL)
2217 pwd = pwd_hold(curthread);
2218 ret = pwd->pwd_rdir != NULL;
2224 * Find a file which contains given module and load it, if "parent" is not
2225 * NULL, register a reference to it.
2228 linker_load_module(const char *kldname, const char *modname,
2229 struct linker_file *parent, const struct mod_depend *verinfo,
2230 struct linker_file **lfpp)
2232 linker_file_t lfdep;
2233 const char *filename;
2237 sx_assert(&kld_sx, SA_XLOCKED);
2238 if (modname == NULL) {
2240 * We have to load KLD
2242 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2244 if (!linker_root_mounted())
2246 pathname = linker_search_kld(kldname);
2248 if (modlist_lookup2(modname, verinfo) != NULL)
2250 if (!linker_root_mounted())
2252 if (kldname != NULL)
2253 pathname = strdup(kldname, M_LINKER);
2256 * Need to find a KLD with required module
2258 pathname = linker_search_module(modname,
2259 strlen(modname), verinfo);
2261 if (pathname == NULL)
2265 * Can't load more than one file with the same basename XXX:
2266 * Actually it should be possible to have multiple KLDs with
2267 * the same basename but different path because they can
2268 * provide different versions of the same modules.
2270 filename = linker_basename(pathname);
2271 if (linker_find_file_by_name(filename))
2274 error = linker_load_file(pathname, &lfdep);
2277 if (modname && verinfo &&
2278 modlist_lookup2(modname, verinfo) == NULL) {
2279 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2284 linker_file_add_dependency(parent, lfdep);
2288 free(pathname, M_LINKER);
2293 * This routine is responsible for finding dependencies of userland initiated
2294 * kldload(2)'s of files.
2297 linker_load_dependencies(linker_file_t lf)
2299 linker_file_t lfdep;
2300 struct mod_metadata **start, **stop, **mdp, **nmdp;
2301 struct mod_metadata *mp, *nmp;
2302 const struct mod_depend *verinfo;
2304 const char *modname, *nmodname;
2308 * All files are dependent on /kernel.
2310 sx_assert(&kld_sx, SA_XLOCKED);
2311 if (linker_kernel_file) {
2312 linker_kernel_file->refs++;
2313 linker_file_add_dependency(lf, linker_kernel_file);
2315 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2318 for (mdp = start; mdp < stop; mdp++) {
2320 if (mp->md_type != MDT_VERSION)
2322 modname = mp->md_cval;
2323 ver = ((const struct mod_version *)mp->md_data)->mv_version;
2324 mod = modlist_lookup(modname, ver);
2326 printf("interface %s.%d already present in the KLD"
2327 " '%s'!\n", modname, ver,
2328 mod->container->filename);
2333 for (mdp = start; mdp < stop; mdp++) {
2335 if (mp->md_type != MDT_DEPEND)
2337 modname = mp->md_cval;
2338 verinfo = mp->md_data;
2340 for (nmdp = start; nmdp < stop; nmdp++) {
2342 if (nmp->md_type != MDT_VERSION)
2344 nmodname = nmp->md_cval;
2345 if (strcmp(modname, nmodname) == 0)
2348 if (nmdp < stop)/* early exit, it's a self reference */
2350 mod = modlist_lookup2(modname, verinfo);
2351 if (mod) { /* woohoo, it's loaded already */
2352 lfdep = mod->container;
2354 linker_file_add_dependency(lf, lfdep);
2357 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2359 printf("KLD %s: depends on %s - not available or"
2360 " version mismatch\n", lf->filename, modname);
2367 linker_addmodules(lf, start, stop, 0);
2372 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2374 struct sysctl_req *req;
2377 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2381 * Export a nul-separated, double-nul-terminated list of all function names
2385 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2391 error = mac_kld_check_stat(req->td->td_ucred);
2395 error = sysctl_wire_old_buffer(req, 0);
2399 TAILQ_FOREACH(lf, &linker_files, link) {
2400 error = LINKER_EACH_FUNCTION_NAME(lf,
2401 sysctl_kern_function_list_iterate, req);
2403 sx_xunlock(&kld_sx);
2407 sx_xunlock(&kld_sx);
2408 return (SYSCTL_OUT(req, "", 1));
2411 SYSCTL_PROC(_kern, OID_AUTO, function_list,
2412 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
2413 sysctl_kern_function_list, "",
2414 "kernel function list");