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.40 2008/01/05 14:02:38 swildner 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/nlookup.h>
45 #include <sys/vnode.h>
46 #include <sys/sysctl.h>
48 #include <vm/vm_zone.h>
50 #ifdef _KERNEL_VIRTUAL
58 /* Metadata from the static kernel */
59 SET_DECLARE(modmetadata_set, struct mod_metadata);
60 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
62 linker_file_t linker_current_file;
63 linker_file_t linker_kernel_file;
65 static struct lock lock; /* lock for the file list */
66 static linker_class_list_t classes;
67 static linker_file_list_t linker_files;
68 static int next_file_id = 1;
71 linker_init(void* arg)
73 lockinit(&lock, "klink", 0, 0);
75 TAILQ_INIT(&linker_files);
78 SYSINIT(linker, SI_BOOT2_KLD, SI_ORDER_FIRST, linker_init, 0);
81 linker_add_class(const char* desc, void* priv,
82 struct linker_class_ops* ops)
86 lc = kmalloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT | M_ZERO);
93 TAILQ_INSERT_HEAD(&classes, lc, link);
99 linker_file_sysinit(linker_file_t lf)
101 struct sysinit** start, ** stop;
102 struct sysinit** sipp;
103 struct sysinit** xipp;
104 struct sysinit* save;
105 const moduledata_t *moddata;
108 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
111 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
112 return 0; /* XXX is this correct ? No sysinit ? */
115 for (sipp = start; sipp < stop; sipp++) {
116 if ((*sipp)->func == module_register_init) {
117 moddata = (*sipp)->udata;
118 error = module_register(moddata, lf);
120 kprintf("linker_file_sysinit \"%s\" failed to register! %d\n",
121 lf->filename, error);
128 * Perform a bubble sort of the system initialization objects by
129 * their subsystem (primary key) and order (secondary key).
131 * Since some things care about execution order, this is the
132 * operation which ensures continued function.
134 for (sipp = start; sipp < stop; sipp++) {
135 for (xipp = sipp + 1; xipp < stop; xipp++) {
136 if ((*sipp)->subsystem < (*xipp)->subsystem ||
137 ((*sipp)->subsystem == (*xipp)->subsystem &&
138 (*sipp)->order <= (*xipp)->order))
148 * Traverse the (now) ordered list of system initialization tasks.
149 * Perform each task, and continue on to the next task.
151 for (sipp = start; sipp < stop; sipp++) {
152 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
153 continue; /* skip dummy task(s)*/
156 (*((*sipp)->func))((*sipp)->udata);
158 return 0; /* no errors */
162 linker_file_sysuninit(linker_file_t lf)
164 struct sysinit** start, ** stop;
165 struct sysinit** sipp;
166 struct sysinit** xipp;
167 struct sysinit* save;
169 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
172 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
176 * Perform a reverse bubble sort of the system initialization objects
177 * by their subsystem (primary key) and order (secondary key).
179 * Since some things care about execution order, this is the
180 * operation which ensures continued function.
182 for (sipp = start; sipp < stop; sipp++) {
183 for (xipp = sipp + 1; xipp < stop; xipp++) {
184 if ((*sipp)->subsystem > (*xipp)->subsystem ||
185 ((*sipp)->subsystem == (*xipp)->subsystem &&
186 (*sipp)->order >= (*xipp)->order))
196 * Traverse the (now) ordered list of system initialization tasks.
197 * Perform each task, and continue on to the next task.
199 for (sipp = start; sipp < stop; sipp++) {
200 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
201 continue; /* skip dummy task(s)*/
204 (*((*sipp)->func))((*sipp)->udata);
209 linker_file_register_sysctls(linker_file_t lf)
211 struct sysctl_oid **start, **stop, **oidp;
213 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
216 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
218 for (oidp = start; oidp < stop; oidp++)
219 sysctl_register_oid(*oidp);
223 linker_file_unregister_sysctls(linker_file_t lf)
225 struct sysctl_oid **start, **stop, **oidp;
227 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
230 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
232 for (oidp = start; oidp < stop; oidp++)
233 sysctl_unregister_oid(*oidp);
237 linker_load_file(const char* filename, linker_file_t* result)
241 int foundfile, error = 0;
244 /* Refuse to load modules if securelevel raised */
245 if (securelevel > 0 || kernel_mem_readonly)
248 lf = linker_find_file_by_name(filename);
250 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
255 if (find_mod_metadata(filename)) {
256 if (linker_kernel_file)
257 ++linker_kernel_file->refs;
258 *result = linker_kernel_file;
262 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
263 if (koname == NULL) {
267 ksprintf(koname, "%s.ko", filename);
270 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
271 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
272 filename, lc->desc));
274 error = lc->ops->load_file(koname, &lf); /* First with .ko */
275 if (lf == NULL && error == ENOENT)
276 error = lc->ops->load_file(filename, &lf); /* Then try without */
278 * If we got something other than ENOENT, then it exists but we cannot
279 * load it for some other reason.
284 linker_file_register_sysctls(lf);
285 error = linker_file_sysinit(lf);
292 * Less than ideal, but tells the user whether it failed to load or
293 * the module was not found.
297 * Format not recognized or otherwise unloadable.
298 * When loading a module that is statically built into
299 * the kernel EEXIST percolates back up as the return
300 * value. Preserve this so that apps can recognize this
306 error = ENOENT; /* Nothing found */
311 kfree(koname, M_LINKER);
316 linker_find_file_by_name(const char* filename)
318 linker_file_t lf = 0;
322 for (i = strlen(filename); i > 0 && filename[i-1] != '/'; --i)
326 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
329 ksprintf(koname, "%s.ko", filename);
331 lockmgr(&lock, LK_SHARED);
332 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
333 if (!strcmp(lf->filename, koname))
335 if (!strcmp(lf->filename, filename))
338 lockmgr(&lock, LK_RELEASE);
342 kfree(koname, M_LINKER);
347 linker_find_file_by_id(int fileid)
349 linker_file_t lf = 0;
351 lockmgr(&lock, LK_SHARED);
352 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link))
353 if (lf->id == fileid)
355 lockmgr(&lock, LK_RELEASE);
361 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
363 linker_file_t lf = 0;
365 const char *filename;
367 filename = rindex(pathname, '/');
368 if (filename && filename[1])
373 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
374 lockmgr(&lock, LK_EXCLUSIVE);
375 namelen = strlen(filename) + 1;
376 lf = kmalloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
379 bzero(lf, sizeof(*lf));
384 lf->filename = (char*) (lf + 1);
385 strcpy(lf->filename, filename);
386 lf->id = next_file_id++;
389 STAILQ_INIT(&lf->common);
390 TAILQ_INIT(&lf->modules);
394 TAILQ_INSERT_TAIL(&linker_files, lf, link);
397 lockmgr(&lock, LK_RELEASE);
402 linker_file_unload(linker_file_t file)
405 struct common_symbol* cp;
409 /* Refuse to unload modules if securelevel raised */
410 if (securelevel > 0 || kernel_mem_readonly)
413 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
414 lockmgr(&lock, LK_EXCLUSIVE);
415 if (file->refs == 1) {
416 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
418 * Temporarily bump file->refs to prevent recursive unloading
423 * Inform any modules associated with this file.
425 mod = TAILQ_FIRST(&file->modules);
428 * Give the module a chance to veto the unload. Note that the
429 * act of unloading the module may cause other modules in the
430 * same file list to be unloaded recursively.
432 if ((error = module_unload(mod)) != 0) {
433 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
435 lockmgr(&lock, LK_RELEASE);
441 * Recursive relationships may prevent the release from
442 * immediately removing the module, or may remove other
443 * modules in the list.
445 next = module_getfnext(mod);
451 * Since we intend to destroy the file structure, we expect all
452 * modules to have been removed by now.
454 for (mod = TAILQ_FIRST(&file->modules);
456 mod = module_getfnext(mod)
458 kprintf("linker_file_unload: module %p still has refs!\n", mod);
464 if (file->refs > 0) {
465 lockmgr(&lock, LK_RELEASE);
469 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
470 if (file->flags & LINKER_FILE_LINKED) {
471 linker_file_sysuninit(file);
472 linker_file_unregister_sysctls(file);
475 TAILQ_REMOVE(&linker_files, file, link);
476 lockmgr(&lock, LK_RELEASE);
478 for (i = 0; i < file->ndeps; i++)
479 linker_file_unload(file->deps[i]);
480 kfree(file->deps, M_LINKER);
482 for (cp = STAILQ_FIRST(&file->common); cp;
483 cp = STAILQ_FIRST(&file->common)) {
484 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
488 file->ops->unload(file);
489 kfree(file, M_LINKER);
496 linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
498 linker_file_t* newdeps;
500 newdeps = kmalloc((file->ndeps + 1) * sizeof(linker_file_t*),
501 M_LINKER, M_WAITOK | M_ZERO);
506 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
507 kfree(file->deps, M_LINKER);
509 file->deps = newdeps;
510 file->deps[file->ndeps] = dep;
517 * Locate a linker set and its contents.
518 * This is a helper function to avoid linker_if.h exposure elsewhere.
519 * Note: firstp and lastp are really void ***
522 linker_file_lookup_set(linker_file_t file, const char *name,
523 void *firstp, void *lastp, int *countp)
525 return file->ops->lookup_set(file, name, firstp, lastp, countp);
529 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
532 linker_symval_t symval;
534 size_t common_size = 0;
537 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
540 if (file->ops->lookup_symbol(file, name, &sym) == 0) {
541 file->ops->symbol_values(file, sym, &symval);
544 * XXX Assume a common symbol if its value is 0 and it has a non-zero
545 * size, otherwise it could be an absolute symbol with a value of 0.
547 if (symval.value == 0 && symval.size != 0) {
549 * For commons, first look them up in the dependancies and
550 * only allocate space if not found there.
552 common_size = symval.size;
554 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
555 *raddr = symval.value;
560 for (i = 0; i < file->ndeps; i++) {
561 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
562 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", *raddr));
567 /* If we have not found it in the dependencies, search globally */
568 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
569 /* But skip the current file if it's on the list */
572 /* And skip the files we searched above */
573 for (i = 0; i < file->ndeps; i++)
574 if (lf == file->deps[i])
578 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
579 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", *raddr));
585 if (common_size > 0) {
587 * This is a common symbol which was not found in the
588 * dependancies. We maintain a simple common symbol table in
591 struct common_symbol* cp;
593 for (cp = STAILQ_FIRST(&file->common); cp;
594 cp = STAILQ_NEXT(cp, link))
595 if (!strcmp(cp->name, name)) {
596 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
597 *raddr = cp->address;
602 * Round the symbol size up to align.
604 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
605 cp = kmalloc(sizeof(struct common_symbol)
608 M_LINKER, M_WAITOK | M_ZERO);
610 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
614 cp->address = (caddr_t) (cp + 1);
615 cp->name = cp->address + common_size;
616 strcpy(cp->name, name);
617 bzero(cp->address, common_size);
618 STAILQ_INSERT_TAIL(&file->common, cp, link);
620 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
621 *raddr = cp->address;
625 #ifdef _KERNEL_VIRTUAL
626 *raddr = dlsym(RTLD_NEXT, name);
627 if (*raddr != NULL) {
628 KLD_DPF(SYM, ("linker_file_lookup_symbol: found dlsym=%x\n", *raddr));
633 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
639 * DDB Helpers. DDB has to look across multiple files with their own
640 * symbol tables and string tables.
642 * Note that we do not obey list locking protocols here. We really don't
643 * need DDB to hang because somebody's got the lock held. We'll take the
644 * chance that the files list is inconsistant instead.
648 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
652 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
653 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
660 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
663 u_long off = (uintptr_t)value;
664 u_long diff, bestdiff;
670 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
671 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
673 if (es != 0 && diff < bestdiff) {
692 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
696 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
697 if (lf->ops->symbol_values(lf, sym, symval) == 0)
710 sys_kldload(struct kldload_args *uap)
712 struct thread *td = curthread;
713 char* filename = NULL, *modulename;
717 uap->sysmsg_result = -1;
719 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
722 if ((error = suser(td)) != 0)
725 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
726 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
729 /* Can't load more than one module with the same name */
730 modulename = rindex(filename, '/');
731 if (modulename == NULL)
732 modulename = filename;
735 if (linker_find_file_by_name(modulename)) {
740 if ((error = linker_load_file(filename, &lf)) != 0)
744 uap->sysmsg_result = lf->id;
748 kfree(filename, M_TEMP);
753 sys_kldunload(struct kldunload_args *uap)
755 struct thread *td = curthread;
759 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
762 if ((error = suser(td)) != 0)
765 lf = linker_find_file_by_id(uap->fileid);
767 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
768 if (lf->userrefs == 0) {
769 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
774 error = linker_file_unload(lf);
785 sys_kldfind(struct kldfind_args *uap)
787 char *filename = NULL, *modulename;
791 uap->sysmsg_result = -1;
793 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
794 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
797 modulename = rindex(filename, '/');
798 if (modulename == NULL)
799 modulename = filename;
801 lf = linker_find_file_by_name(modulename);
803 uap->sysmsg_result = lf->id;
809 kfree(filename, M_TEMP);
814 sys_kldnext(struct kldnext_args *uap)
819 if (uap->fileid == 0) {
820 if (TAILQ_FIRST(&linker_files))
821 uap->sysmsg_result = TAILQ_FIRST(&linker_files)->id;
823 uap->sysmsg_result = 0;
827 lf = linker_find_file_by_id(uap->fileid);
829 if (TAILQ_NEXT(lf, link))
830 uap->sysmsg_result = TAILQ_NEXT(lf, link)->id;
832 uap->sysmsg_result = 0;
840 sys_kldstat(struct kldstat_args *uap)
845 struct kld_file_stat* stat;
848 lf = linker_find_file_by_id(uap->fileid);
857 * Check the version of the user's structure.
859 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
861 if (version != sizeof(struct kld_file_stat)) {
866 namelen = strlen(lf->filename) + 1;
867 if (namelen > MAXPATHLEN)
868 namelen = MAXPATHLEN;
869 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
871 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
873 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
875 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
877 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
880 uap->sysmsg_result = 0;
887 sys_kldfirstmod(struct kldfirstmod_args *uap)
892 lf = linker_find_file_by_id(uap->fileid);
894 if (TAILQ_FIRST(&lf->modules))
895 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules));
897 uap->sysmsg_result = 0;
905 sys_kldsym(struct kldsym_args *uap)
909 linker_symval_t symval;
911 struct kld_sym_lookup lookup;
914 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
916 if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP) {
921 symstr = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
922 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
925 if (uap->fileid != 0) {
926 lf = linker_find_file_by_id(uap->fileid);
931 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
932 lf->ops->symbol_values(lf, sym, &symval) == 0) {
933 lookup.symvalue = (uintptr_t)symval.value;
934 lookup.symsize = symval.size;
935 error = copyout(&lookup, uap->data, sizeof(lookup));
939 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
940 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
941 lf->ops->symbol_values(lf, sym, &symval) == 0) {
942 lookup.symvalue = (uintptr_t)symval.value;
943 lookup.symsize = symval.size;
944 error = copyout(&lookup, uap->data, sizeof(lookup));
953 kfree(symstr, M_TEMP);
958 * Look for module metadata in the static kernel
960 struct mod_metadata *
961 find_mod_metadata(const char *modname)
964 struct mod_metadata **mdp;
965 struct mod_metadata *mdt;
968 * Strip path prefixes and any dot extension. MDT_MODULE names
969 * are just the module name without a path or ".ko".
971 for (len = strlen(modname) - 1; len >= 0; --len) {
972 if (modname[len] == '/')
976 for (len = 0; modname[len] && modname[len] != '.'; ++len)
980 * Look for the module declaration
982 SET_FOREACH(mdp, modmetadata_set) {
984 if (mdt->md_type != MDT_MODULE)
986 if (strlen(mdt->md_cval) == len &&
987 strncmp(mdt->md_cval, modname, len) == 0) {
995 * Preloaded module support
998 linker_preload(void* arg)
1006 struct sysinit **sipp;
1007 const moduledata_t *moddata;
1008 struct sysinit **si_start, **si_stop;
1011 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1012 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1013 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1014 if (modname == NULL) {
1015 kprintf("Preloaded module at %p does not have a name!\n", modptr);
1018 if (modtype == NULL) {
1019 kprintf("Preloaded module at %p does not have a type!\n", modptr);
1024 * This is a hack at the moment, but what's in FreeBSD-5 is even
1025 * worse so I'd rather the hack.
1027 kprintf("Preloaded %s \"%s\" at %p", modtype, modname, modptr);
1028 if (find_mod_metadata(modname)) {
1029 kprintf(" (ignored, already in static kernel)\n");
1034 lf = linker_find_file_by_name(modname);
1041 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
1042 error = lc->ops->load_file(modname, &lf);
1051 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0) {
1053 * This is to set the sysinit moduledata so that the module
1054 * can attach itself to the correct containing file.
1055 * The sysinit could be run at *any* time.
1057 for (sipp = si_start; sipp < si_stop; sipp++) {
1058 if ((*sipp)->func == module_register_init) {
1059 moddata = (*sipp)->udata;
1060 error = module_register(moddata, lf);
1062 kprintf("Preloaded %s \"%s\" failed to register: %d\n",
1063 modtype, modname, error);
1066 sysinit_add(si_start, si_stop);
1068 linker_file_register_sysctls(lf);
1073 SYSINIT(preload, SI_BOOT2_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1076 * Search for a not-loaded module by name.
1078 * Modules may be found in the following locations:
1080 * - preloaded (result is just the module name)
1081 * - on disk (result is full path to module)
1083 * If the module name is qualified in any way (contains path, etc.)
1084 * the we simply return a copy of it.
1086 * The search path can be manipulated via sysctl. Note that we use the ';'
1087 * character as a separator to be consistent with the bootloader.
1090 static char linker_path[MAXPATHLEN] = "/;/boot;/modules";
1092 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1093 sizeof(linker_path), "module load search path");
1094 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1097 linker_strdup(const char *str)
1101 if ((result = kmalloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
1102 strcpy(result, str);
1107 linker_search_path(const char *name)
1109 struct nlookupdata nd;
1110 char *cp, *ep, *result;
1111 size_t name_len, prefix_len;
1116 /* qualified at all? */
1117 if (index(name, '/'))
1118 return(linker_strdup(name));
1120 /* traverse the linker path */
1122 name_len = strlen(name);
1125 /* find the end of this component */
1126 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1128 prefix_len = ep - cp;
1129 /* if this component doesn't end with a slash, add one */
1130 if (ep == cp || *(ep - 1) != '/')
1136 * +2 : possible separator, plus terminator.
1138 result = kmalloc(prefix_len + name_len + 2, M_LINKER, M_WAITOK);
1140 strncpy(result, cp, prefix_len);
1142 result[prefix_len++] = '/';
1143 strcpy(result + prefix_len, name);
1146 * Attempt to open the file, and return the path if we succeed and it's
1149 error = nlookup_init(&nd, result, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
1151 error = vn_open(&nd, NULL, FREAD, 0);
1153 type = nd.nl_open_vp->v_type;
1160 kfree(result, M_LINKER);