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 $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/sysproto.h>
36 #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 #include <sys/mplock2.h>
53 #ifdef _KERNEL_VIRTUAL
61 /* Metadata from the static kernel */
62 SET_DECLARE(modmetadata_set
, struct mod_metadata
);
63 MALLOC_DEFINE(M_LINKER
, "kld", "kernel linker");
65 linker_file_t linker_current_file
;
66 linker_file_t linker_kernel_file
;
68 static struct lock lock
; /* lock for the file list */
69 static linker_class_list_t classes
;
70 static linker_file_list_t linker_files
;
71 static int next_file_id
= 1;
73 /* XXX wrong name; we're looking at version provision tags here, not modules */
74 typedef TAILQ_HEAD(, modlist
) modlisthead_t
;
76 TAILQ_ENTRY(modlist
) link
; /* chain together all modules */
77 linker_file_t container
;
81 typedef struct modlist
*modlist_t
;
82 static modlisthead_t found_modules
;
85 static int linker_load_module(const char *kldname
, const char *modname
,
86 struct linker_file
*parent
, struct mod_depend
*verinfo
,
87 struct linker_file
**lfpp
);
90 linker_strdup(const char *str
)
94 result
= kmalloc(strlen(str
) + 1, M_LINKER
, M_WAITOK
);
100 linker_init(void* arg
)
102 lockinit(&lock
, "klink", 0, 0);
103 TAILQ_INIT(&classes
);
104 TAILQ_INIT(&linker_files
);
107 SYSINIT(linker
, SI_BOOT2_KLD
, SI_ORDER_FIRST
, linker_init
, 0);
110 linker_add_class(const char* desc
, void* priv
,
111 struct linker_class_ops
* ops
)
115 lc
= kmalloc(sizeof(struct linker_class
), M_LINKER
, M_NOWAIT
| M_ZERO
);
122 TAILQ_INSERT_HEAD(&classes
, lc
, link
);
128 linker_file_sysinit(linker_file_t lf
)
130 struct sysinit
** start
, ** stop
;
131 struct sysinit
** sipp
;
132 struct sysinit
** xipp
;
133 struct sysinit
* save
;
135 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
138 if (linker_file_lookup_set(lf
, "sysinit_set", &start
, &stop
, NULL
) != 0)
142 * Perform a bubble sort of the system initialization objects by
143 * their subsystem (primary key) and order (secondary key).
145 * Since some things care about execution order, this is the
146 * operation which ensures continued function.
148 for (sipp
= start
; sipp
< stop
; sipp
++) {
149 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
150 if ((*sipp
)->subsystem
< (*xipp
)->subsystem
||
151 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
152 (*sipp
)->order
<= (*xipp
)->order
))
162 * Traverse the (now) ordered list of system initialization tasks.
163 * Perform each task, and continue on to the next task.
165 for (sipp
= start
; sipp
< stop
; sipp
++) {
166 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
167 continue; /* skip dummy task(s)*/
170 (*((*sipp
)->func
))((*sipp
)->udata
);
175 linker_file_sysuninit(linker_file_t lf
)
177 struct sysinit
** start
, ** stop
;
178 struct sysinit
** sipp
;
179 struct sysinit
** xipp
;
180 struct sysinit
* save
;
182 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
185 if (linker_file_lookup_set(lf
, "sysuninit_set", &start
, &stop
, NULL
) != 0)
189 * Perform a reverse bubble sort of the system initialization objects
190 * by their subsystem (primary key) and order (secondary key).
192 * Since some things care about execution order, this is the
193 * operation which ensures continued function.
195 for (sipp
= start
; sipp
< stop
; sipp
++) {
196 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
197 if ((*sipp
)->subsystem
> (*xipp
)->subsystem
||
198 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
199 (*sipp
)->order
>= (*xipp
)->order
))
209 * Traverse the (now) ordered list of system initialization tasks.
210 * Perform each task, and continue on to the next task.
212 for (sipp
= start
; sipp
< stop
; sipp
++) {
213 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
214 continue; /* skip dummy task(s)*/
217 (*((*sipp
)->func
))((*sipp
)->udata
);
222 linker_file_register_sysctls(linker_file_t lf
)
224 struct sysctl_oid
**start
, **stop
, **oidp
;
226 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
229 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
231 for (oidp
= start
; oidp
< stop
; oidp
++)
232 sysctl_register_oid(*oidp
);
236 linker_file_unregister_sysctls(linker_file_t lf
)
238 struct sysctl_oid
**start
, **stop
, **oidp
;
240 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
243 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
245 for (oidp
= start
; oidp
< stop
; oidp
++)
246 sysctl_unregister_oid(*oidp
);
250 linker_file_register_modules(linker_file_t lf
)
252 struct mod_metadata
**start
, **stop
, **mdp
;
253 const moduledata_t
*moddata
;
254 int first_error
, error
;
256 KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
259 if (linker_file_lookup_set(lf
, "modmetadata_set", &start
, &stop
, NULL
) != 0) {
261 * This fallback should be unnecessary, but if we get booted
262 * from boot2 instead of loader and we are missing our
263 * metadata then we have to try the best we can.
265 if (lf
== linker_kernel_file
) {
266 start
= SET_BEGIN(modmetadata_set
);
267 stop
= SET_LIMIT(modmetadata_set
);
272 for (mdp
= start
; mdp
< stop
; mdp
++) {
273 if ((*mdp
)->md_type
!= MDT_MODULE
)
275 moddata
= (*mdp
)->md_data
;
276 KLD_DPF(FILE, ("Registering module %s in %s\n", moddata
->name
, lf
->filename
));
277 error
= module_register(moddata
, lf
);
279 kprintf("Module %s failed to register: %d\n", moddata
->name
, error
);
280 if (first_error
== 0)
284 return (first_error
);
288 linker_init_kernel_modules(void)
291 linker_file_register_modules(linker_kernel_file
);
294 SYSINIT(linker_kernel
, SI_BOOT2_KLD
, SI_ORDER_ANY
, linker_init_kernel_modules
, 0);
297 linker_load_file(const char *filename
, linker_file_t
*result
)
301 int foundfile
, error
= 0;
303 /* Refuse to load modules if securelevel raised */
304 if (securelevel
> 0 || kernel_mem_readonly
)
307 lf
= linker_find_file_by_name(filename
);
309 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename
));
317 TAILQ_FOREACH(lc
, &classes
, link
) {
318 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
319 filename
, lc
->desc
));
321 error
= lc
->ops
->load_file(filename
, &lf
);
323 * If we got something other than ENOENT, then it exists but we cannot
324 * load it for some other reason.
329 error
= linker_file_register_modules(lf
);
330 if (error
== EEXIST
) {
331 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */);
334 linker_file_register_sysctls(lf
);
335 linker_file_sysinit(lf
);
336 lf
->flags
|= LINKER_FILE_LINKED
;
342 * Less than ideal, but tells the user whether it failed to load or
343 * the module was not found.
347 * If the file type has not been recognized by the last try
348 * printout a message before to fail.
351 kprintf("linker_load_file: Unsupported file type\n");
354 * Format not recognized or otherwise unloadable.
355 * When loading a module that is statically built into
356 * the kernel EEXIST percolates back up as the return
357 * value. Preserve this so that apps can recognize this
363 error
= ENOENT
; /* Nothing found */
372 linker_find_file_by_name(const char* filename
)
374 linker_file_t lf
= NULL
;
378 for (i
= strlen(filename
); i
> 0 && filename
[i
-1] != '/'; --i
)
382 koname
= kmalloc(strlen(filename
) + 4, M_LINKER
, M_WAITOK
);
383 ksprintf(koname
, "%s.ko", filename
);
385 lockmgr(&lock
, LK_SHARED
);
386 TAILQ_FOREACH(lf
, &linker_files
, link
) {
387 if (!strcmp(lf
->filename
, koname
))
389 if (!strcmp(lf
->filename
, filename
))
392 lockmgr(&lock
, LK_RELEASE
);
395 kfree(koname
, M_LINKER
);
400 linker_find_file_by_id(int fileid
)
402 linker_file_t lf
= NULL
;
404 lockmgr(&lock
, LK_SHARED
);
405 TAILQ_FOREACH(lf
, &linker_files
, link
)
406 if (lf
->id
== fileid
)
408 lockmgr(&lock
, LK_RELEASE
);
414 linker_file_foreach(linker_predicate_t
*predicate
, void *context
)
419 lockmgr(&lock
, LK_SHARED
);
420 TAILQ_FOREACH(lf
, &linker_files
, link
) {
421 retval
= predicate(lf
, context
);
425 lockmgr(&lock
, LK_RELEASE
);
430 linker_make_file(const char* pathname
, void* priv
, struct linker_file_ops
* ops
)
432 linker_file_t lf
= NULL
;
433 const char *filename
;
435 filename
= rindex(pathname
, '/');
436 if (filename
&& filename
[1])
441 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename
));
442 lockmgr(&lock
, LK_EXCLUSIVE
);
443 lf
= kmalloc(sizeof(struct linker_file
), M_LINKER
, M_WAITOK
| M_ZERO
);
447 lf
->filename
= linker_strdup(filename
);
448 lf
->id
= next_file_id
++;
451 STAILQ_INIT(&lf
->common
);
452 TAILQ_INIT(&lf
->modules
);
456 TAILQ_INSERT_TAIL(&linker_files
, lf
, link
);
458 lockmgr(&lock
, LK_RELEASE
);
463 linker_file_unload(linker_file_t file
)
466 modlist_t ml
, nextml
;
467 struct common_symbol
* cp
;
471 /* Refuse to unload modules if securelevel raised */
472 if (securelevel
> 0 || kernel_mem_readonly
)
475 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file
->refs
));
477 lockmgr(&lock
, LK_EXCLUSIVE
);
479 /* Easy case of just dropping a reference. */
480 if (file
->refs
> 1) {
482 lockmgr(&lock
, LK_RELEASE
);
486 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
489 * Inform any modules associated with this file.
491 mod
= TAILQ_FIRST(&file
->modules
);
492 for (mod
= TAILQ_FIRST(&file
->modules
); mod
; mod
= next
) {
493 next
= module_getfnext(mod
);
496 * Give the module a chance to veto the unload. Note that the
497 * act of unloading the module may cause other modules in the
498 * same file list to be unloaded recursively.
500 if ((error
= module_unload(mod
)) != 0) {
501 KLD_DPF(FILE, ("linker_file_unload: module %p vetoes unload\n",
503 lockmgr(&lock
, LK_RELEASE
);
510 TAILQ_FOREACH_MUTABLE(ml
, &found_modules
, link
, nextml
) {
511 if (ml
->container
== file
) {
512 TAILQ_REMOVE(&found_modules
, ml
, link
);
517 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
518 if (file
->flags
& LINKER_FILE_LINKED
) {
519 file
->flags
&= ~LINKER_FILE_LINKED
;
520 lockmgr(&lock
, LK_RELEASE
);
521 linker_file_sysuninit(file
);
522 linker_file_unregister_sysctls(file
);
523 lockmgr(&lock
, LK_EXCLUSIVE
);
526 TAILQ_REMOVE(&linker_files
, file
, link
);
529 lockmgr(&lock
, LK_RELEASE
);
530 for (i
= 0; i
< file
->ndeps
; i
++)
531 linker_file_unload(file
->deps
[i
]);
532 lockmgr(&lock
, LK_EXCLUSIVE
);
533 kfree(file
->deps
, M_LINKER
);
537 while ((cp
= STAILQ_FIRST(&file
->common
)) != NULL
) {
538 STAILQ_REMOVE_HEAD(&file
->common
, link
);
542 file
->ops
->unload(file
);
544 if (file
->filename
) {
545 kfree(file
->filename
, M_LINKER
);
546 file
->filename
= NULL
;
549 kfree(file
, M_LINKER
);
551 lockmgr(&lock
, LK_RELEASE
);
558 linker_file_add_dependancy(linker_file_t file
, linker_file_t dep
)
560 linker_file_t
* newdeps
;
562 newdeps
= kmalloc((file
->ndeps
+ 1) * sizeof(linker_file_t
*),
563 M_LINKER
, M_WAITOK
| M_ZERO
);
566 bcopy(file
->deps
, newdeps
, file
->ndeps
* sizeof(linker_file_t
*));
567 kfree(file
->deps
, M_LINKER
);
569 file
->deps
= newdeps
;
570 file
->deps
[file
->ndeps
] = dep
;
575 * Locate a linker set and its contents.
576 * This is a helper function to avoid linker_if.h exposure elsewhere.
577 * Note: firstp and lastp are really void ***
580 linker_file_lookup_set(linker_file_t file
, const char *name
,
581 void *firstp
, void *lastp
, int *countp
)
583 return file
->ops
->lookup_set(file
, name
, firstp
, lastp
, countp
);
587 linker_file_lookup_symbol(linker_file_t file
, const char* name
, int deps
, caddr_t
*raddr
)
590 linker_symval_t symval
;
592 size_t common_size
= 0;
595 KLD_DPF(SYM
, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
598 if (file
->ops
->lookup_symbol(file
, name
, &sym
) == 0) {
599 file
->ops
->symbol_values(file
, sym
, &symval
);
602 * XXX Assume a common symbol if its value is 0 and it has a non-zero
603 * size, otherwise it could be an absolute symbol with a value of 0.
605 if (symval
.value
== NULL
&& symval
.size
!= 0) {
607 * For commons, first look them up in the dependancies and
608 * only allocate space if not found there.
610 common_size
= symval
.size
;
612 KLD_DPF(SYM
, ("linker_file_lookup_symbol: symbol.value=%p\n", symval
.value
));
613 *raddr
= symval
.value
;
618 for (i
= 0; i
< file
->ndeps
; i
++) {
619 if (linker_file_lookup_symbol(file
->deps
[i
], name
, 0, raddr
) == 0) {
620 KLD_DPF(SYM
, ("linker_file_lookup_symbol: deps value=%p\n", *raddr
));
625 /* If we have not found it in the dependencies, search globally */
626 TAILQ_FOREACH(lf
, &linker_files
, link
) {
627 /* But skip the current file if it's on the list */
630 /* And skip the files we searched above */
631 for (i
= 0; i
< file
->ndeps
; i
++)
632 if (lf
== file
->deps
[i
])
636 if (linker_file_lookup_symbol(lf
, name
, 0, raddr
) == 0) {
637 KLD_DPF(SYM
, ("linker_file_lookup_symbol: global value=%p\n", *raddr
));
643 if (common_size
> 0) {
645 * This is a common symbol which was not found in the
646 * dependancies. We maintain a simple common symbol table in
649 struct common_symbol
* cp
;
651 STAILQ_FOREACH(cp
, &file
->common
, link
)
652 if (!strcmp(cp
->name
, name
)) {
653 KLD_DPF(SYM
, ("linker_file_lookup_symbol: old common value=%p\n", cp
->address
));
654 *raddr
= cp
->address
;
659 * Round the symbol size up to align.
661 common_size
= (common_size
+ sizeof(int) - 1) & -sizeof(int);
662 cp
= kmalloc(sizeof(struct common_symbol
)
665 M_LINKER
, M_WAITOK
| M_ZERO
);
667 cp
->address
= (caddr_t
) (cp
+ 1);
668 cp
->name
= cp
->address
+ common_size
;
669 strcpy(cp
->name
, name
);
670 bzero(cp
->address
, common_size
);
671 STAILQ_INSERT_TAIL(&file
->common
, cp
, link
);
673 KLD_DPF(SYM
, ("linker_file_lookup_symbol: new common value=%p\n", cp
->address
));
674 *raddr
= cp
->address
;
678 #ifdef _KERNEL_VIRTUAL
679 *raddr
= dlsym(RTLD_NEXT
, name
);
680 if (*raddr
!= NULL
) {
681 KLD_DPF(SYM
, ("linker_file_lookup_symbol: found dlsym=%p\n", *raddr
));
686 KLD_DPF(SYM
, ("linker_file_lookup_symbol: fail\n"));
692 * DDB Helpers. DDB has to look across multiple files with their own
693 * symbol tables and string tables.
695 * Note that we do not obey list locking protocols here. We really don't
696 * need DDB to hang because somebody's got the lock held. We'll take the
697 * chance that the files list is inconsistant instead.
701 linker_ddb_lookup(const char *symstr
, c_linker_sym_t
*sym
)
705 TAILQ_FOREACH(lf
, &linker_files
, link
) {
706 if (lf
->ops
->lookup_symbol(lf
, symstr
, sym
) == 0)
713 linker_ddb_search_symbol(caddr_t value
, c_linker_sym_t
*sym
, long *diffp
)
716 u_long off
= (uintptr_t)value
;
717 u_long diff
, bestdiff
;
723 TAILQ_FOREACH(lf
, &linker_files
, link
) {
724 if (lf
->ops
->search_symbol(lf
, value
, &es
, &diff
) != 0)
726 if (es
!= NULL
&& diff
< bestdiff
) {
745 linker_ddb_symbol_values(c_linker_sym_t sym
, linker_symval_t
*symval
)
749 TAILQ_FOREACH(lf
, &linker_files
, link
) {
750 if (lf
->ops
->symbol_values(lf
, sym
, symval
) == 0)
764 sys_kldload(struct kldload_args
*uap
)
766 struct thread
*td
= curthread
;
768 char *kldname
, *modname
;
772 uap
->sysmsg_result
= -1;
774 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
777 if ((error
= priv_check(td
, PRIV_KLD_LOAD
)) != 0)
780 file
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
781 if ((error
= copyinstr(uap
->file
, file
, MAXPATHLEN
, NULL
)) != 0)
785 * If file does not contain a qualified name or any dot in it
786 * (kldname.ko, or kldname.ver.ko) treat it as an interface
789 if (index(file
, '/') || index(file
, '.')) {
798 error
= linker_load_module(kldname
, modname
, NULL
, NULL
, &lf
);
804 uap
->sysmsg_result
= lf
->id
;
816 sys_kldunload(struct kldunload_args
*uap
)
818 struct thread
*td
= curthread
;
822 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
825 if ((error
= priv_check(td
, PRIV_KLD_UNLOAD
)) != 0)
829 lf
= linker_find_file_by_id(uap
->fileid
);
831 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf
->userrefs
));
832 if (lf
->userrefs
== 0) {
833 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
838 error
= linker_file_unload(lf
);
853 sys_kldfind(struct kldfind_args
*uap
)
855 char *filename
= NULL
, *modulename
;
859 uap
->sysmsg_result
= -1;
861 filename
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
862 if ((error
= copyinstr(uap
->file
, filename
, MAXPATHLEN
, NULL
)) != 0)
865 modulename
= rindex(filename
, '/');
866 if (modulename
== NULL
)
867 modulename
= filename
;
870 lf
= linker_find_file_by_name(modulename
);
872 uap
->sysmsg_result
= lf
->id
;
879 kfree(filename
, M_TEMP
);
887 sys_kldnext(struct kldnext_args
*uap
)
893 if (uap
->fileid
== 0) {
894 lf
= TAILQ_FIRST(&linker_files
);
896 lf
= linker_find_file_by_id(uap
->fileid
);
901 lf
= TAILQ_NEXT(lf
, link
);
904 /* Skip partially loaded files. */
905 while (lf
!= NULL
&& !(lf
->flags
& LINKER_FILE_LINKED
)) {
906 lf
= TAILQ_NEXT(lf
, link
);
910 uap
->sysmsg_result
= lf
->id
;
912 uap
->sysmsg_result
= 0;
923 sys_kldstat(struct kldstat_args
*uap
)
928 struct kld_file_stat
* stat
;
932 lf
= linker_find_file_by_id(uap
->fileid
);
941 * Check the version of the user's structure.
943 if ((error
= copyin(&stat
->version
, &version
, sizeof(version
))) != 0)
945 if (version
!= sizeof(struct kld_file_stat
)) {
950 namelen
= strlen(lf
->filename
) + 1;
951 if (namelen
> MAXPATHLEN
)
952 namelen
= MAXPATHLEN
;
953 if ((error
= copyout(lf
->filename
, &stat
->name
[0], namelen
)) != 0)
955 if ((error
= copyout(&lf
->refs
, &stat
->refs
, sizeof(int))) != 0)
957 if ((error
= copyout(&lf
->id
, &stat
->id
, sizeof(int))) != 0)
959 if ((error
= copyout(&lf
->address
, &stat
->address
, sizeof(caddr_t
))) != 0)
961 if ((error
= copyout(&lf
->size
, &stat
->size
, sizeof(size_t))) != 0)
964 uap
->sysmsg_result
= 0;
975 sys_kldfirstmod(struct kldfirstmod_args
*uap
)
981 lf
= linker_find_file_by_id(uap
->fileid
);
983 if (TAILQ_FIRST(&lf
->modules
))
984 uap
->sysmsg_result
= module_getid(TAILQ_FIRST(&lf
->modules
));
986 uap
->sysmsg_result
= 0;
999 sys_kldsym(struct kldsym_args
*uap
)
1001 char *symstr
= NULL
;
1003 linker_symval_t symval
;
1005 struct kld_sym_lookup lookup
;
1009 if ((error
= copyin(uap
->data
, &lookup
, sizeof(lookup
))) != 0)
1011 if (lookup
.version
!= sizeof(lookup
) || uap
->cmd
!= KLDSYM_LOOKUP
) {
1016 symstr
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
1017 if ((error
= copyinstr(lookup
.symname
, symstr
, MAXPATHLEN
, NULL
)) != 0)
1020 if (uap
->fileid
!= 0) {
1021 lf
= linker_find_file_by_id(uap
->fileid
);
1026 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
1027 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
1028 lookup
.symvalue
= (uintptr_t)symval
.value
;
1029 lookup
.symsize
= symval
.size
;
1030 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
1034 TAILQ_FOREACH(lf
, &linker_files
, link
) {
1035 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
1036 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
1037 lookup
.symvalue
= (uintptr_t)symval
.value
;
1038 lookup
.symsize
= symval
.size
;
1039 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
1049 kfree(symstr
, M_TEMP
);
1054 * Preloaded module support
1058 modlist_lookup(const char *name
, int ver
)
1062 TAILQ_FOREACH(mod
, &found_modules
, link
) {
1063 if (strcmp(mod
->name
, name
) == 0 && (ver
== 0 || mod
->version
== ver
))
1070 modlist_lookup2(const char *name
, struct mod_depend
*verinfo
)
1072 modlist_t mod
, bestmod
;
1075 if (verinfo
== NULL
)
1076 return (modlist_lookup(name
, 0));
1078 TAILQ_FOREACH(mod
, &found_modules
, link
) {
1079 if (strcmp(mod
->name
, name
) != 0)
1082 if (ver
== verinfo
->md_ver_preferred
)
1084 if (ver
>= verinfo
->md_ver_minimum
&&
1085 ver
<= verinfo
->md_ver_maximum
&&
1086 (bestmod
== NULL
|| ver
> bestmod
->version
))
1093 linker_reference_module(const char *modname
, struct mod_depend
*verinfo
,
1094 linker_file_t
*result
)
1099 lockmgr(&lock
, LK_SHARED
);
1100 if ((mod
= modlist_lookup2(modname
, verinfo
)) != NULL
) {
1101 *result
= mod
->container
;
1103 lockmgr(&lock
, LK_RELEASE
);
1107 lockmgr(&lock
, LK_RELEASE
);
1109 error
= linker_load_module(NULL
, modname
, NULL
, verinfo
, result
);
1115 linker_release_module(const char *modname
, struct mod_depend
*verinfo
,
1121 lockmgr(&lock
, LK_SHARED
);
1123 KASSERT(modname
!= NULL
,
1124 ("linker_release_module: no file or name"));
1125 mod
= modlist_lookup2(modname
, verinfo
);
1127 lockmgr(&lock
, LK_RELEASE
);
1130 lf
= mod
->container
;
1132 KASSERT(modname
== NULL
&& verinfo
== NULL
,
1133 ("linker_release_module: both file and name"));
1134 lockmgr(&lock
, LK_RELEASE
);
1136 error
= linker_file_unload(lf
);
1142 modlist_newmodule(const char *modname
, int version
, linker_file_t container
)
1146 mod
= kmalloc(sizeof(struct modlist
), M_LINKER
, M_NOWAIT
| M_ZERO
);
1148 panic("no memory for module list");
1149 mod
->container
= container
;
1150 mod
->name
= modname
;
1151 mod
->version
= version
;
1152 TAILQ_INSERT_TAIL(&found_modules
, mod
, link
);
1157 linker_addmodules(linker_file_t lf
, struct mod_metadata
**start
,
1158 struct mod_metadata
**stop
, int preload
)
1160 struct mod_metadata
*mp
, **mdp
;
1161 const char *modname
;
1164 for (mdp
= start
; mdp
< stop
; mdp
++) {
1166 if (mp
->md_type
!= MDT_VERSION
)
1168 modname
= mp
->md_cval
;
1169 ver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1170 if (modlist_lookup(modname
, ver
) != NULL
) {
1171 kprintf("module %s already present!\n", modname
);
1172 /* XXX what can we do? this is a build error. :-( */
1175 modlist_newmodule(modname
, ver
, lf
);
1180 linker_preload(void* arg
)
1183 const char *modname
, *nmodname
;
1185 linker_file_t lf
, nlf
;
1188 linker_file_list_t loaded_files
;
1189 linker_file_list_t depended_files
;
1190 struct mod_metadata
*mp
, *nmp
;
1191 struct mod_metadata
**start
, **stop
, **mdp
, **nmdp
;
1192 struct mod_depend
*verinfo
;
1196 struct sysinit
**si_start
, **si_stop
;
1198 TAILQ_INIT(&loaded_files
);
1199 TAILQ_INIT(&depended_files
);
1200 TAILQ_INIT(&found_modules
);
1203 while ((modptr
= preload_search_next_name(modptr
)) != NULL
) {
1204 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
1205 modtype
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
1206 if (modname
== NULL
) {
1207 kprintf("Preloaded module at %p does not have a name!\n", modptr
);
1210 if (modtype
== NULL
) {
1211 kprintf("Preloaded module at %p does not have a type!\n", modptr
);
1216 kprintf("Preloaded %s \"%s\" at %p.\n", modtype
, modname
, modptr
);
1218 TAILQ_FOREACH(lc
, &classes
, link
) {
1219 error
= lc
->ops
->preload_file(modname
, &lf
);
1225 TAILQ_INSERT_TAIL(&loaded_files
, lf
, loaded
);
1229 * First get a list of stuff in the kernel.
1231 if (linker_file_lookup_set(linker_kernel_file
, MDT_SETNAME
, &start
,
1233 linker_addmodules(linker_kernel_file
, start
, stop
, 1);
1236 * This is a once-off kinky bubble sort to resolve relocation
1237 * dependency requirements.
1240 TAILQ_FOREACH(lf
, &loaded_files
, loaded
) {
1241 error
= linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, NULL
);
1243 * First, look to see if we would successfully link with this
1246 resolves
= 1; /* unless we know otherwise */
1248 for (mdp
= start
; mdp
< stop
; mdp
++) {
1250 if (mp
->md_type
!= MDT_DEPEND
)
1252 modname
= mp
->md_cval
;
1253 verinfo
= mp
->md_data
;
1254 for (nmdp
= start
; nmdp
< stop
; nmdp
++) {
1256 if (nmp
->md_type
!= MDT_VERSION
)
1258 nmodname
= nmp
->md_cval
;
1259 if (strcmp(modname
, nmodname
) == 0)
1262 if (nmdp
< stop
)/* it's a self reference */
1266 * ok, the module isn't here yet, we
1269 if (modlist_lookup2(modname
, verinfo
) == NULL
)
1274 * OK, if we found our modules, we can link. So, "provide"
1275 * the modules inside and add it to the end of the link order
1280 for (mdp
= start
; mdp
< stop
; mdp
++) {
1282 if (mp
->md_type
!= MDT_VERSION
)
1284 modname
= mp
->md_cval
;
1285 nver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1286 if (modlist_lookup(modname
, nver
) != NULL
) {
1287 kprintf("module %s already present!\n", modname
);
1288 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1289 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */ );
1290 /* we changed tailq next ptr */
1293 modlist_newmodule(modname
, nver
, lf
);
1296 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1297 TAILQ_INSERT_TAIL(&depended_files
, lf
, loaded
);
1299 * Since we provided modules, we need to restart the
1300 * sort so that the previous files that depend on us
1301 * have a chance. Also, we've busted the tailq next
1302 * pointer with the REMOVE.
1309 * At this point, we check to see what could not be resolved..
1311 while ((lf
= TAILQ_FIRST(&loaded_files
)) != NULL
) {
1312 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1313 kprintf("KLD file %s is missing dependencies\n", lf
->filename
);
1314 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */ );
1318 * We made it. Finish off the linking in the order we determined.
1320 TAILQ_FOREACH_MUTABLE(lf
, &depended_files
, loaded
, nlf
) {
1321 if (linker_kernel_file
) {
1322 linker_kernel_file
->refs
++;
1323 linker_file_add_dependancy(lf
, linker_kernel_file
);
1327 error
= linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, NULL
);
1329 for (mdp
= start
; mdp
< stop
; mdp
++) {
1331 if (mp
->md_type
!= MDT_DEPEND
)
1333 modname
= mp
->md_cval
;
1334 verinfo
= mp
->md_data
;
1335 mod
= modlist_lookup2(modname
, verinfo
);
1336 /* Don't count self-dependencies */
1337 if (lf
== mod
->container
)
1339 mod
->container
->refs
++;
1340 linker_file_add_dependancy(lf
, mod
->container
);
1344 * Now do relocation etc using the symbol search paths
1345 * established by the dependencies
1347 error
= lf
->ops
->preload_finish(lf
);
1349 TAILQ_REMOVE(&depended_files
, lf
, loaded
);
1350 kprintf("KLD file %s - could not finalize loading\n",
1352 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */);
1355 linker_file_register_modules(lf
);
1356 if (linker_file_lookup_set(lf
, "sysinit_set", &si_start
, &si_stop
, NULL
) == 0)
1357 sysinit_add(si_start
, si_stop
);
1358 linker_file_register_sysctls(lf
);
1359 lf
->flags
|= LINKER_FILE_LINKED
;
1361 /* woohoo! we made it! */
1364 SYSINIT(preload
, SI_BOOT2_KLD
, SI_ORDER_MIDDLE
, linker_preload
, 0);
1367 * Search for a not-loaded module by name.
1369 * Modules may be found in the following locations:
1371 * - preloaded (result is just the module name)
1372 * - on disk (result is full path to module)
1374 * If the module name is qualified in any way (contains path, etc.)
1375 * the we simply return a copy of it.
1377 * The search path can be manipulated via sysctl. Note that we use the ';'
1378 * character as a separator to be consistent with the bootloader.
1381 static char linker_path
[MAXPATHLEN
] = "/boot;/boot/modules;/;/modules";
1383 SYSCTL_STRING(_kern
, OID_AUTO
, module_path
, CTLFLAG_RW
, linker_path
,
1384 sizeof(linker_path
), "module load search path");
1385 TUNABLE_STR("module_path", linker_path
, sizeof(linker_path
));
1388 linker_search_path(const char *name
)
1390 struct nlookupdata nd
;
1391 char *cp
, *ep
, *result
;
1392 size_t name_len
, prefix_len
;
1397 const char *exts
[] = { "", ".ko", NULL
};
1400 /* qualified at all? */
1401 if (index(name
, '/'))
1402 return(linker_strdup(name
));
1404 /* traverse the linker path */
1406 name_len
= strlen(name
);
1409 /* find the end of this component */
1410 for (ep
= cp
; (*ep
!= 0) && (*ep
!= ';'); ep
++)
1412 prefix_len
= ep
- cp
;
1413 /* if this component doesn't end with a slash, add one */
1414 if (ep
== cp
|| *(ep
- 1) != '/')
1420 * +2+3 : possible separator, plus terminator + possible extension.
1422 result
= kmalloc(prefix_len
+ name_len
+ 2+3, M_LINKER
, M_WAITOK
);
1424 strncpy(result
, cp
, prefix_len
);
1426 result
[prefix_len
++] = '/';
1427 strcpy(result
+ prefix_len
, name
);
1429 result_len
= strlen(result
);
1430 for (ext
= exts
; *ext
!= NULL
; ext
++) {
1431 strcpy(result
+ result_len
, *ext
);
1434 * Attempt to open the file, and return the path if we succeed and it's
1437 error
= nlookup_init(&nd
, result
, UIO_SYSSPACE
, NLC_FOLLOW
|NLC_LOCKVP
);
1439 error
= vn_open(&nd
, NULL
, FREAD
, 0);
1441 type
= nd
.nl_open_vp
->v_type
;
1450 kfree(result
, M_LINKER
);
1460 * Find a file which contains given module and load it, if "parent" is not
1461 * NULL, register a reference to it.
1464 linker_load_module(const char *kldname
, const char *modname
,
1465 struct linker_file
*parent
, struct mod_depend
*verinfo
,
1466 struct linker_file
**lfpp
)
1468 linker_file_t lfdep
;
1469 const char *filename
;
1473 if (modname
== NULL
) {
1475 * We have to load KLD
1477 KASSERT(verinfo
== NULL
, ("linker_load_module: verinfo is not NULL"));
1478 pathname
= linker_search_path(kldname
);
1480 if (modlist_lookup2(modname
, verinfo
) != NULL
)
1482 if (kldname
!= NULL
)
1484 pathname
= linker_strdup(kldname
);
1486 else if (rootvnode
== NULL
)
1490 pathname
= linker_search_path(modname
);
1494 * Need to find a KLD with required module
1496 pathname
= linker_search_module(modname
,
1497 strlen(modname
), verinfo
);
1500 if (pathname
== NULL
)
1504 * Can't load more than one file with the same basename XXX:
1505 * Actually it should be possible to have multiple KLDs with
1506 * the same basename but different path because they can
1507 * provide different versions of the same modules.
1509 filename
= rindex(pathname
, '/');
1510 if (filename
== NULL
)
1511 filename
= pathname
;
1514 if (linker_find_file_by_name(filename
))
1518 error
= linker_load_file(pathname
, &lfdep
);
1521 if (modname
&& verinfo
&& modlist_lookup2(modname
, verinfo
) == NULL
) {
1522 linker_file_unload(lfdep
/* , LINKER_UNLOAD_FORCE */ );
1527 linker_file_add_dependancy(parent
, lfdep
);
1532 kfree(pathname
, M_LINKER
);
1537 * This routine is responsible for finding dependencies of userland initiated
1538 * kldload(2)'s of files.
1541 linker_load_dependencies(linker_file_t lf
)
1543 linker_file_t lfdep
;
1544 struct mod_metadata
**start
, **stop
, **mdp
, **nmdp
;
1545 struct mod_metadata
*mp
, *nmp
;
1546 struct mod_depend
*verinfo
;
1548 const char *modname
, *nmodname
;
1549 int ver
, error
= 0, count
;
1552 * All files are dependant on /kernel.
1554 if (linker_kernel_file
) {
1555 linker_kernel_file
->refs
++;
1556 linker_file_add_dependancy(lf
, linker_kernel_file
);
1558 if (linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, &count
) != 0)
1560 for (mdp
= start
; mdp
< stop
; mdp
++) {
1562 if (mp
->md_type
!= MDT_VERSION
)
1564 modname
= mp
->md_cval
;
1565 ver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1566 mod
= modlist_lookup(modname
, ver
);
1568 kprintf("interface %s.%d already present in the KLD '%s'!\n",
1569 modname
, ver
, mod
->container
->filename
);
1574 for (mdp
= start
; mdp
< stop
; mdp
++) {
1576 if (mp
->md_type
!= MDT_DEPEND
)
1578 modname
= mp
->md_cval
;
1579 verinfo
= mp
->md_data
;
1581 for (nmdp
= start
; nmdp
< stop
; nmdp
++) {
1583 if (nmp
->md_type
!= MDT_VERSION
)
1585 nmodname
= nmp
->md_cval
;
1586 if (strcmp(modname
, nmodname
) == 0)
1589 if (nmdp
< stop
) /* early exit, it's a self reference */
1591 mod
= modlist_lookup2(modname
, verinfo
);
1592 if (mod
) { /* woohoo, it's loaded already */
1593 lfdep
= mod
->container
;
1595 linker_file_add_dependancy(lf
, lfdep
);
1598 error
= linker_load_module(NULL
, modname
, lf
, verinfo
, NULL
);
1600 kprintf("KLD %s: depends on %s - not available or version mismatch\n",
1601 lf
->filename
, modname
);
1608 linker_addmodules(lf
, start
, stop
, 0);