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$");
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>
41 #include <sys/mutex.h>
44 #include <sys/module.h>
45 #include <sys/linker.h>
46 #include <sys/fcntl.h>
47 #include <sys/libkern.h>
48 #include <sys/namei.h>
49 #include <sys/vnode.h>
50 #include <sys/sysctl.h>
52 #include "linker_if.h"
59 * static char *linker_search_path(const char *name, struct mod_depend
62 static const char *linker_basename(const char *path);
64 /* Metadata from the static kernel */
65 SET_DECLARE(modmetadata_set, struct mod_metadata);
67 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
69 linker_file_t linker_kernel_file;
71 static struct mtx kld_mtx; /* kernel linker mutex */
73 static linker_class_list_t classes;
74 static linker_file_list_t linker_files;
75 static int next_file_id = 1;
76 static int linker_no_more_classes = 0;
78 #define LINKER_GET_NEXT_FILE_ID(a) do { \
79 linker_file_t lftmp; \
83 TAILQ_FOREACH(lftmp, &linker_files, link) { \
84 if (next_file_id == lftmp->id) { \
86 mtx_unlock(&kld_mtx); \
91 mtx_unlock(&kld_mtx); /* Hold for safe read of id variable */ \
95 /* XXX wrong name; we're looking at version provision tags here, not modules */
96 typedef TAILQ_HEAD(, modlist) modlisthead_t;
98 TAILQ_ENTRY(modlist) link; /* chain together all modules */
99 linker_file_t container;
103 typedef struct modlist *modlist_t;
104 static modlisthead_t found_modules;
106 static modlist_t modlist_lookup2(const char *name,
107 struct mod_depend *verinfo);
110 linker_strdup(const char *str)
114 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
120 linker_init(void *arg)
123 mtx_init(&kld_mtx, "kernel linker", NULL, MTX_DEF);
124 TAILQ_INIT(&classes);
125 TAILQ_INIT(&linker_files);
128 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0)
131 linker_stop_class_add(void *arg)
134 linker_no_more_classes = 1;
137 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL)
140 linker_add_class(linker_class_t lc)
144 * We disallow any class registration past SI_ORDER_ANY
145 * of SI_SUB_KLD. We bump the reference count to keep the
146 * ops from being freed.
148 if (linker_no_more_classes == 1)
150 kobj_class_compile((kobj_class_t) lc);
151 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
152 TAILQ_INSERT_TAIL(&classes, lc, link);
157 linker_file_sysinit(linker_file_t lf)
159 struct sysinit **start, **stop, **sipp, **xipp, *save;
161 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
164 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
167 * Perform a bubble sort of the system initialization objects by
168 * their subsystem (primary key) and order (secondary key).
170 * Since some things care about execution order, this is the operation
171 * which ensures continued function.
173 for (sipp = start; sipp < stop; sipp++) {
174 for (xipp = sipp + 1; xipp < stop; xipp++) {
175 if ((*sipp)->subsystem < (*xipp)->subsystem ||
176 ((*sipp)->subsystem == (*xipp)->subsystem &&
177 (*sipp)->order <= (*xipp)->order))
186 * Traverse the (now) ordered list of system initialization tasks.
187 * Perform each task, and continue on to the next task.
189 for (sipp = start; sipp < stop; sipp++) {
190 if ((*sipp)->subsystem == SI_SUB_DUMMY)
191 continue; /* skip dummy task(s) */
194 (*((*sipp)->func)) ((*sipp)->udata);
199 linker_file_sysuninit(linker_file_t lf)
201 struct sysinit **start, **stop, **sipp, **xipp, *save;
203 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
206 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
211 * Perform a reverse bubble sort of the system initialization objects
212 * by their subsystem (primary key) and order (secondary key).
214 * Since some things care about execution order, this is the operation
215 * which ensures continued function.
217 for (sipp = start; sipp < stop; sipp++) {
218 for (xipp = sipp + 1; xipp < stop; xipp++) {
219 if ((*sipp)->subsystem > (*xipp)->subsystem ||
220 ((*sipp)->subsystem == (*xipp)->subsystem &&
221 (*sipp)->order >= (*xipp)->order))
230 * Traverse the (now) ordered list of system initialization tasks.
231 * Perform each task, and continue on to the next task.
233 for (sipp = start; sipp < stop; sipp++) {
234 if ((*sipp)->subsystem == SI_SUB_DUMMY)
235 continue; /* skip dummy task(s) */
238 (*((*sipp)->func)) ((*sipp)->udata);
243 linker_file_register_sysctls(linker_file_t lf)
245 struct sysctl_oid **start, **stop, **oidp;
248 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
251 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
254 for (oidp = start; oidp < stop; oidp++)
255 sysctl_register_oid(*oidp);
259 linker_file_unregister_sysctls(linker_file_t lf)
261 struct sysctl_oid **start, **stop, **oidp;
263 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
264 " for %s\n", lf->filename));
266 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
269 for (oidp = start; oidp < stop; oidp++)
270 sysctl_unregister_oid(*oidp);
274 linker_file_register_modules(linker_file_t lf)
276 struct mod_metadata **start, **stop, **mdp;
277 const moduledata_t *moddata;
278 int first_error, error;
280 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
281 " in %s\n", lf->filename));
283 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
286 * This fallback should be unnecessary, but if we get booted
287 * from boot2 instead of loader and we are missing our
288 * metadata then we have to try the best we can.
290 if (lf == linker_kernel_file) {
291 start = SET_BEGIN(modmetadata_set);
292 stop = SET_LIMIT(modmetadata_set);
297 for (mdp = start; mdp < stop; mdp++) {
298 if ((*mdp)->md_type != MDT_MODULE)
300 moddata = (*mdp)->md_data;
301 KLD_DPF(FILE, ("Registering module %s in %s\n",
302 moddata->name, lf->filename));
303 error = module_register(moddata, lf);
305 printf("Module %s failed to register: %d\n",
306 moddata->name, error);
307 if (first_error == 0)
311 return (first_error);
315 linker_init_kernel_modules(void)
318 linker_file_register_modules(linker_kernel_file);
321 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0)
324 linker_load_file(const char *filename, linker_file_t *result)
328 int foundfile, error = 0;
330 /* Refuse to load modules if securelevel raised */
334 lf = linker_find_file_by_name(filename);
336 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
337 " incrementing refs\n", filename));
346 * We do not need to protect (lock) classes here because there is
347 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
348 * and there is no class deregistration mechanism at this time.
350 TAILQ_FOREACH(lc, &classes, link) {
351 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
353 error = LINKER_LOAD_FILE(lc, filename, &lf);
355 * If we got something other than ENOENT, then it exists but
356 * we cannot load it for some other reason.
361 error = linker_file_register_modules(lf);
362 if (error == EEXIST) {
363 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
366 linker_file_register_sysctls(lf);
367 linker_file_sysinit(lf);
368 lf->flags |= LINKER_FILE_LINKED;
375 * Less than ideal, but tells the user whether it failed to load or
376 * the module was not found.
380 * Format not recognized or otherwise unloadable.
381 * When loading a module that is statically built into
382 * the kernel EEXIST percolates back up as the return
383 * value. Preserve this so that apps like sysinstall
384 * can recognize this special case and not post bogus
390 error = ENOENT; /* Nothing found */
396 linker_reference_module(const char *modname, struct mod_depend *verinfo,
397 linker_file_t *result)
401 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
402 *result = mod->container;
407 return (linker_load_module(NULL, modname, NULL, verinfo, result));
411 linker_find_file_by_name(const char *filename)
413 linker_file_t lf = 0;
416 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
419 sprintf(koname, "%s.ko", filename);
422 TAILQ_FOREACH(lf, &linker_files, link) {
423 if (strcmp(lf->filename, koname) == 0)
425 if (strcmp(lf->filename, filename) == 0)
428 mtx_unlock(&kld_mtx);
431 free(koname, M_LINKER);
436 linker_find_file_by_id(int fileid)
438 linker_file_t lf = 0;
441 TAILQ_FOREACH(lf, &linker_files, link)
442 if (lf->id == fileid)
444 mtx_unlock(&kld_mtx);
449 linker_make_file(const char *pathname, linker_class_t lc)
452 const char *filename;
455 filename = linker_basename(pathname);
457 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
458 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
464 lf->filename = linker_strdup(filename);
465 LINKER_GET_NEXT_FILE_ID(lf->id);
468 STAILQ_INIT(&lf->common);
469 TAILQ_INIT(&lf->modules);
471 TAILQ_INSERT_TAIL(&linker_files, lf, link);
472 mtx_unlock(&kld_mtx);
478 linker_file_unload(linker_file_t file, int flags)
481 modlist_t ml, nextml;
482 struct common_symbol *cp;
487 /* Refuse to unload modules if securelevel raised. */
491 error = mac_check_kld_unload(curthread->td_ucred);
496 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
497 if (file->refs == 1) {
498 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
499 " informing modules\n"));
502 * Inform any modules associated with this file.
505 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
506 next = module_getfnext(mod);
510 * Give the module a chance to veto the unload.
512 if ((error = module_unload(mod, flags)) != 0) {
513 KLD_DPF(FILE, ("linker_file_unload: module %p"
514 " vetoes unload\n", mod));
523 if (file->refs > 0) {
526 for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) {
527 nextml = TAILQ_NEXT(ml, link);
528 if (ml->container == file) {
529 TAILQ_REMOVE(&found_modules, ml, link);
535 * Don't try to run SYSUNINITs if we are unloaded due to a
538 if (file->flags & LINKER_FILE_LINKED) {
539 linker_file_sysuninit(file);
540 linker_file_unregister_sysctls(file);
543 TAILQ_REMOVE(&linker_files, file, link);
544 mtx_unlock(&kld_mtx);
547 for (i = 0; i < file->ndeps; i++)
548 linker_file_unload(file->deps[i], flags);
549 free(file->deps, M_LINKER);
552 for (cp = STAILQ_FIRST(&file->common); cp;
553 cp = STAILQ_FIRST(&file->common)) {
554 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
559 if (file->filename) {
560 free(file->filename, M_LINKER);
561 file->filename = NULL;
563 kobj_delete((kobj_t) file, M_LINKER);
569 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
571 linker_file_t *newdeps;
573 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
574 M_LINKER, M_WAITOK | M_ZERO);
579 bcopy(file->deps, newdeps,
580 file->ndeps * sizeof(linker_file_t *));
581 free(file->deps, M_LINKER);
583 file->deps = newdeps;
584 file->deps[file->ndeps] = dep;
590 * Locate a linker set and its contents. This is a helper function to avoid
591 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void ***
594 linker_file_lookup_set(linker_file_t file, const char *name,
595 void *firstp, void *lastp, int *countp)
598 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
602 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
605 linker_symval_t symval;
607 size_t common_size = 0;
610 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
613 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
614 LINKER_SYMBOL_VALUES(file, sym, &symval);
615 if (symval.value == 0)
617 * For commons, first look them up in the
618 * dependencies and only allocate space if not found
621 common_size = symval.size;
623 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
624 ".value=%p\n", symval.value));
625 return (symval.value);
629 for (i = 0; i < file->ndeps; i++) {
630 address = linker_file_lookup_symbol(file->deps[i],
633 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
634 " deps value=%p\n", address));
639 if (common_size > 0) {
641 * This is a common symbol which was not found in the
642 * dependencies. We maintain a simple common symbol table in
645 struct common_symbol *cp;
647 STAILQ_FOREACH(cp, &file->common, link) {
648 if (strcmp(cp->name, name) == 0) {
649 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
650 " old common value=%p\n", cp->address));
651 return (cp->address);
655 * Round the symbol size up to align.
657 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
658 cp = malloc(sizeof(struct common_symbol)
659 + common_size + strlen(name) + 1, M_LINKER,
662 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
665 cp->address = (caddr_t)(cp + 1);
666 cp->name = cp->address + common_size;
667 strcpy(cp->name, name);
668 bzero(cp->address, common_size);
669 STAILQ_INSERT_TAIL(&file->common, cp, link);
671 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
672 " value=%p\n", cp->address));
673 return (cp->address);
675 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
681 * DDB Helpers. DDB has to look across multiple files with their own symbol
682 * tables and string tables.
684 * Note that we do not obey list locking protocols here. We really don't need
685 * DDB to hang because somebody's got the lock held. We'll take the chance
686 * that the files list is inconsistant instead.
690 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
694 TAILQ_FOREACH(lf, &linker_files, link) {
695 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
702 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
705 c_linker_sym_t best, es;
706 u_long diff, bestdiff, off;
709 off = (uintptr_t)value;
711 TAILQ_FOREACH(lf, &linker_files, link) {
712 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
714 if (es != 0 && diff < bestdiff) {
733 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
737 TAILQ_FOREACH(lf, &linker_files, link) {
738 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
752 kldload(struct thread *td, struct kldload_args *uap)
754 char *kldname, *modname;
755 char *pathname = NULL;
759 td->td_retval[0] = -1;
763 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
766 if ((error = suser(td)) != 0)
769 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
770 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
774 * If path do not contain qualified name or any dot in it
775 * (kldname.ko, or kldname.ver.ko) treat it as interface
778 if (index(pathname, '/') || index(pathname, '.')) {
785 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
790 td->td_retval[0] = lf->id;
793 free(pathname, M_TEMP);
802 kern_kldunload(struct thread *td, int fileid, int flags)
809 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
812 if ((error = suser(td)) != 0)
815 lf = linker_find_file_by_id(fileid);
817 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
818 if (lf->userrefs == 0) {
820 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
822 printf("kldunload: attempt to unload file that was"
823 " loaded by the kernel\n");
828 error = linker_file_unload(lf, flags);
842 kldunload(struct thread *td, struct kldunload_args *uap)
845 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
852 kldunloadf(struct thread *td, struct kldunloadf_args *uap)
855 if (uap->flags != LINKER_UNLOAD_NORMAL &&
856 uap->flags != LINKER_UNLOAD_FORCE)
858 return (kern_kldunload(td, uap->fileid, uap->flags));
865 kldfind(struct thread *td, struct kldfind_args *uap)
868 const char *filename;
873 error = mac_check_kld_stat(td->td_ucred);
879 td->td_retval[0] = -1;
881 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
882 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
885 filename = linker_basename(pathname);
886 lf = linker_find_file_by_name(filename);
888 td->td_retval[0] = lf->id;
893 free(pathname, M_TEMP);
902 kldnext(struct thread *td, struct kldnext_args *uap)
908 error = mac_check_kld_stat(td->td_ucred);
915 if (uap->fileid == 0) {
917 if (TAILQ_FIRST(&linker_files))
918 td->td_retval[0] = TAILQ_FIRST(&linker_files)->id;
920 td->td_retval[0] = 0;
921 mtx_unlock(&kld_mtx);
924 lf = linker_find_file_by_id(uap->fileid);
926 if (TAILQ_NEXT(lf, link))
927 td->td_retval[0] = TAILQ_NEXT(lf, link)->id;
929 td->td_retval[0] = 0;
941 kldstat(struct thread *td, struct kldstat_args *uap)
945 int namelen, version;
946 struct kld_file_stat *stat;
949 error = mac_check_kld_stat(td->td_ucred);
956 lf = linker_find_file_by_id(uap->fileid);
964 * Check the version of the user's structure.
966 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
968 if (version != sizeof(struct kld_file_stat)) {
972 namelen = strlen(lf->filename) + 1;
973 if (namelen > MAXPATHLEN)
974 namelen = MAXPATHLEN;
975 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
977 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
979 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
981 if ((error = copyout(&lf->address, &stat->address,
982 sizeof(caddr_t))) != 0)
984 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
987 td->td_retval[0] = 0;
997 kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1004 error = mac_check_kld_stat(td->td_ucred);
1010 lf = linker_find_file_by_id(uap->fileid);
1013 mp = TAILQ_FIRST(&lf->modules);
1015 td->td_retval[0] = module_getid(mp);
1017 td->td_retval[0] = 0;
1029 kldsym(struct thread *td, struct kldsym_args *uap)
1031 char *symstr = NULL;
1033 linker_symval_t symval;
1035 struct kld_sym_lookup lookup;
1039 error = mac_check_kld_stat(td->td_ucred);
1046 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1048 if (lookup.version != sizeof(lookup) ||
1049 uap->cmd != KLDSYM_LOOKUP) {
1053 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1054 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1056 if (uap->fileid != 0) {
1057 lf = linker_find_file_by_id(uap->fileid);
1062 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1063 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1064 lookup.symvalue = (uintptr_t) symval.value;
1065 lookup.symsize = symval.size;
1066 error = copyout(&lookup, uap->data, sizeof(lookup));
1071 TAILQ_FOREACH(lf, &linker_files, link) {
1072 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1073 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1074 lookup.symvalue = (uintptr_t)symval.value;
1075 lookup.symsize = symval.size;
1076 error = copyout(&lookup, uap->data,
1081 mtx_unlock(&kld_mtx);
1087 free(symstr, M_TEMP);
1093 * Preloaded module support
1097 modlist_lookup(const char *name, int ver)
1101 TAILQ_FOREACH(mod, &found_modules, link) {
1102 if (strcmp(mod->name, name) == 0 &&
1103 (ver == 0 || mod->version == ver))
1110 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1112 modlist_t mod, bestmod;
1115 if (verinfo == NULL)
1116 return (modlist_lookup(name, 0));
1118 for (mod = TAILQ_FIRST(&found_modules); mod;
1119 mod = TAILQ_NEXT(mod, link)) {
1120 if (strcmp(mod->name, name) != 0)
1123 if (ver == verinfo->md_ver_preferred)
1125 if (ver >= verinfo->md_ver_minimum &&
1126 ver <= verinfo->md_ver_maximum &&
1127 (bestmod == NULL || ver > bestmod->version))
1134 modlist_newmodule(const char *modname, int version, linker_file_t container)
1138 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1140 panic("no memory for module list");
1141 mod->container = container;
1142 mod->name = modname;
1143 mod->version = version;
1144 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1149 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1150 struct mod_metadata **stop, int preload)
1152 struct mod_metadata *mp, **mdp;
1153 const char *modname;
1156 for (mdp = start; mdp < stop; mdp++) {
1158 if (mp->md_type != MDT_VERSION)
1160 modname = mp->md_cval;
1161 ver = ((struct mod_version *)mp->md_data)->mv_version;
1162 if (modlist_lookup(modname, ver) != NULL) {
1163 printf("module %s already present!\n", modname);
1164 /* XXX what can we do? this is a build error. :-( */
1167 modlist_newmodule(modname, ver, lf);
1172 linker_preload(void *arg)
1175 const char *modname, *nmodname;
1180 linker_file_list_t loaded_files;
1181 linker_file_list_t depended_files;
1182 struct mod_metadata *mp, *nmp;
1183 struct mod_metadata **start, **stop, **mdp, **nmdp;
1184 struct mod_depend *verinfo;
1188 struct sysinit **si_start, **si_stop;
1190 TAILQ_INIT(&loaded_files);
1191 TAILQ_INIT(&depended_files);
1192 TAILQ_INIT(&found_modules);
1196 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1197 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1198 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1199 if (modname == NULL) {
1200 printf("Preloaded module at %p does not have a"
1201 " name!\n", modptr);
1204 if (modtype == NULL) {
1205 printf("Preloaded module at %p does not have a type!\n",
1210 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1213 TAILQ_FOREACH(lc, &classes, link) {
1214 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1220 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1224 * First get a list of stuff in the kernel.
1226 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1228 linker_addmodules(linker_kernel_file, start, stop, 1);
1231 * this is a once-off kinky bubble sort resolve relocation dependency
1235 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1236 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1239 * First, look to see if we would successfully link with this
1242 resolves = 1; /* unless we know otherwise */
1244 for (mdp = start; mdp < stop; mdp++) {
1246 if (mp->md_type != MDT_DEPEND)
1248 modname = mp->md_cval;
1249 verinfo = mp->md_data;
1250 for (nmdp = start; nmdp < stop; nmdp++) {
1252 if (nmp->md_type != MDT_VERSION)
1254 nmodname = nmp->md_cval;
1255 if (strcmp(modname, nmodname) == 0)
1258 if (nmdp < stop) /* it's a self reference */
1262 * ok, the module isn't here yet, we
1265 if (modlist_lookup2(modname, verinfo) == NULL)
1270 * OK, if we found our modules, we can link. So, "provide"
1271 * the modules inside and add it to the end of the link order
1276 for (mdp = start; mdp < stop; mdp++) {
1278 if (mp->md_type != MDT_VERSION)
1280 modname = mp->md_cval;
1281 nver = ((struct mod_version *)
1282 mp->md_data)->mv_version;
1283 if (modlist_lookup(modname,
1285 printf("module %s already"
1286 " present!\n", modname);
1287 linker_file_unload(lf,
1288 LINKER_UNLOAD_FORCE);
1289 TAILQ_REMOVE(&loaded_files,
1291 /* we changed tailq next ptr */
1294 modlist_newmodule(modname, nver, lf);
1297 TAILQ_REMOVE(&loaded_files, lf, loaded);
1298 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1300 * Since we provided modules, we need to restart the
1301 * sort so that the previous files that depend on us
1302 * have a chance. Also, we've busted the tailq next
1303 * pointer with the REMOVE.
1310 * At this point, we check to see what could not be resolved..
1312 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1313 printf("KLD file %s is missing dependencies\n", lf->filename);
1314 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1315 TAILQ_REMOVE(&loaded_files, lf, loaded);
1319 * We made it. Finish off the linking in the order we determined.
1321 TAILQ_FOREACH(lf, &depended_files, loaded) {
1322 if (linker_kernel_file) {
1323 linker_kernel_file->refs++;
1324 error = linker_file_add_dependency(lf,
1325 linker_kernel_file);
1327 panic("cannot add dependency");
1329 lf->userrefs++; /* so we can (try to) kldunload it */
1330 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1333 for (mdp = start; mdp < stop; mdp++) {
1335 if (mp->md_type != MDT_DEPEND)
1337 modname = mp->md_cval;
1338 verinfo = mp->md_data;
1339 mod = modlist_lookup2(modname, verinfo);
1340 /* Don't count self-dependencies */
1341 if (lf == mod->container)
1343 mod->container->refs++;
1344 error = linker_file_add_dependency(lf,
1347 panic("cannot add dependency");
1351 * Now do relocation etc using the symbol search paths
1352 * established by the dependencies
1354 error = LINKER_LINK_PRELOAD_FINISH(lf);
1356 printf("KLD file %s - could not finalize loading\n",
1358 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1361 linker_file_register_modules(lf);
1362 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1363 &si_stop, NULL) == 0)
1364 sysinit_add(si_start, si_stop);
1365 linker_file_register_sysctls(lf);
1366 lf->flags |= LINKER_FILE_LINKED;
1368 /* woohoo! we made it! */
1371 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0)
1374 * Search for a not-loaded module by name.
1376 * Modules may be found in the following locations:
1378 * - preloaded (result is just the module name) - on disk (result is full path
1381 * If the module name is qualified in any way (contains path, etc.) the we
1382 * simply return a copy of it.
1384 * The search path can be manipulated via sysctl. Note that we use the ';'
1385 * character as a separator to be consistent with the bootloader.
1388 static char linker_hintfile[] = "linker.hints";
1389 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1391 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1392 sizeof(linker_path), "module load search path");
1394 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1396 static char *linker_ext_list[] = {
1403 * Check if file actually exists either with or without extension listed in
1404 * the linker_ext_list. (probably should be generic for the rest of the
1408 linker_lookup_file(const char *path, int pathlen, const char *name,
1409 int namelen, struct vattr *vap)
1411 struct nameidata nd;
1412 struct thread *td = curthread; /* XXX */
1413 char *result, **cpp, *sep;
1414 int error, len, extlen, reclen, flags;
1418 for (cpp = linker_ext_list; *cpp; cpp++) {
1423 extlen++; /* trailing '\0' */
1424 sep = (path[pathlen - 1] != '/') ? "/" : "";
1426 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1427 result = malloc(reclen, M_LINKER, M_WAITOK);
1428 for (cpp = linker_ext_list; *cpp; cpp++) {
1429 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1430 namelen, name, *cpp);
1432 * Attempt to open the file, and return the path if
1433 * we succeed and it's a regular file.
1435 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1437 error = vn_open(&nd, &flags, 0, -1);
1439 NDFREE(&nd, NDF_ONLY_PNBUF);
1440 type = nd.ni_vp->v_type;
1442 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td);
1443 VOP_UNLOCK(nd.ni_vp, 0, td);
1444 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1449 free(result, M_LINKER);
1453 #define INT_ALIGN(base, ptr) ptr = \
1454 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1457 * Lookup KLD which contains requested module in the "linker.hints" file. If
1458 * version specification is available, then try to find the best KLD.
1459 * Otherwise just find the latest one.
1462 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1463 int modnamelen, struct mod_depend *verinfo)
1465 struct thread *td = curthread; /* XXX */
1466 struct ucred *cred = td ? td->td_ucred : NULL;
1467 struct nameidata nd;
1468 struct vattr vattr, mattr;
1469 u_char *hints = NULL;
1470 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1471 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1474 bestver = found = 0;
1476 sep = (path[pathlen - 1] != '/') ? "/" : "";
1477 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1479 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1480 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1483 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1485 error = vn_open(&nd, &flags, 0, -1);
1488 NDFREE(&nd, NDF_ONLY_PNBUF);
1489 if (nd.ni_vp->v_type != VREG)
1492 error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td);
1496 * XXX: we need to limit this number to some reasonable value
1498 if (vattr.va_size > 100 * 1024) {
1499 printf("hints file too large %ld\n", (long)vattr.va_size);
1502 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1505 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1506 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1509 VOP_UNLOCK(nd.ni_vp, 0, td);
1510 vn_close(nd.ni_vp, FREAD, cred, td);
1513 printf("can't read %d\n", reclen);
1516 intp = (int *)hints;
1518 if (ival != LINKER_HINTS_VERSION) {
1519 printf("hints file version mismatch %d\n", ival);
1522 bufend = hints + vattr.va_size;
1523 recptr = (u_char *)intp;
1525 while (recptr < bufend && !found) {
1526 intp = (int *)recptr;
1533 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1536 INT_ALIGN(hints, cp);
1540 if (verinfo == NULL ||
1541 ival == verinfo->md_ver_preferred) {
1545 if (ival >= verinfo->md_ver_minimum &&
1546 ival <= verinfo->md_ver_maximum &&
1556 recptr += reclen + sizeof(int);
1559 * Finally check if KLD is in the place
1562 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1564 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1567 * KLD is newer than hints file. What we should do now?
1569 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1570 printf("warning: KLD '%s' is newer than the linker.hints"
1573 free(pathbuf, M_LINKER);
1575 free(hints, M_TEMP);
1576 if (nd.ni_vp != NULL) {
1577 VOP_UNLOCK(nd.ni_vp, 0, td);
1578 vn_close(nd.ni_vp, FREAD, cred, td);
1581 * If nothing found or hints is absent - fallback to the old
1582 * way by using "kldname[.ko]" as module name.
1584 if (!found && !bestver && result == NULL)
1585 result = linker_lookup_file(path, pathlen, modname,
1591 * Lookup KLD which contains requested module in the all directories.
1594 linker_search_module(const char *modname, int modnamelen,
1595 struct mod_depend *verinfo)
1597 char *cp, *ep, *result;
1600 * traverse the linker path
1602 for (cp = linker_path; *cp; cp = ep + 1) {
1603 /* find the end of this component */
1604 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1605 result = linker_hints_lookup(cp, ep - cp, modname,
1606 modnamelen, verinfo);
1616 * Search for module in all directories listed in the linker_path.
1619 linker_search_kld(const char *name)
1621 char *cp, *ep, *result, **cpp;
1624 /* qualified at all? */
1625 if (index(name, '/'))
1626 return (linker_strdup(name));
1629 for (cpp = linker_ext_list; *cpp; cpp++) {
1634 extlen++; /* trailing '\0' */
1636 /* traverse the linker path */
1638 for (ep = linker_path; *ep; ep++) {
1640 /* find the end of this component */
1641 for (; *ep != 0 && *ep != ';'; ep++);
1642 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1650 linker_basename(const char *path)
1652 const char *filename;
1654 filename = rindex(path, '/');
1655 if (filename == NULL)
1663 * Find a file which contains given module and load it, if "parent" is not
1664 * NULL, register a reference to it.
1667 linker_load_module(const char *kldname, const char *modname,
1668 struct linker_file *parent, struct mod_depend *verinfo,
1669 struct linker_file **lfpp)
1671 linker_file_t lfdep;
1672 const char *filename;
1676 if (modname == NULL) {
1678 * We have to load KLD
1680 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1682 pathname = linker_search_kld(kldname);
1684 if (modlist_lookup2(modname, verinfo) != NULL)
1686 if (kldname != NULL)
1687 pathname = linker_strdup(kldname);
1688 else if (rootvnode == NULL)
1692 * Need to find a KLD with required module
1694 pathname = linker_search_module(modname,
1695 strlen(modname), verinfo);
1697 if (pathname == NULL)
1701 * Can't load more than one file with the same basename XXX:
1702 * Actually it should be possible to have multiple KLDs with
1703 * the same basename but different path because they can
1704 * provide different versions of the same modules.
1706 filename = linker_basename(pathname);
1707 if (linker_find_file_by_name(filename)) {
1712 error = linker_load_file(pathname, &lfdep);
1715 if (modname && verinfo &&
1716 modlist_lookup2(modname, verinfo) == NULL) {
1717 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
1722 error = linker_file_add_dependency(parent, lfdep);
1731 free(pathname, M_LINKER);
1736 * This routine is responsible for finding dependencies of userland initiated
1737 * kldload(2)'s of files.
1740 linker_load_dependencies(linker_file_t lf)
1742 linker_file_t lfdep;
1743 struct mod_metadata **start, **stop, **mdp, **nmdp;
1744 struct mod_metadata *mp, *nmp;
1745 struct mod_depend *verinfo;
1747 const char *modname, *nmodname;
1748 int ver, error = 0, count;
1751 * All files are dependant on /kernel.
1753 if (linker_kernel_file) {
1754 linker_kernel_file->refs++;
1755 error = linker_file_add_dependency(lf, linker_kernel_file);
1759 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
1762 for (mdp = start; mdp < stop; mdp++) {
1764 if (mp->md_type != MDT_VERSION)
1766 modname = mp->md_cval;
1767 ver = ((struct mod_version *)mp->md_data)->mv_version;
1768 mod = modlist_lookup(modname, ver);
1770 printf("interface %s.%d already present in the KLD"
1771 " '%s'!\n", modname, ver,
1772 mod->container->filename);
1777 for (mdp = start; mdp < stop; mdp++) {
1779 if (mp->md_type != MDT_DEPEND)
1781 modname = mp->md_cval;
1782 verinfo = mp->md_data;
1784 for (nmdp = start; nmdp < stop; nmdp++) {
1786 if (nmp->md_type != MDT_VERSION)
1788 nmodname = nmp->md_cval;
1789 if (strcmp(modname, nmodname) == 0)
1792 if (nmdp < stop)/* early exit, it's a self reference */
1794 mod = modlist_lookup2(modname, verinfo);
1795 if (mod) { /* woohoo, it's loaded already */
1796 lfdep = mod->container;
1798 error = linker_file_add_dependency(lf, lfdep);
1803 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
1805 printf("KLD %s: depends on %s - not available\n",
1806 lf->filename, modname);
1813 linker_addmodules(lf, start, stop, 0);
1818 sysctl_kern_function_list_iterate(const char *name, void *opaque)
1820 struct sysctl_req *req;
1823 return (SYSCTL_OUT(req, name, strlen(name) + 1));
1827 * Export a nul-separated, double-nul-terminated list of all function names
1831 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
1837 error = mac_check_kld_stat(req->td->td_ucred);
1841 error = sysctl_wire_old_buffer(req, 0);
1845 TAILQ_FOREACH(lf, &linker_files, link) {
1846 error = LINKER_EACH_FUNCTION_NAME(lf,
1847 sysctl_kern_function_list_iterate, req);
1849 mtx_unlock(&kld_mtx);
1853 mtx_unlock(&kld_mtx);
1854 return (SYSCTL_OUT(req, "", 1));
1857 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
1858 NULL, 0, sysctl_kern_function_list, "", "kernel function list");