2 * Copyright (c) 1997 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
26 * $FreeBSD: src/sys/kern/kern_linker.c,v 1.41.2.3 2001/11/21 17:50:35 luigi Exp $
27 * $DragonFly: src/sys/kern/kern_linker.c,v 1.12 2003/10/13 04:16:51 hmp Exp $
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/sysproto.h>
37 #include <sys/sysent.h>
40 #include <sys/module.h>
41 #include <sys/linker.h>
42 #include <sys/fcntl.h>
43 #include <sys/libkern.h>
44 #include <sys/namei.h>
45 #include <sys/vnode.h>
46 #include <sys/sysctl.h>
48 #include <vm/vm_zone.h>
54 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
55 linker_file_t linker_current_file;
56 linker_file_t linker_kernel_file;
58 static struct lock lock; /* lock for the file list */
59 static linker_class_list_t classes;
60 static linker_file_list_t linker_files;
61 static int next_file_id = 1;
64 linker_init(void* arg)
66 lockinit(&lock, 0, "klink", 0, 0);
68 TAILQ_INIT(&linker_files);
71 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
74 linker_add_class(const char* desc, void* priv,
75 struct linker_class_ops* ops)
79 lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT);
82 bzero(lc, sizeof(*lc));
87 TAILQ_INSERT_HEAD(&classes, lc, link);
93 linker_file_sysinit(linker_file_t lf)
95 struct linker_set* sysinits;
96 struct sysinit** sipp;
97 struct sysinit** xipp;
99 const moduledata_t *moddata;
102 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
105 if (linker_file_lookup_symbol(lf, "sysinit_set", 0, (caddr_t *)&sysinits)) {
106 KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs not found\n"));
107 return 0; /* XXX is this correct ? No sysinit ? */
109 KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits));
110 if (sysinits == NULL)
111 return 0; /* XXX is this correct ? No sysinit ? */
114 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
115 if ((*sipp)->func == module_register_init) {
116 moddata = (*sipp)->udata;
117 error = module_register(moddata, lf);
119 printf("linker_file_sysinit \"%s\" failed to register! %d\n",
120 lf->filename, error);
127 * Perform a bubble sort of the system initialization objects by
128 * their subsystem (primary key) and order (secondary key).
130 * Since some things care about execution order, this is the
131 * operation which ensures continued function.
133 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
134 for (xipp = sipp + 1; *xipp; xipp++) {
135 if ((*sipp)->subsystem < (*xipp)->subsystem ||
136 ((*sipp)->subsystem == (*xipp)->subsystem &&
137 (*sipp)->order <= (*xipp)->order))
147 * Traverse the (now) ordered list of system initialization tasks.
148 * Perform each task, and continue on to the next task.
150 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
151 if ((*sipp)->subsystem == SI_SUB_DUMMY)
152 continue; /* skip dummy task(s)*/
155 (*((*sipp)->func))((*sipp)->udata);
157 return 0; /* no errors */
161 linker_file_sysuninit(linker_file_t lf)
163 struct linker_set* sysuninits;
164 struct sysinit** sipp;
165 struct sysinit** xipp;
166 struct sysinit* save;
168 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
171 if (linker_file_lookup_symbol(lf, "sysuninit_set", 0, (caddr_t *)&sysuninits)) {
172 KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs not found\n"));
175 KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits));
176 if (sysuninits == NULL)
180 * Perform a reverse bubble sort of the system initialization objects
181 * by their subsystem (primary key) and order (secondary key).
183 * Since some things care about execution order, this is the
184 * operation which ensures continued function.
186 for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
187 for (xipp = sipp + 1; *xipp; xipp++) {
188 if ((*sipp)->subsystem > (*xipp)->subsystem ||
189 ((*sipp)->subsystem == (*xipp)->subsystem &&
190 (*sipp)->order >= (*xipp)->order))
200 * Traverse the (now) ordered list of system initialization tasks.
201 * Perform each task, and continue on to the next task.
203 for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
204 if ((*sipp)->subsystem == SI_SUB_DUMMY)
205 continue; /* skip dummy task(s)*/
208 (*((*sipp)->func))((*sipp)->udata);
213 linker_file_register_sysctls(linker_file_t lf)
215 struct linker_set* sysctls;
217 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
220 if (linker_file_lookup_symbol(lf, "sysctl_set", 0, (caddr_t *)&sysctls)) {
221 KLD_DPF(FILE, ("linker_file_register_sysctls: SYSCTLs not found\n"));
224 KLD_DPF(FILE, ("linker_file_register_sysctls: SYSCTLs %p\n", sysctls));
228 sysctl_register_set(sysctls);
232 linker_file_unregister_sysctls(linker_file_t lf)
234 struct linker_set* sysctls;
236 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
239 if (linker_file_lookup_symbol(lf, "sysctl_set", 0, (caddr_t *)&sysctls)) {
240 KLD_DPF(FILE, ("linker_file_unregister_sysctls: SYSCTLs not found\n"));
243 KLD_DPF(FILE, ("linker_file_unregister_sysctls: SYSCTLs %p\n", sysctls));
247 sysctl_unregister_set(sysctls);
251 linker_load_file(const char* filename, linker_file_t* result)
255 int foundfile, error = 0;
258 /* Refuse to load modules if securelevel raised */
262 lf = linker_find_file_by_name(filename);
264 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
270 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
271 if (koname == NULL) {
275 sprintf(koname, "%s.ko", filename);
278 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
279 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
280 filename, lc->desc));
282 error = lc->ops->load_file(koname, &lf); /* First with .ko */
283 if (lf == NULL && error == ENOENT)
284 error = lc->ops->load_file(filename, &lf); /* Then try without */
286 * If we got something other than ENOENT, then it exists but we cannot
287 * load it for some other reason.
292 linker_file_register_sysctls(lf);
293 error = linker_file_sysinit(lf);
300 * Less than ideal, but tells the user whether it failed to load or
301 * the module was not found.
305 * Format not recognized or otherwise unloadable.
306 * When loading a module that is statically built into
307 * the kernel EEXIST percolates back up as the return
308 * value. Preserve this so that apps like sysinstall
309 * can recognize this special case and not post bogus
315 error = ENOENT; /* Nothing found */
319 free(koname, M_LINKER);
324 linker_find_file_by_name(const char* filename)
326 linker_file_t lf = 0;
329 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
332 sprintf(koname, "%s.ko", filename);
334 lockmgr(&lock, LK_SHARED, 0, curthread);
335 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
336 if (!strcmp(lf->filename, koname))
338 if (!strcmp(lf->filename, filename))
341 lockmgr(&lock, LK_RELEASE, 0, curthread);
345 free(koname, M_LINKER);
350 linker_find_file_by_id(int fileid)
352 linker_file_t lf = 0;
354 lockmgr(&lock, LK_SHARED, 0, curthread);
355 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link))
356 if (lf->id == fileid)
358 lockmgr(&lock, LK_RELEASE, 0, curthread);
364 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
366 linker_file_t lf = 0;
368 const char *filename;
370 filename = rindex(pathname, '/');
371 if (filename && filename[1])
376 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
377 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
378 namelen = strlen(filename) + 1;
379 lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
382 bzero(lf, sizeof(*lf));
387 lf->filename = (char*) (lf + 1);
388 strcpy(lf->filename, filename);
389 lf->id = next_file_id++;
392 STAILQ_INIT(&lf->common);
393 TAILQ_INIT(&lf->modules);
397 TAILQ_INSERT_TAIL(&linker_files, lf, link);
400 lockmgr(&lock, LK_RELEASE, 0, curthread);
405 linker_file_unload(linker_file_t file)
408 struct common_symbol* cp;
412 /* Refuse to unload modules if securelevel raised */
416 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
417 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
418 if (file->refs == 1) {
419 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
421 * Inform any modules associated with this file.
423 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
424 next = module_getfnext(mod);
427 * Give the module a chance to veto the unload.
429 if ((error = module_unload(mod)) != 0) {
430 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
432 lockmgr(&lock, LK_RELEASE, 0, curthread);
441 if (file->refs > 0) {
442 lockmgr(&lock, LK_RELEASE, 0, curthread);
446 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
447 if (file->flags & LINKER_FILE_LINKED) {
448 linker_file_sysuninit(file);
449 linker_file_unregister_sysctls(file);
452 TAILQ_REMOVE(&linker_files, file, link);
453 lockmgr(&lock, LK_RELEASE, 0, curthread);
455 for (i = 0; i < file->ndeps; i++)
456 linker_file_unload(file->deps[i]);
457 free(file->deps, M_LINKER);
459 for (cp = STAILQ_FIRST(&file->common); cp;
460 cp = STAILQ_FIRST(&file->common)) {
461 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
465 file->ops->unload(file);
466 free(file, M_LINKER);
473 linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
475 linker_file_t* newdeps;
477 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
481 bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
484 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
485 free(file->deps, M_LINKER);
487 file->deps = newdeps;
488 file->deps[file->ndeps] = dep;
495 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
498 linker_symval_t symval;
500 size_t common_size = 0;
503 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
506 if (file->ops->lookup_symbol(file, name, &sym) == 0) {
507 file->ops->symbol_values(file, sym, &symval);
510 * XXX Assume a common symbol if its value is 0 and it has a non-zero
511 * size, otherwise it could be an absolute symbol with a value of 0.
513 if (symval.value == 0 && symval.size != 0) {
515 * For commons, first look them up in the dependancies and
516 * only allocate space if not found there.
518 common_size = symval.size;
520 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
521 *raddr = symval.value;
526 for (i = 0; i < file->ndeps; i++) {
527 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
528 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", *raddr));
533 /* If we have not found it in the dependencies, search globally */
534 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
535 /* But skip the current file if it's on the list */
538 /* And skip the files we searched above */
539 for (i = 0; i < file->ndeps; i++)
540 if (lf == file->deps[i])
544 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
545 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", *raddr));
551 if (common_size > 0) {
553 * This is a common symbol which was not found in the
554 * dependancies. We maintain a simple common symbol table in
557 struct common_symbol* cp;
559 for (cp = STAILQ_FIRST(&file->common); cp;
560 cp = STAILQ_NEXT(cp, link))
561 if (!strcmp(cp->name, name)) {
562 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
563 *raddr = cp->address;
568 * Round the symbol size up to align.
570 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
571 cp = malloc(sizeof(struct common_symbol)
576 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
579 bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
581 cp->address = (caddr_t) (cp + 1);
582 cp->name = cp->address + common_size;
583 strcpy(cp->name, name);
584 bzero(cp->address, common_size);
585 STAILQ_INSERT_TAIL(&file->common, cp, link);
587 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
588 *raddr = cp->address;
592 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
598 * DDB Helpers. DDB has to look across multiple files with their own
599 * symbol tables and string tables.
601 * Note that we do not obey list locking protocols here. We really don't
602 * need DDB to hang because somebody's got the lock held. We'll take the
603 * chance that the files list is inconsistant instead.
607 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
611 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
612 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
619 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
622 u_long off = (uintptr_t)value;
623 u_long diff, bestdiff;
629 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
630 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
632 if (es != 0 && diff < bestdiff) {
651 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
655 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
656 if (lf->ops->symbol_values(lf, sym, symval) == 0)
669 kldload(struct kldload_args *uap)
671 struct thread *td = curthread;
672 char* filename = NULL, *modulename;
676 uap->sysmsg_result = -1;
678 if (securelevel > 0) /* redundant, but that's OK */
681 if ((error = suser(td)) != 0)
684 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
685 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
688 /* Can't load more than one module with the same name */
689 modulename = rindex(filename, '/');
690 if (modulename == NULL)
691 modulename = filename;
694 if (linker_find_file_by_name(modulename)) {
699 if ((error = linker_load_file(filename, &lf)) != 0)
703 uap->sysmsg_result = lf->id;
707 free(filename, M_TEMP);
712 kldunload(struct kldunload_args *uap)
714 struct thread *td = curthread;
718 if (securelevel > 0) /* redundant, but that's OK */
721 if ((error = suser(td)) != 0)
724 lf = linker_find_file_by_id(SCARG(uap, fileid));
726 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
727 if (lf->userrefs == 0) {
728 printf("linkerunload: attempt to unload file that was loaded by the kernel\n");
733 error = linker_file_unload(lf);
744 kldfind(struct kldfind_args *uap)
746 char *filename = NULL, *modulename;
750 uap->sysmsg_result = -1;
752 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
753 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
756 modulename = rindex(filename, '/');
757 if (modulename == NULL)
758 modulename = filename;
760 lf = linker_find_file_by_name(modulename);
762 uap->sysmsg_result = lf->id;
768 free(filename, M_TEMP);
773 kldnext(struct kldnext_args *uap)
778 if (SCARG(uap, fileid) == 0) {
779 if (TAILQ_FIRST(&linker_files))
780 uap->sysmsg_result = TAILQ_FIRST(&linker_files)->id;
782 uap->sysmsg_result = 0;
786 lf = linker_find_file_by_id(SCARG(uap, fileid));
788 if (TAILQ_NEXT(lf, link))
789 uap->sysmsg_result = TAILQ_NEXT(lf, link)->id;
791 uap->sysmsg_result = 0;
799 kldstat(struct kldstat_args *uap)
804 struct kld_file_stat* stat;
807 lf = linker_find_file_by_id(SCARG(uap, fileid));
813 stat = SCARG(uap, stat);
816 * Check the version of the user's structure.
818 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
820 if (version != sizeof(struct kld_file_stat)) {
825 namelen = strlen(lf->filename) + 1;
826 if (namelen > MAXPATHLEN)
827 namelen = MAXPATHLEN;
828 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
830 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
832 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
834 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
836 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
839 uap->sysmsg_result = 0;
846 kldfirstmod(struct kldfirstmod_args *uap)
851 lf = linker_find_file_by_id(SCARG(uap, fileid));
853 if (TAILQ_FIRST(&lf->modules))
854 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules));
856 uap->sysmsg_result = 0;
864 kldsym(struct kldsym_args *uap)
868 linker_symval_t symval;
870 struct kld_sym_lookup lookup;
873 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
875 if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
880 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
881 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
884 if (SCARG(uap, fileid) != 0) {
885 lf = linker_find_file_by_id(SCARG(uap, fileid));
890 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
891 lf->ops->symbol_values(lf, sym, &symval) == 0) {
892 lookup.symvalue = (uintptr_t)symval.value;
893 lookup.symsize = symval.size;
894 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
898 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
899 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
900 lf->ops->symbol_values(lf, sym, &symval) == 0) {
901 lookup.symvalue = (uintptr_t)symval.value;
902 lookup.symsize = symval.size;
903 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
912 free(symstr, M_TEMP);
917 * Preloaded module support
921 linker_preload(void* arg)
929 struct linker_set *sysinits;
930 struct sysinit **sipp;
931 const moduledata_t *moddata;
934 while ((modptr = preload_search_next_name(modptr)) != NULL) {
935 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
936 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
937 if (modname == NULL) {
938 printf("Preloaded module at %p does not have a name!\n", modptr);
941 if (modtype == NULL) {
942 printf("Preloaded module at %p does not have a type!\n", modptr);
945 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
946 lf = linker_find_file_by_name(modname);
952 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
953 error = lc->ops->load_file(modname, &lf);
962 if (linker_file_lookup_symbol(lf, "sysinit_set", 0, (caddr_t *)&sysinits) == 0 && sysinits) {
964 * This is to set the sysinit moduledata so that the module
965 * can attach itself to the correct containing file.
966 * The sysinit could be run at *any* time.
968 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
969 if ((*sipp)->func == module_register_init) {
970 moddata = (*sipp)->udata;
971 error = module_register(moddata, lf);
973 printf("Preloaded %s \"%s\" failed to register: %d\n",
974 modtype, modname, error);
977 sysinit_add((struct sysinit **)sysinits->ls_items);
979 linker_file_register_sysctls(lf);
984 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
987 * Search for a not-loaded module by name.
989 * Modules may be found in the following locations:
991 * - preloaded (result is just the module name)
992 * - on disk (result is full path to module)
994 * If the module name is qualified in any way (contains path, etc.)
995 * the we simply return a copy of it.
997 * The search path can be manipulated via sysctl. Note that we use the ';'
998 * character as a separator to be consistent with the bootloader.
1001 static char linker_path[MAXPATHLEN] = "/;/boot/;/modules/";
1003 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1004 sizeof(linker_path), "module load search path");
1007 linker_strdup(const char *str)
1011 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
1012 strcpy(result, str);
1017 linker_search_path(const char *name)
1019 struct nameidata nd;
1020 struct thread *td = curthread;
1021 char *cp, *ep, *result;
1025 /* qualified at all? */
1026 if (index(name, '/'))
1027 return(linker_strdup(name));
1029 /* traverse the linker path */
1033 /* find the end of this component */
1034 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1036 result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
1037 if (result == NULL) /* actually ENOMEM */
1040 strncpy(result, cp, ep - cp);
1041 strcpy(result + (ep - cp), name);
1044 * Attempt to open the file, and return the path if we succeed and it's
1047 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, result, td);
1048 error = vn_open(&nd, FREAD, 0);
1050 NDFREE(&nd, NDF_ONLY_PNBUF);
1051 type = nd.ni_vp->v_type;
1052 VOP_UNLOCK(nd.ni_vp, 0, td);
1053 vn_close(nd.ni_vp, FREAD, td);
1057 free(result, M_LINKER);