2 * Copyright (c) 1997-2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_hwpmc_hooks.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/sysproto.h>
38 #include <sys/sysent.h>
42 #include <sys/mutex.h>
44 #include <sys/module.h>
45 #include <sys/mount.h>
46 #include <sys/linker.h>
47 #include <sys/eventhandler.h>
48 #include <sys/fcntl.h>
50 #include <sys/libkern.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysctl.h>
58 #include <security/mac/mac_framework.h>
60 #include "linker_if.h"
63 #include <sys/pmckern.h>
68 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RW,
69 &kld_debug, 0, "Set various levels of KLD debug");
73 * static char *linker_search_path(const char *name, struct mod_depend
76 static const char *linker_basename(const char *path);
79 * Find a currently loaded file given its filename.
81 static linker_file_t linker_find_file_by_name(const char* _filename);
84 * Find a currently loaded file given its file id.
86 static linker_file_t linker_find_file_by_id(int _fileid);
88 /* Metadata from the static kernel */
89 SET_DECLARE(modmetadata_set, struct mod_metadata);
91 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
93 linker_file_t linker_kernel_file;
95 static struct sx kld_sx; /* kernel linker lock */
98 * Load counter used by clients to determine if a linker file has been
99 * re-loaded. This counter is incremented for each file load.
103 static linker_class_list_t classes;
104 static linker_file_list_t linker_files;
105 static int next_file_id = 1;
106 static int linker_no_more_classes = 0;
108 #define LINKER_GET_NEXT_FILE_ID(a) do { \
109 linker_file_t lftmp; \
112 sx_assert(&kld_sx, SA_XLOCKED); \
114 TAILQ_FOREACH(lftmp, &linker_files, link) { \
115 if (next_file_id == lftmp->id) { \
120 (a) = next_file_id; \
124 /* XXX wrong name; we're looking at version provision tags here, not modules */
125 typedef TAILQ_HEAD(, modlist) modlisthead_t;
127 TAILQ_ENTRY(modlist) link; /* chain together all modules */
128 linker_file_t container;
132 typedef struct modlist *modlist_t;
133 static modlisthead_t found_modules;
135 static int linker_file_add_dependency(linker_file_t file,
137 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
138 const char* name, int deps);
139 static int linker_load_module(const char *kldname,
140 const char *modname, struct linker_file *parent,
141 struct mod_depend *verinfo, struct linker_file **lfpp);
142 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
145 linker_strdup(const char *str)
149 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
155 linker_init(void *arg)
158 sx_init(&kld_sx, "kernel linker");
159 TAILQ_INIT(&classes);
160 TAILQ_INIT(&linker_files);
163 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
166 linker_stop_class_add(void *arg)
169 linker_no_more_classes = 1;
172 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
175 linker_add_class(linker_class_t lc)
179 * We disallow any class registration past SI_ORDER_ANY
180 * of SI_SUB_KLD. We bump the reference count to keep the
181 * ops from being freed.
183 if (linker_no_more_classes == 1)
185 kobj_class_compile((kobj_class_t) lc);
186 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
187 TAILQ_INSERT_TAIL(&classes, lc, link);
192 linker_file_sysinit(linker_file_t lf)
194 struct sysinit **start, **stop, **sipp, **xipp, *save;
196 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
199 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
202 * Perform a bubble sort of the system initialization objects by
203 * their subsystem (primary key) and order (secondary key).
205 * Since some things care about execution order, this is the operation
206 * which ensures continued function.
208 for (sipp = start; sipp < stop; sipp++) {
209 for (xipp = sipp + 1; xipp < stop; xipp++) {
210 if ((*sipp)->subsystem < (*xipp)->subsystem ||
211 ((*sipp)->subsystem == (*xipp)->subsystem &&
212 (*sipp)->order <= (*xipp)->order))
221 * Traverse the (now) ordered list of system initialization tasks.
222 * Perform each task, and continue on to the next task.
225 for (sipp = start; sipp < stop; sipp++) {
226 if ((*sipp)->subsystem == SI_SUB_DUMMY)
227 continue; /* skip dummy task(s) */
230 (*((*sipp)->func)) ((*sipp)->udata);
236 linker_file_sysuninit(linker_file_t lf)
238 struct sysinit **start, **stop, **sipp, **xipp, *save;
240 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
243 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
248 * Perform a reverse bubble sort of the system initialization objects
249 * by their subsystem (primary key) and order (secondary key).
251 * Since some things care about execution order, this is the operation
252 * which ensures continued function.
254 for (sipp = start; sipp < stop; sipp++) {
255 for (xipp = sipp + 1; xipp < stop; xipp++) {
256 if ((*sipp)->subsystem > (*xipp)->subsystem ||
257 ((*sipp)->subsystem == (*xipp)->subsystem &&
258 (*sipp)->order >= (*xipp)->order))
267 * Traverse the (now) ordered list of system initialization tasks.
268 * Perform each task, and continue on to the next task.
271 for (sipp = start; sipp < stop; sipp++) {
272 if ((*sipp)->subsystem == SI_SUB_DUMMY)
273 continue; /* skip dummy task(s) */
276 (*((*sipp)->func)) ((*sipp)->udata);
282 linker_file_register_sysctls(linker_file_t lf)
284 struct sysctl_oid **start, **stop, **oidp;
287 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
290 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
294 for (oidp = start; oidp < stop; oidp++)
295 sysctl_register_oid(*oidp);
300 linker_file_unregister_sysctls(linker_file_t lf)
302 struct sysctl_oid **start, **stop, **oidp;
304 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
305 " for %s\n", lf->filename));
307 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
311 for (oidp = start; oidp < stop; oidp++)
312 sysctl_unregister_oid(*oidp);
317 linker_file_register_modules(linker_file_t lf)
319 struct mod_metadata **start, **stop, **mdp;
320 const moduledata_t *moddata;
321 int first_error, error;
323 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
324 " in %s\n", lf->filename));
326 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
329 * This fallback should be unnecessary, but if we get booted
330 * from boot2 instead of loader and we are missing our
331 * metadata then we have to try the best we can.
333 if (lf == linker_kernel_file) {
334 start = SET_BEGIN(modmetadata_set);
335 stop = SET_LIMIT(modmetadata_set);
340 for (mdp = start; mdp < stop; mdp++) {
341 if ((*mdp)->md_type != MDT_MODULE)
343 moddata = (*mdp)->md_data;
344 KLD_DPF(FILE, ("Registering module %s in %s\n",
345 moddata->name, lf->filename));
346 error = module_register(moddata, lf);
348 printf("Module %s failed to register: %d\n",
349 moddata->name, error);
350 if (first_error == 0)
354 return (first_error);
358 linker_init_kernel_modules(void)
362 linker_file_register_modules(linker_kernel_file);
366 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
370 linker_load_file(const char *filename, linker_file_t *result)
374 int foundfile, error, modules;
376 /* Refuse to load modules if securelevel raised */
377 if (prison0.pr_securelevel > 0)
380 sx_assert(&kld_sx, SA_XLOCKED);
381 lf = linker_find_file_by_name(filename);
383 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
384 " incrementing refs\n", filename));
393 * We do not need to protect (lock) classes here because there is
394 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
395 * and there is no class deregistration mechanism at this time.
397 TAILQ_FOREACH(lc, &classes, link) {
398 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
400 error = LINKER_LOAD_FILE(lc, filename, &lf);
402 * If we got something other than ENOENT, then it exists but
403 * we cannot load it for some other reason.
408 error = linker_file_register_modules(lf);
409 if (error == EEXIST) {
410 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
413 modules = !TAILQ_EMPTY(&lf->modules);
415 linker_file_register_sysctls(lf);
416 linker_file_sysinit(lf);
418 lf->flags |= LINKER_FILE_LINKED;
421 * If all of the modules in this file failed
422 * to load, unload the file and return an
425 if (modules && TAILQ_EMPTY(&lf->modules)) {
426 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
434 * Less than ideal, but tells the user whether it failed to load or
435 * the module was not found.
440 * If the file type has not been recognized by the last try
441 * printout a message before to fail.
444 printf("linker_load_file: Unsupported file type\n");
447 * Format not recognized or otherwise unloadable.
448 * When loading a module that is statically built into
449 * the kernel EEXIST percolates back up as the return
450 * value. Preserve this so that apps like sysinstall
451 * can recognize this special case and not post bogus
457 error = ENOENT; /* Nothing found */
462 linker_reference_module(const char *modname, struct mod_depend *verinfo,
463 linker_file_t *result)
469 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
470 *result = mod->container;
476 error = linker_load_module(NULL, modname, NULL, verinfo, result);
482 linker_release_module(const char *modname, struct mod_depend *verinfo,
490 KASSERT(modname != NULL,
491 ("linker_release_module: no file or name"));
492 mod = modlist_lookup2(modname, verinfo);
499 KASSERT(modname == NULL && verinfo == NULL,
500 ("linker_release_module: both file and name"));
501 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
507 linker_find_file_by_name(const char *filename)
512 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
513 sprintf(koname, "%s.ko", filename);
515 sx_assert(&kld_sx, SA_XLOCKED);
516 TAILQ_FOREACH(lf, &linker_files, link) {
517 if (strcmp(lf->filename, koname) == 0)
519 if (strcmp(lf->filename, filename) == 0)
522 free(koname, M_LINKER);
527 linker_find_file_by_id(int fileid)
531 sx_assert(&kld_sx, SA_XLOCKED);
532 TAILQ_FOREACH(lf, &linker_files, link)
533 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
539 linker_file_foreach(linker_predicate_t *predicate, void *context)
545 TAILQ_FOREACH(lf, &linker_files, link) {
546 retval = predicate(lf, context);
555 linker_make_file(const char *pathname, linker_class_t lc)
558 const char *filename;
561 sx_assert(&kld_sx, SA_XLOCKED);
562 filename = linker_basename(pathname);
564 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
565 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
571 lf->filename = linker_strdup(filename);
572 lf->pathname = linker_strdup(pathname);
573 LINKER_GET_NEXT_FILE_ID(lf->id);
576 lf->loadcnt = ++loadcnt;
577 STAILQ_INIT(&lf->common);
578 TAILQ_INIT(&lf->modules);
579 TAILQ_INSERT_TAIL(&linker_files, lf, link);
584 linker_file_unload(linker_file_t file, int flags)
587 modlist_t ml, nextml;
588 struct common_symbol *cp;
591 /* Refuse to unload modules if securelevel raised. */
592 if (prison0.pr_securelevel > 0)
595 sx_assert(&kld_sx, SA_XLOCKED);
596 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
598 /* Easy case of just dropping a reference. */
599 if (file->refs > 1) {
604 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
605 " informing modules\n"));
608 * Quiesce all the modules to give them a chance to veto the unload.
611 for (mod = TAILQ_FIRST(&file->modules); mod;
612 mod = module_getfnext(mod)) {
614 error = module_quiesce(mod);
615 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
616 KLD_DPF(FILE, ("linker_file_unload: module %s"
617 " vetoed unload\n", module_getname(mod)));
619 * XXX: Do we need to tell all the quiesced modules
620 * that they can resume work now via a new module
630 * Inform any modules associated with this file that they are
634 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
635 next = module_getfnext(mod);
639 * Give the module a chance to veto the unload.
641 if ((error = module_unload(mod)) != 0) {
644 KLD_DPF(FILE, ("linker_file_unload: module %s"
645 " failed unload\n", module_getname(mod)));
655 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
656 if (ml->container == file) {
657 TAILQ_REMOVE(&found_modules, ml, link);
663 * Don't try to run SYSUNINITs if we are unloaded due to a
666 if (file->flags & LINKER_FILE_LINKED) {
667 file->flags &= ~LINKER_FILE_LINKED;
669 linker_file_sysuninit(file);
670 linker_file_unregister_sysctls(file);
673 TAILQ_REMOVE(&linker_files, file, link);
676 for (i = 0; i < file->ndeps; i++)
677 linker_file_unload(file->deps[i], flags);
678 free(file->deps, M_LINKER);
681 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
682 STAILQ_REMOVE_HEAD(&file->common, link);
687 if (file->filename) {
688 free(file->filename, M_LINKER);
689 file->filename = NULL;
691 if (file->pathname) {
692 free(file->pathname, M_LINKER);
693 file->pathname = NULL;
695 kobj_delete((kobj_t) file, M_LINKER);
700 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
702 return (LINKER_CTF_GET(file, lc));
706 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
708 linker_file_t *newdeps;
710 sx_assert(&kld_sx, SA_XLOCKED);
711 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
712 M_LINKER, M_WAITOK | M_ZERO);
717 bcopy(file->deps, newdeps,
718 file->ndeps * sizeof(linker_file_t *));
719 free(file->deps, M_LINKER);
721 file->deps = newdeps;
722 file->deps[file->ndeps] = dep;
724 KLD_DPF(FILE, ("linker_file_add_dependency:"
725 " adding %s as dependency for %s\n",
726 dep->filename, file->filename));
731 * Locate a linker set and its contents. This is a helper function to avoid
732 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
733 * This function is used in this file so we can avoid having lots of (void **)
737 linker_file_lookup_set(linker_file_t file, const char *name,
738 void *firstp, void *lastp, int *countp)
742 locked = sx_xlocked(&kld_sx);
745 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
752 * List all functions in a file.
755 linker_file_function_listall(linker_file_t lf,
756 linker_function_nameval_callback_t callback_func, void *arg)
758 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
762 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
767 locked = sx_xlocked(&kld_sx);
770 sym = linker_file_lookup_symbol_internal(file, name, deps);
777 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
781 linker_symval_t symval;
783 size_t common_size = 0;
786 sx_assert(&kld_sx, SA_XLOCKED);
787 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
790 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
791 LINKER_SYMBOL_VALUES(file, sym, &symval);
792 if (symval.value == 0)
794 * For commons, first look them up in the
795 * dependencies and only allocate space if not found
798 common_size = symval.size;
800 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
801 ".value=%p\n", symval.value));
802 return (symval.value);
806 for (i = 0; i < file->ndeps; i++) {
807 address = linker_file_lookup_symbol_internal(
808 file->deps[i], name, 0);
810 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
811 " deps value=%p\n", address));
816 if (common_size > 0) {
818 * This is a common symbol which was not found in the
819 * dependencies. We maintain a simple common symbol table in
822 struct common_symbol *cp;
824 STAILQ_FOREACH(cp, &file->common, link) {
825 if (strcmp(cp->name, name) == 0) {
826 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
827 " old common value=%p\n", cp->address));
828 return (cp->address);
832 * Round the symbol size up to align.
834 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
835 cp = malloc(sizeof(struct common_symbol)
836 + common_size + strlen(name) + 1, M_LINKER,
838 cp->address = (caddr_t)(cp + 1);
839 cp->name = cp->address + common_size;
840 strcpy(cp->name, name);
841 bzero(cp->address, common_size);
842 STAILQ_INSERT_TAIL(&file->common, cp, link);
844 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
845 " value=%p\n", cp->address));
846 return (cp->address);
848 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
853 * Both DDB and stack(9) rely on the kernel linker to provide forward and
854 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
855 * do this in a lockfree manner. We provide a set of internal helper
856 * routines to perform these operations without locks, and then wrappers that
859 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
863 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
867 TAILQ_FOREACH(lf, &linker_files, link) {
868 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
876 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
879 c_linker_sym_t best, es;
880 u_long diff, bestdiff, off;
883 off = (uintptr_t)value;
885 TAILQ_FOREACH(lf, &linker_files, link) {
886 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
888 if (es != 0 && diff < bestdiff) {
907 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
911 TAILQ_FOREACH(lf, &linker_files, link) {
912 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
919 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
922 linker_symval_t symval;
927 error = linker_debug_search_symbol(value, &sym, offset);
930 error = linker_debug_symbol_values(sym, &symval);
933 strlcpy(buf, symval.name, buflen);
938 * DDB Helpers. DDB has to look across multiple files with their own symbol
939 * tables and string tables.
941 * Note that we do not obey list locking protocols here. We really don't need
942 * DDB to hang because somebody's got the lock held. We'll take the chance
943 * that the files list is inconsistant instead.
947 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
950 return (linker_debug_lookup(symstr, sym));
955 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
958 return (linker_debug_search_symbol(value, sym, diffp));
962 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
965 return (linker_debug_symbol_values(sym, symval));
969 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
973 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
977 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
978 * obey locking protocols, and offer a significantly less complex interface.
981 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
987 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
996 kern_kldload(struct thread *td, const char *file, int *fileid)
999 struct pmckern_map_in pkm;
1001 const char *kldname, *modname;
1005 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1008 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1012 * It is possible that kldloaded module will attach a new ifnet,
1013 * so vnet context must be set when this ocurs.
1015 CURVNET_SET(TD_TO_VNET(td));
1018 * If file does not contain a qualified name or any dot in it
1019 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1022 if (index(file, '/') || index(file, '.')) {
1031 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1033 sx_xunlock(&kld_sx);
1040 EVENTHANDLER_INVOKE(kld_load, lf);
1043 sx_downgrade(&kld_sx);
1044 pkm.pm_file = lf->filename;
1045 pkm.pm_address = (uintptr_t) lf->address;
1046 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
1047 sx_sunlock(&kld_sx);
1049 sx_xunlock(&kld_sx);
1058 sys_kldload(struct thread *td, struct kldload_args *uap)
1060 char *pathname = NULL;
1063 td->td_retval[0] = -1;
1065 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1066 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1068 error = kern_kldload(td, pathname, &fileid);
1070 td->td_retval[0] = fileid;
1072 free(pathname, M_TEMP);
1077 kern_kldunload(struct thread *td, int fileid, int flags)
1080 struct pmckern_map_out pkm;
1085 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1088 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1091 CURVNET_SET(TD_TO_VNET(td));
1093 lf = linker_find_file_by_id(fileid);
1095 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1097 EVENTHANDLER_INVOKE(kld_unload, lf, &error);
1100 else if (lf->userrefs == 0) {
1102 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1104 printf("kldunload: attempt to unload file that was"
1105 " loaded by the kernel\n");
1109 /* Save data needed by hwpmc(4) before unloading. */
1110 pkm.pm_address = (uintptr_t) lf->address;
1111 pkm.pm_size = lf->size;
1114 error = linker_file_unload(lf, flags);
1123 sx_downgrade(&kld_sx);
1124 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
1125 sx_sunlock(&kld_sx);
1127 sx_xunlock(&kld_sx);
1129 sx_xunlock(&kld_sx);
1136 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1139 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1143 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1146 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1147 uap->flags != LINKER_UNLOAD_FORCE)
1149 return (kern_kldunload(td, uap->fileid, uap->flags));
1153 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1156 const char *filename;
1161 error = mac_kld_check_stat(td->td_ucred);
1166 td->td_retval[0] = -1;
1168 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1169 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1172 filename = linker_basename(pathname);
1174 lf = linker_find_file_by_name(filename);
1176 td->td_retval[0] = lf->id;
1179 sx_xunlock(&kld_sx);
1181 free(pathname, M_TEMP);
1186 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1192 error = mac_kld_check_stat(td->td_ucred);
1198 if (uap->fileid == 0)
1199 lf = TAILQ_FIRST(&linker_files);
1201 lf = linker_find_file_by_id(uap->fileid);
1206 lf = TAILQ_NEXT(lf, link);
1209 /* Skip partially loaded files. */
1210 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1211 lf = TAILQ_NEXT(lf, link);
1214 td->td_retval[0] = lf->id;
1216 td->td_retval[0] = 0;
1218 sx_xunlock(&kld_sx);
1223 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1225 struct kld_file_stat stat;
1229 * Check the version of the user's structure.
1231 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1234 if (version != sizeof(struct kld_file_stat_1) &&
1235 version != sizeof(struct kld_file_stat))
1238 error = kern_kldstat(td, uap->fileid, &stat);
1241 return (copyout(&stat, uap->stat, version));
1245 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1252 error = mac_kld_check_stat(td->td_ucred);
1258 lf = linker_find_file_by_id(fileid);
1260 sx_xunlock(&kld_sx);
1264 /* Version 1 fields: */
1265 namelen = strlen(lf->filename) + 1;
1266 if (namelen > MAXPATHLEN)
1267 namelen = MAXPATHLEN;
1268 bcopy(lf->filename, &stat->name[0], namelen);
1269 stat->refs = lf->refs;
1271 stat->address = lf->address;
1272 stat->size = lf->size;
1273 /* Version 2 fields: */
1274 namelen = strlen(lf->pathname) + 1;
1275 if (namelen > MAXPATHLEN)
1276 namelen = MAXPATHLEN;
1277 bcopy(lf->pathname, &stat->pathname[0], namelen);
1278 sx_xunlock(&kld_sx);
1280 td->td_retval[0] = 0;
1285 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1292 error = mac_kld_check_stat(td->td_ucred);
1298 lf = linker_find_file_by_id(uap->fileid);
1301 mp = TAILQ_FIRST(&lf->modules);
1303 td->td_retval[0] = module_getid(mp);
1305 td->td_retval[0] = 0;
1309 sx_xunlock(&kld_sx);
1314 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1316 char *symstr = NULL;
1318 linker_symval_t symval;
1320 struct kld_sym_lookup lookup;
1324 error = mac_kld_check_stat(td->td_ucred);
1329 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1331 if (lookup.version != sizeof(lookup) ||
1332 uap->cmd != KLDSYM_LOOKUP)
1334 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1335 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1338 if (uap->fileid != 0) {
1339 lf = linker_find_file_by_id(uap->fileid);
1342 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1343 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1344 lookup.symvalue = (uintptr_t) symval.value;
1345 lookup.symsize = symval.size;
1346 error = copyout(&lookup, uap->data, sizeof(lookup));
1350 TAILQ_FOREACH(lf, &linker_files, link) {
1351 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1352 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1353 lookup.symvalue = (uintptr_t)symval.value;
1354 lookup.symsize = symval.size;
1355 error = copyout(&lookup, uap->data,
1363 sx_xunlock(&kld_sx);
1365 free(symstr, M_TEMP);
1370 * Preloaded module support
1374 modlist_lookup(const char *name, int ver)
1378 TAILQ_FOREACH(mod, &found_modules, link) {
1379 if (strcmp(mod->name, name) == 0 &&
1380 (ver == 0 || mod->version == ver))
1387 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1389 modlist_t mod, bestmod;
1392 if (verinfo == NULL)
1393 return (modlist_lookup(name, 0));
1395 TAILQ_FOREACH(mod, &found_modules, link) {
1396 if (strcmp(mod->name, name) != 0)
1399 if (ver == verinfo->md_ver_preferred)
1401 if (ver >= verinfo->md_ver_minimum &&
1402 ver <= verinfo->md_ver_maximum &&
1403 (bestmod == NULL || ver > bestmod->version))
1410 modlist_newmodule(const char *modname, int version, linker_file_t container)
1414 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1416 panic("no memory for module list");
1417 mod->container = container;
1418 mod->name = modname;
1419 mod->version = version;
1420 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1425 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1426 struct mod_metadata **stop, int preload)
1428 struct mod_metadata *mp, **mdp;
1429 const char *modname;
1432 for (mdp = start; mdp < stop; mdp++) {
1434 if (mp->md_type != MDT_VERSION)
1436 modname = mp->md_cval;
1437 ver = ((struct mod_version *)mp->md_data)->mv_version;
1438 if (modlist_lookup(modname, ver) != NULL) {
1439 printf("module %s already present!\n", modname);
1440 /* XXX what can we do? this is a build error. :-( */
1443 modlist_newmodule(modname, ver, lf);
1448 linker_preload(void *arg)
1451 const char *modname, *nmodname;
1453 linker_file_t lf, nlf;
1456 linker_file_list_t loaded_files;
1457 linker_file_list_t depended_files;
1458 struct mod_metadata *mp, *nmp;
1459 struct mod_metadata **start, **stop, **mdp, **nmdp;
1460 struct mod_depend *verinfo;
1464 struct sysinit **si_start, **si_stop;
1466 TAILQ_INIT(&loaded_files);
1467 TAILQ_INIT(&depended_files);
1468 TAILQ_INIT(&found_modules);
1473 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1474 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1475 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1476 if (modname == NULL) {
1477 printf("Preloaded module at %p does not have a"
1478 " name!\n", modptr);
1481 if (modtype == NULL) {
1482 printf("Preloaded module at %p does not have a type!\n",
1487 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1490 TAILQ_FOREACH(lc, &classes, link) {
1491 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1497 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1501 * First get a list of stuff in the kernel.
1503 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1505 linker_addmodules(linker_kernel_file, start, stop, 1);
1508 * This is a once-off kinky bubble sort to resolve relocation
1509 * dependency requirements.
1512 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1513 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1516 * First, look to see if we would successfully link with this
1519 resolves = 1; /* unless we know otherwise */
1521 for (mdp = start; mdp < stop; mdp++) {
1523 if (mp->md_type != MDT_DEPEND)
1525 modname = mp->md_cval;
1526 verinfo = mp->md_data;
1527 for (nmdp = start; nmdp < stop; nmdp++) {
1529 if (nmp->md_type != MDT_VERSION)
1531 nmodname = nmp->md_cval;
1532 if (strcmp(modname, nmodname) == 0)
1535 if (nmdp < stop) /* it's a self reference */
1539 * ok, the module isn't here yet, we
1542 if (modlist_lookup2(modname, verinfo) == NULL)
1547 * OK, if we found our modules, we can link. So, "provide"
1548 * the modules inside and add it to the end of the link order
1553 for (mdp = start; mdp < stop; mdp++) {
1555 if (mp->md_type != MDT_VERSION)
1557 modname = mp->md_cval;
1558 nver = ((struct mod_version *)
1559 mp->md_data)->mv_version;
1560 if (modlist_lookup(modname,
1562 printf("module %s already"
1563 " present!\n", modname);
1564 TAILQ_REMOVE(&loaded_files,
1566 linker_file_unload(lf,
1567 LINKER_UNLOAD_FORCE);
1568 /* we changed tailq next ptr */
1571 modlist_newmodule(modname, nver, lf);
1574 TAILQ_REMOVE(&loaded_files, lf, loaded);
1575 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1577 * Since we provided modules, we need to restart the
1578 * sort so that the previous files that depend on us
1579 * have a chance. Also, we've busted the tailq next
1580 * pointer with the REMOVE.
1587 * At this point, we check to see what could not be resolved..
1589 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1590 TAILQ_REMOVE(&loaded_files, lf, loaded);
1591 printf("KLD file %s is missing dependencies\n", lf->filename);
1592 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1596 * We made it. Finish off the linking in the order we determined.
1598 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1599 if (linker_kernel_file) {
1600 linker_kernel_file->refs++;
1601 error = linker_file_add_dependency(lf,
1602 linker_kernel_file);
1604 panic("cannot add dependency");
1606 lf->userrefs++; /* so we can (try to) kldunload it */
1607 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1610 for (mdp = start; mdp < stop; mdp++) {
1612 if (mp->md_type != MDT_DEPEND)
1614 modname = mp->md_cval;
1615 verinfo = mp->md_data;
1616 mod = modlist_lookup2(modname, verinfo);
1618 printf("KLD file %s - cannot find "
1619 "dependency \"%s\"\n",
1620 lf->filename, modname);
1623 /* Don't count self-dependencies */
1624 if (lf == mod->container)
1626 mod->container->refs++;
1627 error = linker_file_add_dependency(lf,
1630 panic("cannot add dependency");
1634 * Now do relocation etc using the symbol search paths
1635 * established by the dependencies
1637 error = LINKER_LINK_PRELOAD_FINISH(lf);
1639 printf("KLD file %s - could not finalize loading\n",
1643 linker_file_register_modules(lf);
1644 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1645 &si_stop, NULL) == 0)
1646 sysinit_add(si_start, si_stop);
1647 linker_file_register_sysctls(lf);
1648 lf->flags |= LINKER_FILE_LINKED;
1651 TAILQ_REMOVE(&depended_files, lf, loaded);
1652 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1654 sx_xunlock(&kld_sx);
1655 /* woohoo! we made it! */
1658 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1661 * Search for a not-loaded module by name.
1663 * Modules may be found in the following locations:
1665 * - preloaded (result is just the module name) - on disk (result is full path
1668 * If the module name is qualified in any way (contains path, etc.) the we
1669 * simply return a copy of it.
1671 * The search path can be manipulated via sysctl. Note that we use the ';'
1672 * character as a separator to be consistent with the bootloader.
1675 static char linker_hintfile[] = "linker.hints";
1676 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1678 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1679 sizeof(linker_path), "module load search path");
1681 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1683 static char *linker_ext_list[] = {
1690 * Check if file actually exists either with or without extension listed in
1691 * the linker_ext_list. (probably should be generic for the rest of the
1695 linker_lookup_file(const char *path, int pathlen, const char *name,
1696 int namelen, struct vattr *vap)
1698 struct nameidata nd;
1699 struct thread *td = curthread; /* XXX */
1700 char *result, **cpp, *sep;
1701 int error, len, extlen, reclen, flags, vfslocked;
1705 for (cpp = linker_ext_list; *cpp; cpp++) {
1710 extlen++; /* trailing '\0' */
1711 sep = (path[pathlen - 1] != '/') ? "/" : "";
1713 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1714 result = malloc(reclen, M_LINKER, M_WAITOK);
1715 for (cpp = linker_ext_list; *cpp; cpp++) {
1716 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1717 namelen, name, *cpp);
1719 * Attempt to open the file, and return the path if
1720 * we succeed and it's a regular file.
1722 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
1724 error = vn_open(&nd, &flags, 0, NULL);
1726 vfslocked = NDHASGIANT(&nd);
1727 NDFREE(&nd, NDF_ONLY_PNBUF);
1728 type = nd.ni_vp->v_type;
1730 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1731 VOP_UNLOCK(nd.ni_vp, 0);
1732 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1733 VFS_UNLOCK_GIANT(vfslocked);
1738 free(result, M_LINKER);
1742 #define INT_ALIGN(base, ptr) ptr = \
1743 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1746 * Lookup KLD which contains requested module in the "linker.hints" file. If
1747 * version specification is available, then try to find the best KLD.
1748 * Otherwise just find the latest one.
1751 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1752 int modnamelen, struct mod_depend *verinfo)
1754 struct thread *td = curthread; /* XXX */
1755 struct ucred *cred = td ? td->td_ucred : NULL;
1756 struct nameidata nd;
1757 struct vattr vattr, mattr;
1758 u_char *hints = NULL;
1759 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1760 int error, ival, bestver, *intp, found, flags, clen, blen;
1765 bestver = found = 0;
1767 sep = (path[pathlen - 1] != '/') ? "/" : "";
1768 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1770 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1771 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1774 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
1776 error = vn_open(&nd, &flags, 0, NULL);
1779 vfslocked = NDHASGIANT(&nd);
1780 NDFREE(&nd, NDF_ONLY_PNBUF);
1781 if (nd.ni_vp->v_type != VREG)
1784 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1788 * XXX: we need to limit this number to some reasonable value
1790 if (vattr.va_size > 100 * 1024) {
1791 printf("hints file too large %ld\n", (long)vattr.va_size);
1794 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1797 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1798 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1801 VOP_UNLOCK(nd.ni_vp, 0);
1802 vn_close(nd.ni_vp, FREAD, cred, td);
1803 VFS_UNLOCK_GIANT(vfslocked);
1806 printf("can't read %zd\n", reclen);
1809 intp = (int *)hints;
1811 if (ival != LINKER_HINTS_VERSION) {
1812 printf("hints file version mismatch %d\n", ival);
1815 bufend = hints + vattr.va_size;
1816 recptr = (u_char *)intp;
1818 while (recptr < bufend && !found) {
1819 intp = (int *)recptr;
1826 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1829 INT_ALIGN(hints, cp);
1833 if (verinfo == NULL ||
1834 ival == verinfo->md_ver_preferred) {
1838 if (ival >= verinfo->md_ver_minimum &&
1839 ival <= verinfo->md_ver_maximum &&
1849 recptr += reclen + sizeof(int);
1852 * Finally check if KLD is in the place
1855 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1857 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1860 * KLD is newer than hints file. What we should do now?
1862 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1863 printf("warning: KLD '%s' is newer than the linker.hints"
1866 free(pathbuf, M_LINKER);
1868 free(hints, M_TEMP);
1869 if (nd.ni_vp != NULL) {
1870 VOP_UNLOCK(nd.ni_vp, 0);
1871 vn_close(nd.ni_vp, FREAD, cred, td);
1872 VFS_UNLOCK_GIANT(vfslocked);
1875 * If nothing found or hints is absent - fallback to the old
1876 * way by using "kldname[.ko]" as module name.
1878 if (!found && !bestver && result == NULL)
1879 result = linker_lookup_file(path, pathlen, modname,
1885 * Lookup KLD which contains requested module in the all directories.
1888 linker_search_module(const char *modname, int modnamelen,
1889 struct mod_depend *verinfo)
1891 char *cp, *ep, *result;
1894 * traverse the linker path
1896 for (cp = linker_path; *cp; cp = ep + 1) {
1897 /* find the end of this component */
1898 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1899 result = linker_hints_lookup(cp, ep - cp, modname,
1900 modnamelen, verinfo);
1910 * Search for module in all directories listed in the linker_path.
1913 linker_search_kld(const char *name)
1915 char *cp, *ep, *result;
1918 /* qualified at all? */
1919 if (index(name, '/'))
1920 return (linker_strdup(name));
1922 /* traverse the linker path */
1924 for (ep = linker_path; *ep; ep++) {
1926 /* find the end of this component */
1927 for (; *ep != 0 && *ep != ';'; ep++);
1928 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1936 linker_basename(const char *path)
1938 const char *filename;
1940 filename = rindex(path, '/');
1941 if (filename == NULL)
1950 * Inform hwpmc about the set of kernel modules currently loaded.
1953 linker_hwpmc_list_objects(void)
1956 struct pmckern_map_in *kobase;
1961 TAILQ_FOREACH(lf, &linker_files, link)
1964 /* Allocate nmappings + 1 entries. */
1965 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1966 M_LINKER, M_WAITOK | M_ZERO);
1968 TAILQ_FOREACH(lf, &linker_files, link) {
1970 /* Save the info for this linker file. */
1971 kobase[i].pm_file = lf->filename;
1972 kobase[i].pm_address = (uintptr_t)lf->address;
1975 sx_sunlock(&kld_sx);
1977 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
1979 /* The last entry of the malloced area comprises of all zeros. */
1980 KASSERT(kobase[i].pm_file == NULL,
1981 ("linker_hwpmc_list_objects: last object not NULL"));
1983 return ((void *)kobase);
1988 * Find a file which contains given module and load it, if "parent" is not
1989 * NULL, register a reference to it.
1992 linker_load_module(const char *kldname, const char *modname,
1993 struct linker_file *parent, struct mod_depend *verinfo,
1994 struct linker_file **lfpp)
1996 linker_file_t lfdep;
1997 const char *filename;
2001 sx_assert(&kld_sx, SA_XLOCKED);
2002 if (modname == NULL) {
2004 * We have to load KLD
2006 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2008 pathname = linker_search_kld(kldname);
2010 if (modlist_lookup2(modname, verinfo) != NULL)
2012 if (kldname != NULL)
2013 pathname = linker_strdup(kldname);
2014 else if (rootvnode == NULL)
2018 * Need to find a KLD with required module
2020 pathname = linker_search_module(modname,
2021 strlen(modname), verinfo);
2023 if (pathname == NULL)
2027 * Can't load more than one file with the same basename XXX:
2028 * Actually it should be possible to have multiple KLDs with
2029 * the same basename but different path because they can
2030 * provide different versions of the same modules.
2032 filename = linker_basename(pathname);
2033 if (linker_find_file_by_name(filename))
2036 error = linker_load_file(pathname, &lfdep);
2039 if (modname && verinfo &&
2040 modlist_lookup2(modname, verinfo) == NULL) {
2041 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2046 error = linker_file_add_dependency(parent, lfdep);
2053 free(pathname, M_LINKER);
2058 * This routine is responsible for finding dependencies of userland initiated
2059 * kldload(2)'s of files.
2062 linker_load_dependencies(linker_file_t lf)
2064 linker_file_t lfdep;
2065 struct mod_metadata **start, **stop, **mdp, **nmdp;
2066 struct mod_metadata *mp, *nmp;
2067 struct mod_depend *verinfo;
2069 const char *modname, *nmodname;
2070 int ver, error = 0, count;
2073 * All files are dependant on /kernel.
2075 sx_assert(&kld_sx, SA_XLOCKED);
2076 if (linker_kernel_file) {
2077 linker_kernel_file->refs++;
2078 error = linker_file_add_dependency(lf, linker_kernel_file);
2082 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2085 for (mdp = start; mdp < stop; mdp++) {
2087 if (mp->md_type != MDT_VERSION)
2089 modname = mp->md_cval;
2090 ver = ((struct mod_version *)mp->md_data)->mv_version;
2091 mod = modlist_lookup(modname, ver);
2093 printf("interface %s.%d already present in the KLD"
2094 " '%s'!\n", modname, ver,
2095 mod->container->filename);
2100 for (mdp = start; mdp < stop; mdp++) {
2102 if (mp->md_type != MDT_DEPEND)
2104 modname = mp->md_cval;
2105 verinfo = mp->md_data;
2107 for (nmdp = start; nmdp < stop; nmdp++) {
2109 if (nmp->md_type != MDT_VERSION)
2111 nmodname = nmp->md_cval;
2112 if (strcmp(modname, nmodname) == 0)
2115 if (nmdp < stop)/* early exit, it's a self reference */
2117 mod = modlist_lookup2(modname, verinfo);
2118 if (mod) { /* woohoo, it's loaded already */
2119 lfdep = mod->container;
2121 error = linker_file_add_dependency(lf, lfdep);
2126 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2128 printf("KLD %s: depends on %s - not available or"
2129 " version mismatch\n", lf->filename, modname);
2136 linker_addmodules(lf, start, stop, 0);
2141 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2143 struct sysctl_req *req;
2146 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2150 * Export a nul-separated, double-nul-terminated list of all function names
2154 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2160 error = mac_kld_check_stat(req->td->td_ucred);
2164 error = sysctl_wire_old_buffer(req, 0);
2168 TAILQ_FOREACH(lf, &linker_files, link) {
2169 error = LINKER_EACH_FUNCTION_NAME(lf,
2170 sysctl_kern_function_list_iterate, req);
2172 sx_xunlock(&kld_sx);
2176 sx_xunlock(&kld_sx);
2177 return (SYSCTL_OUT(req, "", 1));
2180 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD,
2181 NULL, 0, sysctl_kern_function_list, "", "kernel function list");