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.44 2008/09/01 19:39:44 dillon 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>
41 #include <sys/module.h>
42 #include <sys/queue.h>
43 #include <sys/linker.h>
44 #include <sys/fcntl.h>
45 #include <sys/libkern.h>
46 #include <sys/nlookup.h>
47 #include <sys/vnode.h>
48 #include <sys/sysctl.h>
50 #include <vm/vm_zone.h>
52 #include <sys/mplock2.h>
54 #ifdef _KERNEL_VIRTUAL
62 /* Metadata from the static kernel */
63 SET_DECLARE(modmetadata_set, struct mod_metadata);
64 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
66 linker_file_t linker_current_file;
67 linker_file_t linker_kernel_file;
69 static struct lock lock; /* lock for the file list */
70 static linker_class_list_t classes;
71 static linker_file_list_t linker_files;
72 static int next_file_id = 1;
74 /* XXX wrong name; we're looking at version provision tags here, not modules */
75 typedef TAILQ_HEAD(, modlist) modlisthead_t;
77 TAILQ_ENTRY(modlist) link; /* chain together all modules */
78 linker_file_t container;
82 typedef struct modlist *modlist_t;
83 static modlisthead_t found_modules;
86 static int linker_load_module(const char *kldname, const char *modname,
87 struct linker_file *parent, struct mod_depend *verinfo,
88 struct linker_file **lfpp);
91 linker_strdup(const char *str)
95 result = kmalloc(strlen(str) + 1, M_LINKER, M_WAITOK);
101 linker_init(void* arg)
103 lockinit(&lock, "klink", 0, 0);
104 TAILQ_INIT(&classes);
105 TAILQ_INIT(&linker_files);
108 SYSINIT(linker, SI_BOOT2_KLD, SI_ORDER_FIRST, linker_init, 0);
111 linker_add_class(const char* desc, void* priv,
112 struct linker_class_ops* ops)
116 lc = kmalloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT | M_ZERO);
123 TAILQ_INSERT_HEAD(&classes, lc, link);
129 linker_file_sysinit(linker_file_t lf)
131 struct sysinit** start, ** stop;
132 struct sysinit** sipp;
133 struct sysinit** xipp;
134 struct sysinit* save;
136 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
139 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
143 * Perform a bubble sort of the system initialization objects by
144 * their subsystem (primary key) and order (secondary key).
146 * Since some things care about execution order, this is the
147 * operation which ensures continued function.
149 for (sipp = start; sipp < stop; sipp++) {
150 for (xipp = sipp + 1; xipp < stop; xipp++) {
151 if ((*sipp)->subsystem < (*xipp)->subsystem ||
152 ((*sipp)->subsystem == (*xipp)->subsystem &&
153 (*sipp)->order <= (*xipp)->order))
163 * Traverse the (now) ordered list of system initialization tasks.
164 * Perform each task, and continue on to the next task.
166 for (sipp = start; sipp < stop; sipp++) {
167 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
168 continue; /* skip dummy task(s)*/
171 (*((*sipp)->func))((*sipp)->udata);
176 linker_file_sysuninit(linker_file_t lf)
178 struct sysinit** start, ** stop;
179 struct sysinit** sipp;
180 struct sysinit** xipp;
181 struct sysinit* save;
183 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
186 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
190 * Perform a reverse bubble sort of the system initialization objects
191 * by their subsystem (primary key) and order (secondary key).
193 * Since some things care about execution order, this is the
194 * operation which ensures continued function.
196 for (sipp = start; sipp < stop; sipp++) {
197 for (xipp = sipp + 1; xipp < stop; xipp++) {
198 if ((*sipp)->subsystem > (*xipp)->subsystem ||
199 ((*sipp)->subsystem == (*xipp)->subsystem &&
200 (*sipp)->order >= (*xipp)->order))
210 * Traverse the (now) ordered list of system initialization tasks.
211 * Perform each task, and continue on to the next task.
213 for (sipp = start; sipp < stop; sipp++) {
214 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
215 continue; /* skip dummy task(s)*/
218 (*((*sipp)->func))((*sipp)->udata);
223 linker_file_register_sysctls(linker_file_t lf)
225 struct sysctl_oid **start, **stop, **oidp;
227 KLD_DPF(FILE, ("linker_file_register_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_register_oid(*oidp);
237 linker_file_unregister_sysctls(linker_file_t lf)
239 struct sysctl_oid **start, **stop, **oidp;
241 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
244 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
246 for (oidp = start; oidp < stop; oidp++)
247 sysctl_unregister_oid(*oidp);
251 linker_file_register_modules(linker_file_t lf)
253 struct mod_metadata **start, **stop, **mdp;
254 const moduledata_t *moddata;
255 int first_error, error;
257 KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
260 if (linker_file_lookup_set(lf, "modmetadata_set", &start, &stop, NULL) != 0) {
262 * This fallback should be unnecessary, but if we get booted
263 * from boot2 instead of loader and we are missing our
264 * metadata then we have to try the best we can.
266 if (lf == linker_kernel_file) {
267 start = SET_BEGIN(modmetadata_set);
268 stop = SET_LIMIT(modmetadata_set);
273 for (mdp = start; mdp < stop; mdp++) {
274 if ((*mdp)->md_type != MDT_MODULE)
276 moddata = (*mdp)->md_data;
277 KLD_DPF(FILE, ("Registering module %s in %s\n", moddata->name, lf->filename));
278 error = module_register(moddata, lf);
280 kprintf("Module %s failed to register: %d\n", moddata->name, error);
281 if (first_error == 0)
285 return (first_error);
289 linker_init_kernel_modules(void)
292 linker_file_register_modules(linker_kernel_file);
295 SYSINIT(linker_kernel, SI_BOOT2_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0);
298 linker_load_file(const char *filename, linker_file_t *result)
302 int foundfile, error = 0;
304 /* Refuse to load modules if securelevel raised */
305 if (securelevel > 0 || kernel_mem_readonly)
308 lf = linker_find_file_by_name(filename);
310 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
318 TAILQ_FOREACH(lc, &classes, link) {
319 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
320 filename, lc->desc));
322 error = lc->ops->load_file(filename, &lf);
324 * If we got something other than ENOENT, then it exists but we cannot
325 * load it for some other reason.
330 error = linker_file_register_modules(lf);
331 if (error == EEXIST) {
332 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */);
335 linker_file_register_sysctls(lf);
336 linker_file_sysinit(lf);
337 lf->flags |= LINKER_FILE_LINKED;
343 * Less than ideal, but tells the user whether it failed to load or
344 * the module was not found.
348 * If the file type has not been recognized by the last try
349 * printout a message before to fail.
352 kprintf("linker_load_file: Unsupported file type\n");
355 * Format not recognized or otherwise unloadable.
356 * When loading a module that is statically built into
357 * the kernel EEXIST percolates back up as the return
358 * value. Preserve this so that apps can recognize this
364 error = ENOENT; /* Nothing found */
373 linker_find_file_by_name(const char* filename)
375 linker_file_t lf = 0;
379 for (i = strlen(filename); i > 0 && filename[i-1] != '/'; --i)
383 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
384 ksprintf(koname, "%s.ko", filename);
386 lockmgr(&lock, LK_SHARED);
387 TAILQ_FOREACH(lf, &linker_files, link) {
388 if (!strcmp(lf->filename, koname))
390 if (!strcmp(lf->filename, filename))
393 lockmgr(&lock, LK_RELEASE);
396 kfree(koname, M_LINKER);
401 linker_find_file_by_id(int fileid)
403 linker_file_t lf = 0;
405 lockmgr(&lock, LK_SHARED);
406 TAILQ_FOREACH(lf, &linker_files, link)
407 if (lf->id == fileid)
409 lockmgr(&lock, LK_RELEASE);
415 linker_file_foreach(linker_predicate_t *predicate, void *context)
420 lockmgr(&lock, LK_SHARED);
421 TAILQ_FOREACH(lf, &linker_files, link) {
422 retval = predicate(lf, context);
426 lockmgr(&lock, LK_RELEASE);
431 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
433 linker_file_t lf = 0;
434 const char *filename;
436 filename = rindex(pathname, '/');
437 if (filename && filename[1])
442 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
443 lockmgr(&lock, LK_EXCLUSIVE);
444 lf = kmalloc(sizeof(struct linker_file), M_LINKER, M_WAITOK);
445 bzero(lf, sizeof(*lf));
450 lf->filename = linker_strdup(filename);
451 lf->id = next_file_id++;
454 STAILQ_INIT(&lf->common);
455 TAILQ_INIT(&lf->modules);
459 TAILQ_INSERT_TAIL(&linker_files, lf, link);
461 lockmgr(&lock, LK_RELEASE);
466 linker_file_unload(linker_file_t file)
469 modlist_t ml, nextml;
470 struct common_symbol* cp;
474 /* Refuse to unload modules if securelevel raised */
475 if (securelevel > 0 || kernel_mem_readonly)
478 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
480 lockmgr(&lock, LK_EXCLUSIVE);
482 /* Easy case of just dropping a reference. */
483 if (file->refs > 1) {
485 lockmgr(&lock, LK_RELEASE);
489 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
492 * Inform any modules associated with this file.
494 mod = TAILQ_FIRST(&file->modules);
495 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
496 next = module_getfnext(mod);
499 * Give the module a chance to veto the unload. Note that the
500 * act of unloading the module may cause other modules in the
501 * same file list to be unloaded recursively.
503 if ((error = module_unload(mod)) != 0) {
504 KLD_DPF(FILE, ("linker_file_unload: module %p vetoes unload\n",
506 lockmgr(&lock, LK_RELEASE);
513 TAILQ_FOREACH_MUTABLE(ml, &found_modules, link, nextml) {
514 if (ml->container == file) {
515 TAILQ_REMOVE(&found_modules, ml, link);
520 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
521 if (file->flags & LINKER_FILE_LINKED) {
522 file->flags &= ~LINKER_FILE_LINKED;
523 lockmgr(&lock, LK_RELEASE);
524 linker_file_sysuninit(file);
525 linker_file_unregister_sysctls(file);
526 lockmgr(&lock, LK_EXCLUSIVE);
529 TAILQ_REMOVE(&linker_files, file, link);
532 lockmgr(&lock, LK_RELEASE);
533 for (i = 0; i < file->ndeps; i++)
534 linker_file_unload(file->deps[i]);
535 lockmgr(&lock, LK_EXCLUSIVE);
536 kfree(file->deps, M_LINKER);
540 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
541 STAILQ_REMOVE_HEAD(&file->common, link);
545 file->ops->unload(file);
547 if (file->filename) {
548 kfree(file->filename, M_LINKER);
549 file->filename = NULL;
552 kfree(file, M_LINKER);
554 lockmgr(&lock, LK_RELEASE);
561 linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
563 linker_file_t* newdeps;
565 newdeps = kmalloc((file->ndeps + 1) * sizeof(linker_file_t*),
566 M_LINKER, M_WAITOK | M_ZERO);
569 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
570 kfree(file->deps, M_LINKER);
572 file->deps = newdeps;
573 file->deps[file->ndeps] = dep;
578 * Locate a linker set and its contents.
579 * This is a helper function to avoid linker_if.h exposure elsewhere.
580 * Note: firstp and lastp are really void ***
583 linker_file_lookup_set(linker_file_t file, const char *name,
584 void *firstp, void *lastp, int *countp)
586 return file->ops->lookup_set(file, name, firstp, lastp, countp);
590 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
593 linker_symval_t symval;
595 size_t common_size = 0;
598 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
601 if (file->ops->lookup_symbol(file, name, &sym) == 0) {
602 file->ops->symbol_values(file, sym, &symval);
605 * XXX Assume a common symbol if its value is 0 and it has a non-zero
606 * size, otherwise it could be an absolute symbol with a value of 0.
608 if (symval.value == 0 && symval.size != 0) {
610 * For commons, first look them up in the dependancies and
611 * only allocate space if not found there.
613 common_size = symval.size;
615 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%p\n", symval.value));
616 *raddr = symval.value;
621 for (i = 0; i < file->ndeps; i++) {
622 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
623 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%p\n", *raddr));
628 /* If we have not found it in the dependencies, search globally */
629 TAILQ_FOREACH(lf, &linker_files, link) {
630 /* But skip the current file if it's on the list */
633 /* And skip the files we searched above */
634 for (i = 0; i < file->ndeps; i++)
635 if (lf == file->deps[i])
639 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
640 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%p\n", *raddr));
646 if (common_size > 0) {
648 * This is a common symbol which was not found in the
649 * dependancies. We maintain a simple common symbol table in
652 struct common_symbol* cp;
654 STAILQ_FOREACH(cp, &file->common, link)
655 if (!strcmp(cp->name, name)) {
656 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%p\n", cp->address));
657 *raddr = cp->address;
662 * Round the symbol size up to align.
664 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
665 cp = kmalloc(sizeof(struct common_symbol)
668 M_LINKER, M_WAITOK | M_ZERO);
670 cp->address = (caddr_t) (cp + 1);
671 cp->name = cp->address + common_size;
672 strcpy(cp->name, name);
673 bzero(cp->address, common_size);
674 STAILQ_INSERT_TAIL(&file->common, cp, link);
676 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%p\n", cp->address));
677 *raddr = cp->address;
681 #ifdef _KERNEL_VIRTUAL
682 *raddr = dlsym(RTLD_NEXT, name);
683 if (*raddr != NULL) {
684 KLD_DPF(SYM, ("linker_file_lookup_symbol: found dlsym=%p\n", *raddr));
689 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
695 * DDB Helpers. DDB has to look across multiple files with their own
696 * symbol tables and string tables.
698 * Note that we do not obey list locking protocols here. We really don't
699 * need DDB to hang because somebody's got the lock held. We'll take the
700 * chance that the files list is inconsistant instead.
704 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
708 TAILQ_FOREACH(lf, &linker_files, link) {
709 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
716 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
719 u_long off = (uintptr_t)value;
720 u_long diff, bestdiff;
726 TAILQ_FOREACH(lf, &linker_files, link) {
727 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
729 if (es != 0 && diff < bestdiff) {
748 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
752 TAILQ_FOREACH(lf, &linker_files, link) {
753 if (lf->ops->symbol_values(lf, sym, symval) == 0)
767 sys_kldload(struct kldload_args *uap)
769 struct thread *td = curthread;
771 char *kldname, *modname;
775 uap->sysmsg_result = -1;
777 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
780 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
783 file = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
784 if ((error = copyinstr(uap->file, file, MAXPATHLEN, NULL)) != 0)
788 * If file does not contain a qualified name or any dot in it
789 * (kldname.ko, or kldname.ver.ko) treat it as an interface
792 if (index(file, '/') || index(file, '.')) {
801 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
807 uap->sysmsg_result = lf->id;
819 sys_kldunload(struct kldunload_args *uap)
821 struct thread *td = curthread;
825 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
828 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
832 lf = linker_find_file_by_id(uap->fileid);
834 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
835 if (lf->userrefs == 0) {
836 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
841 error = linker_file_unload(lf);
856 sys_kldfind(struct kldfind_args *uap)
858 char *filename = NULL, *modulename;
862 uap->sysmsg_result = -1;
864 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
865 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
868 modulename = rindex(filename, '/');
869 if (modulename == NULL)
870 modulename = filename;
873 lf = linker_find_file_by_name(modulename);
875 uap->sysmsg_result = lf->id;
882 kfree(filename, M_TEMP);
890 sys_kldnext(struct kldnext_args *uap)
896 if (uap->fileid == 0) {
897 lf = TAILQ_FIRST(&linker_files);
899 lf = linker_find_file_by_id(uap->fileid);
904 lf = TAILQ_NEXT(lf, link);
907 /* Skip partially loaded files. */
908 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) {
909 lf = TAILQ_NEXT(lf, link);
913 uap->sysmsg_result = lf->id;
915 uap->sysmsg_result = 0;
926 sys_kldstat(struct kldstat_args *uap)
931 struct kld_file_stat* stat;
935 lf = linker_find_file_by_id(uap->fileid);
944 * Check the version of the user's structure.
946 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
948 if (version != sizeof(struct kld_file_stat)) {
953 namelen = strlen(lf->filename) + 1;
954 if (namelen > MAXPATHLEN)
955 namelen = MAXPATHLEN;
956 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
958 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
960 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
962 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
964 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
967 uap->sysmsg_result = 0;
978 sys_kldfirstmod(struct kldfirstmod_args *uap)
984 lf = linker_find_file_by_id(uap->fileid);
986 if (TAILQ_FIRST(&lf->modules))
987 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules));
989 uap->sysmsg_result = 0;
1002 sys_kldsym(struct kldsym_args *uap)
1004 char *symstr = NULL;
1006 linker_symval_t symval;
1008 struct kld_sym_lookup lookup;
1012 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1014 if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP) {
1019 symstr = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1020 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1023 if (uap->fileid != 0) {
1024 lf = linker_find_file_by_id(uap->fileid);
1029 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
1030 lf->ops->symbol_values(lf, sym, &symval) == 0) {
1031 lookup.symvalue = (uintptr_t)symval.value;
1032 lookup.symsize = symval.size;
1033 error = copyout(&lookup, uap->data, sizeof(lookup));
1037 TAILQ_FOREACH(lf, &linker_files, link) {
1038 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
1039 lf->ops->symbol_values(lf, sym, &symval) == 0) {
1040 lookup.symvalue = (uintptr_t)symval.value;
1041 lookup.symsize = symval.size;
1042 error = copyout(&lookup, uap->data, sizeof(lookup));
1052 kfree(symstr, M_TEMP);
1057 * Preloaded module support
1061 modlist_lookup(const char *name, int ver)
1065 TAILQ_FOREACH(mod, &found_modules, link) {
1066 if (strcmp(mod->name, name) == 0 && (ver == 0 || mod->version == ver))
1073 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1075 modlist_t mod, bestmod;
1078 if (verinfo == NULL)
1079 return (modlist_lookup(name, 0));
1081 TAILQ_FOREACH(mod, &found_modules, link) {
1082 if (strcmp(mod->name, name) != 0)
1085 if (ver == verinfo->md_ver_preferred)
1087 if (ver >= verinfo->md_ver_minimum &&
1088 ver <= verinfo->md_ver_maximum &&
1089 (bestmod == NULL || ver > bestmod->version))
1096 linker_reference_module(const char *modname, struct mod_depend *verinfo,
1097 linker_file_t *result)
1102 lockmgr(&lock, LK_SHARED);
1103 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
1104 *result = mod->container;
1106 lockmgr(&lock, LK_RELEASE);
1110 lockmgr(&lock, LK_RELEASE);
1112 error = linker_load_module(NULL, modname, NULL, verinfo, result);
1118 linker_release_module(const char *modname, struct mod_depend *verinfo,
1124 lockmgr(&lock, LK_SHARED);
1126 KASSERT(modname != NULL,
1127 ("linker_release_module: no file or name"));
1128 mod = modlist_lookup2(modname, verinfo);
1130 lockmgr(&lock, LK_RELEASE);
1133 lf = mod->container;
1135 KASSERT(modname == NULL && verinfo == NULL,
1136 ("linker_release_module: both file and name"));
1137 lockmgr(&lock, LK_RELEASE);
1139 error = linker_file_unload(lf);
1145 modlist_newmodule(const char *modname, int version, linker_file_t container)
1149 mod = kmalloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1151 panic("no memory for module list");
1152 mod->container = container;
1153 mod->name = modname;
1154 mod->version = version;
1155 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1160 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1161 struct mod_metadata **stop, int preload)
1163 struct mod_metadata *mp, **mdp;
1164 const char *modname;
1167 for (mdp = start; mdp < stop; mdp++) {
1169 if (mp->md_type != MDT_VERSION)
1171 modname = mp->md_cval;
1172 ver = ((struct mod_version *)mp->md_data)->mv_version;
1173 if (modlist_lookup(modname, ver) != NULL) {
1174 kprintf("module %s already present!\n", modname);
1175 /* XXX what can we do? this is a build error. :-( */
1178 modlist_newmodule(modname, ver, lf);
1183 linker_preload(void* arg)
1186 const char *modname, *nmodname;
1188 linker_file_t lf, nlf;
1191 linker_file_list_t loaded_files;
1192 linker_file_list_t depended_files;
1193 struct mod_metadata *mp, *nmp;
1194 struct mod_metadata **start, **stop, **mdp, **nmdp;
1195 struct mod_depend *verinfo;
1199 struct sysinit **si_start, **si_stop;
1201 TAILQ_INIT(&loaded_files);
1202 TAILQ_INIT(&depended_files);
1203 TAILQ_INIT(&found_modules);
1206 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1207 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1208 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1209 if (modname == NULL) {
1210 kprintf("Preloaded module at %p does not have a name!\n", modptr);
1213 if (modtype == NULL) {
1214 kprintf("Preloaded module at %p does not have a type!\n", modptr);
1219 kprintf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
1221 TAILQ_FOREACH(lc, &classes, link) {
1222 error = lc->ops->preload_file(modname, &lf);
1228 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1232 * First get a list of stuff in the kernel.
1234 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1236 linker_addmodules(linker_kernel_file, start, stop, 1);
1239 * This is a once-off kinky bubble sort to resolve relocation
1240 * dependency requirements.
1243 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1244 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
1246 * First, look to see if we would successfully link with this
1249 resolves = 1; /* unless we know otherwise */
1251 for (mdp = start; mdp < stop; mdp++) {
1253 if (mp->md_type != MDT_DEPEND)
1255 modname = mp->md_cval;
1256 verinfo = mp->md_data;
1257 for (nmdp = start; nmdp < stop; nmdp++) {
1259 if (nmp->md_type != MDT_VERSION)
1261 nmodname = nmp->md_cval;
1262 if (strcmp(modname, nmodname) == 0)
1265 if (nmdp < stop)/* it's a self reference */
1269 * ok, the module isn't here yet, we
1272 if (modlist_lookup2(modname, verinfo) == NULL)
1277 * OK, if we found our modules, we can link. So, "provide"
1278 * the modules inside and add it to the end of the link order
1283 for (mdp = start; mdp < stop; mdp++) {
1285 if (mp->md_type != MDT_VERSION)
1287 modname = mp->md_cval;
1288 nver = ((struct mod_version *)mp->md_data)->mv_version;
1289 if (modlist_lookup(modname, nver) != NULL) {
1290 kprintf("module %s already present!\n", modname);
1291 TAILQ_REMOVE(&loaded_files, lf, loaded);
1292 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
1293 /* we changed tailq next ptr */
1296 modlist_newmodule(modname, nver, lf);
1299 TAILQ_REMOVE(&loaded_files, lf, loaded);
1300 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1302 * Since we provided modules, we need to restart the
1303 * sort so that the previous files that depend on us
1304 * have a chance. Also, we've busted the tailq next
1305 * pointer with the REMOVE.
1312 * At this point, we check to see what could not be resolved..
1314 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1315 TAILQ_REMOVE(&loaded_files, lf, loaded);
1316 kprintf("KLD file %s is missing dependencies\n", lf->filename);
1317 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
1321 * We made it. Finish off the linking in the order we determined.
1323 TAILQ_FOREACH_MUTABLE(lf, &depended_files, loaded, nlf) {
1324 if (linker_kernel_file) {
1325 linker_kernel_file->refs++;
1326 linker_file_add_dependancy(lf, linker_kernel_file);
1330 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
1332 for (mdp = start; mdp < stop; mdp++) {
1334 if (mp->md_type != MDT_DEPEND)
1336 modname = mp->md_cval;
1337 verinfo = mp->md_data;
1338 mod = modlist_lookup2(modname, verinfo);
1339 /* Don't count self-dependencies */
1340 if (lf == mod->container)
1342 mod->container->refs++;
1343 linker_file_add_dependancy(lf, mod->container);
1347 * Now do relocation etc using the symbol search paths
1348 * established by the dependencies
1350 error = lf->ops->preload_finish(lf);
1352 TAILQ_REMOVE(&depended_files, lf, loaded);
1353 kprintf("KLD file %s - could not finalize loading\n",
1355 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */);
1358 linker_file_register_modules(lf);
1359 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0)
1360 sysinit_add(si_start, si_stop);
1361 linker_file_register_sysctls(lf);
1362 lf->flags |= LINKER_FILE_LINKED;
1364 /* woohoo! we made it! */
1367 SYSINIT(preload, SI_BOOT2_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1370 * Search for a not-loaded module by name.
1372 * Modules may be found in the following locations:
1374 * - preloaded (result is just the module name)
1375 * - on disk (result is full path to module)
1377 * If the module name is qualified in any way (contains path, etc.)
1378 * the we simply return a copy of it.
1380 * The search path can be manipulated via sysctl. Note that we use the ';'
1381 * character as a separator to be consistent with the bootloader.
1384 static char linker_path[MAXPATHLEN] = "/boot;/boot/modules;/;/modules";
1386 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1387 sizeof(linker_path), "module load search path");
1388 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1391 linker_search_path(const char *name)
1393 struct nlookupdata nd;
1394 char *cp, *ep, *result;
1395 size_t name_len, prefix_len;
1400 const char *exts[] = { "", ".ko", NULL };
1403 /* qualified at all? */
1404 if (index(name, '/'))
1405 return(linker_strdup(name));
1407 /* traverse the linker path */
1409 name_len = strlen(name);
1412 /* find the end of this component */
1413 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1415 prefix_len = ep - cp;
1416 /* if this component doesn't end with a slash, add one */
1417 if (ep == cp || *(ep - 1) != '/')
1423 * +2+3 : possible separator, plus terminator + possible extension.
1425 result = kmalloc(prefix_len + name_len + 2+3, M_LINKER, M_WAITOK);
1427 strncpy(result, cp, prefix_len);
1429 result[prefix_len++] = '/';
1430 strcpy(result + prefix_len, name);
1432 result_len = strlen(result);
1433 for (ext = exts; *ext != NULL; ext++) {
1434 strcpy(result + result_len, *ext);
1437 * Attempt to open the file, and return the path if we succeed and it's
1440 error = nlookup_init(&nd, result, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
1442 error = vn_open(&nd, NULL, FREAD, 0);
1444 type = nd.nl_open_vp->v_type;
1453 kfree(result, M_LINKER);
1463 * Find a file which contains given module and load it, if "parent" is not
1464 * NULL, register a reference to it.
1467 linker_load_module(const char *kldname, const char *modname,
1468 struct linker_file *parent, struct mod_depend *verinfo,
1469 struct linker_file **lfpp)
1471 linker_file_t lfdep;
1472 const char *filename;
1476 if (modname == NULL) {
1478 * We have to load KLD
1480 KASSERT(verinfo == NULL, ("linker_load_module: verinfo is not NULL"));
1481 pathname = linker_search_path(kldname);
1483 if (modlist_lookup2(modname, verinfo) != NULL)
1485 if (kldname != NULL)
1487 pathname = linker_strdup(kldname);
1489 else if (rootvnode == NULL)
1493 pathname = linker_search_path(modname);
1497 * Need to find a KLD with required module
1499 pathname = linker_search_module(modname,
1500 strlen(modname), verinfo);
1503 if (pathname == NULL)
1507 * Can't load more than one file with the same basename XXX:
1508 * Actually it should be possible to have multiple KLDs with
1509 * the same basename but different path because they can
1510 * provide different versions of the same modules.
1512 filename = rindex(pathname, '/');
1513 if (filename == NULL)
1514 filename = filename;
1517 if (linker_find_file_by_name(filename))
1521 error = linker_load_file(pathname, &lfdep);
1524 if (modname && verinfo && modlist_lookup2(modname, verinfo) == NULL) {
1525 linker_file_unload(lfdep /* , LINKER_UNLOAD_FORCE */ );
1530 linker_file_add_dependancy(parent, lfdep);
1535 kfree(pathname, M_LINKER);
1540 * This routine is responsible for finding dependencies of userland initiated
1541 * kldload(2)'s of files.
1544 linker_load_dependencies(linker_file_t lf)
1546 linker_file_t lfdep;
1547 struct mod_metadata **start, **stop, **mdp, **nmdp;
1548 struct mod_metadata *mp, *nmp;
1549 struct mod_depend *verinfo;
1551 const char *modname, *nmodname;
1552 int ver, error = 0, count;
1555 * All files are dependant on /kernel.
1557 if (linker_kernel_file) {
1558 linker_kernel_file->refs++;
1559 linker_file_add_dependancy(lf, linker_kernel_file);
1561 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, &count) != 0)
1563 for (mdp = start; mdp < stop; mdp++) {
1565 if (mp->md_type != MDT_VERSION)
1567 modname = mp->md_cval;
1568 ver = ((struct mod_version *)mp->md_data)->mv_version;
1569 mod = modlist_lookup(modname, ver);
1571 kprintf("interface %s.%d already present in the KLD '%s'!\n",
1572 modname, ver, mod->container->filename);
1577 for (mdp = start; mdp < stop; mdp++) {
1579 if (mp->md_type != MDT_DEPEND)
1581 modname = mp->md_cval;
1582 verinfo = mp->md_data;
1584 for (nmdp = start; nmdp < stop; nmdp++) {
1586 if (nmp->md_type != MDT_VERSION)
1588 nmodname = nmp->md_cval;
1589 if (strcmp(modname, nmodname) == 0)
1592 if (nmdp < stop) /* early exit, it's a self reference */
1594 mod = modlist_lookup2(modname, verinfo);
1595 if (mod) { /* woohoo, it's loaded already */
1596 lfdep = mod->container;
1598 linker_file_add_dependancy(lf, lfdep);
1601 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
1603 kprintf("KLD %s: depends on %s - not available or version mismatch\n",
1604 lf->filename, modname);
1611 linker_addmodules(lf, start, stop, 0);