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.42 2008/01/10 10:31:48 matthias 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/queue.h>
42 #include <sys/linker.h>
43 #include <sys/fcntl.h>
44 #include <sys/libkern.h>
45 #include <sys/nlookup.h>
46 #include <sys/vnode.h>
47 #include <sys/sysctl.h>
49 #include <vm/vm_zone.h>
51 #ifdef _KERNEL_VIRTUAL
59 /* Metadata from the static kernel */
60 SET_DECLARE(modmetadata_set, struct mod_metadata);
61 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
63 linker_file_t linker_current_file;
64 linker_file_t linker_kernel_file;
66 static struct lock lock; /* lock for the file list */
67 static linker_class_list_t classes;
68 static linker_file_list_t linker_files;
69 static int next_file_id = 1;
72 linker_init(void* arg)
74 lockinit(&lock, "klink", 0, 0);
76 TAILQ_INIT(&linker_files);
79 SYSINIT(linker, SI_BOOT2_KLD, SI_ORDER_FIRST, linker_init, 0);
82 linker_add_class(const char* desc, void* priv,
83 struct linker_class_ops* ops)
87 lc = kmalloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT | M_ZERO);
94 TAILQ_INSERT_HEAD(&classes, lc, link);
100 linker_file_sysinit(linker_file_t lf)
102 struct sysinit** start, ** stop;
103 struct sysinit** sipp;
104 struct sysinit** xipp;
105 struct sysinit* save;
106 const moduledata_t *moddata;
109 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
112 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
113 return 0; /* XXX is this correct ? No sysinit ? */
116 for (sipp = start; sipp < stop; sipp++) {
117 if ((*sipp)->func == module_register_init) {
118 moddata = (*sipp)->udata;
119 error = module_register(moddata, lf);
121 kprintf("linker_file_sysinit \"%s\" failed to register! %d\n",
122 lf->filename, error);
129 * Perform a bubble sort of the system initialization objects by
130 * their subsystem (primary key) and order (secondary key).
132 * Since some things care about execution order, this is the
133 * operation which ensures continued function.
135 for (sipp = start; sipp < stop; sipp++) {
136 for (xipp = sipp + 1; xipp < stop; xipp++) {
137 if ((*sipp)->subsystem < (*xipp)->subsystem ||
138 ((*sipp)->subsystem == (*xipp)->subsystem &&
139 (*sipp)->order <= (*xipp)->order))
149 * Traverse the (now) ordered list of system initialization tasks.
150 * Perform each task, and continue on to the next task.
152 for (sipp = start; sipp < stop; sipp++) {
153 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
154 continue; /* skip dummy task(s)*/
157 (*((*sipp)->func))((*sipp)->udata);
159 return 0; /* no errors */
163 linker_file_sysuninit(linker_file_t lf)
165 struct sysinit** start, ** stop;
166 struct sysinit** sipp;
167 struct sysinit** xipp;
168 struct sysinit* save;
170 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
173 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
177 * Perform a reverse bubble sort of the system initialization objects
178 * by their subsystem (primary key) and order (secondary key).
180 * Since some things care about execution order, this is the
181 * operation which ensures continued function.
183 for (sipp = start; sipp < stop; sipp++) {
184 for (xipp = sipp + 1; xipp < stop; xipp++) {
185 if ((*sipp)->subsystem > (*xipp)->subsystem ||
186 ((*sipp)->subsystem == (*xipp)->subsystem &&
187 (*sipp)->order >= (*xipp)->order))
197 * Traverse the (now) ordered list of system initialization tasks.
198 * Perform each task, and continue on to the next task.
200 for (sipp = start; sipp < stop; sipp++) {
201 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
202 continue; /* skip dummy task(s)*/
205 (*((*sipp)->func))((*sipp)->udata);
210 linker_file_register_sysctls(linker_file_t lf)
212 struct sysctl_oid **start, **stop, **oidp;
214 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
217 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
219 for (oidp = start; oidp < stop; oidp++)
220 sysctl_register_oid(*oidp);
224 linker_file_unregister_sysctls(linker_file_t lf)
226 struct sysctl_oid **start, **stop, **oidp;
228 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
231 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
233 for (oidp = start; oidp < stop; oidp++)
234 sysctl_unregister_oid(*oidp);
238 linker_load_file(const char* filename, linker_file_t* result)
242 int foundfile, error = 0;
245 /* Refuse to load modules if securelevel raised */
246 if (securelevel > 0 || kernel_mem_readonly)
249 lf = linker_find_file_by_name(filename);
251 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
256 if (find_mod_metadata(filename)) {
257 if (linker_kernel_file)
258 ++linker_kernel_file->refs;
259 *result = linker_kernel_file;
263 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
264 ksprintf(koname, "%s.ko", filename);
267 TAILQ_FOREACH(lc, &classes, link) {
268 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
269 filename, lc->desc));
271 error = lc->ops->load_file(koname, &lf); /* First with .ko */
272 if (lf == NULL && error == ENOENT)
273 error = lc->ops->load_file(filename, &lf); /* Then try without */
275 * If we got something other than ENOENT, then it exists but we cannot
276 * load it for some other reason.
281 linker_file_register_sysctls(lf);
282 error = linker_file_sysinit(lf);
289 * Less than ideal, but tells the user whether it failed to load or
290 * the module was not found.
294 * Format not recognized or otherwise unloadable.
295 * When loading a module that is statically built into
296 * the kernel EEXIST percolates back up as the return
297 * value. Preserve this so that apps can recognize this
303 error = ENOENT; /* Nothing found */
308 kfree(koname, M_LINKER);
313 linker_find_file_by_name(const char* filename)
315 linker_file_t lf = 0;
319 for (i = strlen(filename); i > 0 && filename[i-1] != '/'; --i)
323 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
324 ksprintf(koname, "%s.ko", filename);
326 lockmgr(&lock, LK_SHARED);
327 TAILQ_FOREACH(lf, &linker_files, link) {
328 if (!strcmp(lf->filename, koname))
330 if (!strcmp(lf->filename, filename))
333 lockmgr(&lock, LK_RELEASE);
336 kfree(koname, M_LINKER);
341 linker_find_file_by_id(int fileid)
343 linker_file_t lf = 0;
345 lockmgr(&lock, LK_SHARED);
346 TAILQ_FOREACH(lf, &linker_files, link)
347 if (lf->id == fileid)
349 lockmgr(&lock, LK_RELEASE);
355 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
357 linker_file_t lf = 0;
359 const char *filename;
361 filename = rindex(pathname, '/');
362 if (filename && filename[1])
367 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
368 lockmgr(&lock, LK_EXCLUSIVE);
369 namelen = strlen(filename) + 1;
370 lf = kmalloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
371 bzero(lf, sizeof(*lf));
376 lf->filename = (char*) (lf + 1);
377 strcpy(lf->filename, filename);
378 lf->id = next_file_id++;
381 STAILQ_INIT(&lf->common);
382 TAILQ_INIT(&lf->modules);
386 TAILQ_INSERT_TAIL(&linker_files, lf, link);
388 lockmgr(&lock, LK_RELEASE);
393 linker_file_unload(linker_file_t file)
396 struct common_symbol* cp;
400 /* Refuse to unload modules if securelevel raised */
401 if (securelevel > 0 || kernel_mem_readonly)
404 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
405 lockmgr(&lock, LK_EXCLUSIVE);
406 if (file->refs == 1) {
407 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
409 * Temporarily bump file->refs to prevent recursive unloading
414 * Inform any modules associated with this file.
416 mod = TAILQ_FIRST(&file->modules);
419 * Give the module a chance to veto the unload. Note that the
420 * act of unloading the module may cause other modules in the
421 * same file list to be unloaded recursively.
423 if ((error = module_unload(mod)) != 0) {
424 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
426 lockmgr(&lock, LK_RELEASE);
432 * Recursive relationships may prevent the release from
433 * immediately removing the module, or may remove other
434 * modules in the list.
436 next = module_getfnext(mod);
442 * Since we intend to destroy the file structure, we expect all
443 * modules to have been removed by now.
445 for (mod = TAILQ_FIRST(&file->modules);
447 mod = module_getfnext(mod)
449 kprintf("linker_file_unload: module %p still has refs!\n", mod);
455 if (file->refs > 0) {
456 lockmgr(&lock, LK_RELEASE);
460 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
461 if (file->flags & LINKER_FILE_LINKED) {
462 linker_file_sysuninit(file);
463 linker_file_unregister_sysctls(file);
466 TAILQ_REMOVE(&linker_files, file, link);
467 lockmgr(&lock, LK_RELEASE);
469 for (i = 0; i < file->ndeps; i++)
470 linker_file_unload(file->deps[i]);
471 kfree(file->deps, M_LINKER);
473 for (cp = STAILQ_FIRST(&file->common); cp;
474 cp = STAILQ_FIRST(&file->common)) {
475 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
479 file->ops->unload(file);
480 kfree(file, M_LINKER);
487 linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
489 linker_file_t* newdeps;
491 newdeps = kmalloc((file->ndeps + 1) * sizeof(linker_file_t*),
492 M_LINKER, M_WAITOK | M_ZERO);
495 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
496 kfree(file->deps, M_LINKER);
498 file->deps = newdeps;
499 file->deps[file->ndeps] = dep;
506 * Locate a linker set and its contents.
507 * This is a helper function to avoid linker_if.h exposure elsewhere.
508 * Note: firstp and lastp are really void ***
511 linker_file_lookup_set(linker_file_t file, const char *name,
512 void *firstp, void *lastp, int *countp)
514 return file->ops->lookup_set(file, name, firstp, lastp, countp);
518 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
521 linker_symval_t symval;
523 size_t common_size = 0;
526 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
529 if (file->ops->lookup_symbol(file, name, &sym) == 0) {
530 file->ops->symbol_values(file, sym, &symval);
533 * XXX Assume a common symbol if its value is 0 and it has a non-zero
534 * size, otherwise it could be an absolute symbol with a value of 0.
536 if (symval.value == 0 && symval.size != 0) {
538 * For commons, first look them up in the dependancies and
539 * only allocate space if not found there.
541 common_size = symval.size;
543 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
544 *raddr = symval.value;
549 for (i = 0; i < file->ndeps; i++) {
550 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
551 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", *raddr));
556 /* If we have not found it in the dependencies, search globally */
557 TAILQ_FOREACH(lf, &linker_files, link) {
558 /* But skip the current file if it's on the list */
561 /* And skip the files we searched above */
562 for (i = 0; i < file->ndeps; i++)
563 if (lf == file->deps[i])
567 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
568 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", *raddr));
574 if (common_size > 0) {
576 * This is a common symbol which was not found in the
577 * dependancies. We maintain a simple common symbol table in
580 struct common_symbol* cp;
582 STAILQ_FOREACH(cp, &file->common, link)
583 if (!strcmp(cp->name, name)) {
584 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
585 *raddr = cp->address;
590 * Round the symbol size up to align.
592 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
593 cp = kmalloc(sizeof(struct common_symbol)
596 M_LINKER, M_WAITOK | M_ZERO);
598 cp->address = (caddr_t) (cp + 1);
599 cp->name = cp->address + common_size;
600 strcpy(cp->name, name);
601 bzero(cp->address, common_size);
602 STAILQ_INSERT_TAIL(&file->common, cp, link);
604 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
605 *raddr = cp->address;
609 #ifdef _KERNEL_VIRTUAL
610 *raddr = dlsym(RTLD_NEXT, name);
611 if (*raddr != NULL) {
612 KLD_DPF(SYM, ("linker_file_lookup_symbol: found dlsym=%x\n", *raddr));
617 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
623 * DDB Helpers. DDB has to look across multiple files with their own
624 * symbol tables and string tables.
626 * Note that we do not obey list locking protocols here. We really don't
627 * need DDB to hang because somebody's got the lock held. We'll take the
628 * chance that the files list is inconsistant instead.
632 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
636 TAILQ_FOREACH(lf, &linker_files, link) {
637 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
644 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
647 u_long off = (uintptr_t)value;
648 u_long diff, bestdiff;
654 TAILQ_FOREACH(lf, &linker_files, link) {
655 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
657 if (es != 0 && diff < bestdiff) {
676 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
680 TAILQ_FOREACH(lf, &linker_files, link) {
681 if (lf->ops->symbol_values(lf, sym, symval) == 0)
694 sys_kldload(struct kldload_args *uap)
696 struct thread *td = curthread;
697 char* filename = NULL, *modulename;
701 uap->sysmsg_result = -1;
703 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
706 if ((error = suser(td)) != 0)
709 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
710 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
713 /* Can't load more than one module with the same name */
714 modulename = rindex(filename, '/');
715 if (modulename == NULL)
716 modulename = filename;
719 if (linker_find_file_by_name(modulename)) {
724 if ((error = linker_load_file(filename, &lf)) != 0)
728 uap->sysmsg_result = lf->id;
732 kfree(filename, M_TEMP);
737 sys_kldunload(struct kldunload_args *uap)
739 struct thread *td = curthread;
743 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
746 if ((error = suser(td)) != 0)
749 lf = linker_find_file_by_id(uap->fileid);
751 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
752 if (lf->userrefs == 0) {
753 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
758 error = linker_file_unload(lf);
769 sys_kldfind(struct kldfind_args *uap)
771 char *filename = NULL, *modulename;
775 uap->sysmsg_result = -1;
777 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
778 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
781 modulename = rindex(filename, '/');
782 if (modulename == NULL)
783 modulename = filename;
785 lf = linker_find_file_by_name(modulename);
787 uap->sysmsg_result = lf->id;
793 kfree(filename, M_TEMP);
798 sys_kldnext(struct kldnext_args *uap)
803 if (uap->fileid == 0) {
804 if (TAILQ_FIRST(&linker_files))
805 uap->sysmsg_result = TAILQ_FIRST(&linker_files)->id;
807 uap->sysmsg_result = 0;
811 lf = linker_find_file_by_id(uap->fileid);
813 if (TAILQ_NEXT(lf, link))
814 uap->sysmsg_result = TAILQ_NEXT(lf, link)->id;
816 uap->sysmsg_result = 0;
824 sys_kldstat(struct kldstat_args *uap)
829 struct kld_file_stat* stat;
832 lf = linker_find_file_by_id(uap->fileid);
841 * Check the version of the user's structure.
843 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
845 if (version != sizeof(struct kld_file_stat)) {
850 namelen = strlen(lf->filename) + 1;
851 if (namelen > MAXPATHLEN)
852 namelen = MAXPATHLEN;
853 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
855 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
857 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
859 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
861 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
864 uap->sysmsg_result = 0;
871 sys_kldfirstmod(struct kldfirstmod_args *uap)
876 lf = linker_find_file_by_id(uap->fileid);
878 if (TAILQ_FIRST(&lf->modules))
879 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules));
881 uap->sysmsg_result = 0;
889 sys_kldsym(struct kldsym_args *uap)
893 linker_symval_t symval;
895 struct kld_sym_lookup lookup;
898 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
900 if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP) {
905 symstr = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
906 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
909 if (uap->fileid != 0) {
910 lf = linker_find_file_by_id(uap->fileid);
915 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
916 lf->ops->symbol_values(lf, sym, &symval) == 0) {
917 lookup.symvalue = (uintptr_t)symval.value;
918 lookup.symsize = symval.size;
919 error = copyout(&lookup, uap->data, sizeof(lookup));
923 TAILQ_FOREACH(lf, &linker_files, link) {
924 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
925 lf->ops->symbol_values(lf, sym, &symval) == 0) {
926 lookup.symvalue = (uintptr_t)symval.value;
927 lookup.symsize = symval.size;
928 error = copyout(&lookup, uap->data, sizeof(lookup));
937 kfree(symstr, M_TEMP);
942 * Look for module metadata in the static kernel
944 struct mod_metadata *
945 find_mod_metadata(const char *modname)
948 struct mod_metadata **mdp;
949 struct mod_metadata *mdt;
952 * Strip path prefixes and any dot extension. MDT_MODULE names
953 * are just the module name without a path or ".ko".
955 for (len = strlen(modname) - 1; len >= 0; --len) {
956 if (modname[len] == '/')
960 for (len = 0; modname[len] && modname[len] != '.'; ++len)
964 * Look for the module declaration
966 SET_FOREACH(mdp, modmetadata_set) {
968 if (mdt->md_type != MDT_MODULE)
970 if (strlen(mdt->md_cval) == len &&
971 strncmp(mdt->md_cval, modname, len) == 0) {
979 * Preloaded module support
982 linker_preload(void* arg)
990 struct sysinit **sipp;
991 const moduledata_t *moddata;
992 struct sysinit **si_start, **si_stop;
995 while ((modptr = preload_search_next_name(modptr)) != NULL) {
996 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
997 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
998 if (modname == NULL) {
999 kprintf("Preloaded module at %p does not have a name!\n", modptr);
1002 if (modtype == NULL) {
1003 kprintf("Preloaded module at %p does not have a type!\n", modptr);
1008 * This is a hack at the moment, but what's in FreeBSD-5 is even
1009 * worse so I'd rather the hack.
1011 kprintf("Preloaded %s \"%s\" at %p", modtype, modname, modptr);
1012 if (find_mod_metadata(modname)) {
1013 kprintf(" (ignored, already in static kernel)\n");
1018 lf = linker_find_file_by_name(modname);
1025 TAILQ_FOREACH(lc, &classes, link) {
1026 error = lc->ops->load_file(modname, &lf);
1035 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0) {
1037 * This is to set the sysinit moduledata so that the module
1038 * can attach itself to the correct containing file.
1039 * The sysinit could be run at *any* time.
1041 for (sipp = si_start; sipp < si_stop; sipp++) {
1042 if ((*sipp)->func == module_register_init) {
1043 moddata = (*sipp)->udata;
1044 error = module_register(moddata, lf);
1046 kprintf("Preloaded %s \"%s\" failed to register: %d\n",
1047 modtype, modname, error);
1050 sysinit_add(si_start, si_stop);
1052 linker_file_register_sysctls(lf);
1057 SYSINIT(preload, SI_BOOT2_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1060 * Search for a not-loaded module by name.
1062 * Modules may be found in the following locations:
1064 * - preloaded (result is just the module name)
1065 * - on disk (result is full path to module)
1067 * If the module name is qualified in any way (contains path, etc.)
1068 * the we simply return a copy of it.
1070 * The search path can be manipulated via sysctl. Note that we use the ';'
1071 * character as a separator to be consistent with the bootloader.
1074 static char linker_path[MAXPATHLEN] = "/;/boot;/modules";
1076 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1077 sizeof(linker_path), "module load search path");
1078 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1081 linker_strdup(const char *str)
1085 result = kmalloc(strlen(str) + 1, M_LINKER, M_WAITOK);
1086 strcpy(result, str);
1091 linker_search_path(const char *name)
1093 struct nlookupdata nd;
1094 char *cp, *ep, *result;
1095 size_t name_len, prefix_len;
1100 /* qualified at all? */
1101 if (index(name, '/'))
1102 return(linker_strdup(name));
1104 /* traverse the linker path */
1106 name_len = strlen(name);
1109 /* find the end of this component */
1110 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1112 prefix_len = ep - cp;
1113 /* if this component doesn't end with a slash, add one */
1114 if (ep == cp || *(ep - 1) != '/')
1120 * +2 : possible separator, plus terminator.
1122 result = kmalloc(prefix_len + name_len + 2, M_LINKER, M_WAITOK);
1124 strncpy(result, cp, prefix_len);
1126 result[prefix_len++] = '/';
1127 strcpy(result + prefix_len, name);
1130 * Attempt to open the file, and return the path if we succeed and it's
1133 error = nlookup_init(&nd, result, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
1135 error = vn_open(&nd, NULL, FREAD, 0);
1137 type = nd.nl_open_vp->v_type;
1144 kfree(result, M_LINKER);