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 #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 llf_lock
; /* lock for the file list */
67 static struct lock kld_lock
;
68 static linker_class_list_t classes
;
69 static linker_file_list_t linker_files
;
70 static int next_file_id
= 1;
72 /* XXX wrong name; we're looking at version provision tags here, not modules */
73 typedef TAILQ_HEAD(, modlist
) modlisthead_t
;
75 TAILQ_ENTRY(modlist
) link
; /* chain together all modules */
76 linker_file_t container
;
80 typedef struct modlist
*modlist_t
;
81 static modlisthead_t found_modules
;
84 static int linker_load_module(const char *kldname
, const char *modname
,
85 struct linker_file
*parent
, struct mod_depend
*verinfo
,
86 struct linker_file
**lfpp
);
89 linker_strdup(const char *str
)
93 result
= kmalloc(strlen(str
) + 1, M_LINKER
, M_WAITOK
);
99 linker_init(void* arg
)
101 lockinit(&llf_lock
, "klink", 0, 0);
102 lockinit(&kld_lock
, "kldlk", 0, LK_CANRECURSE
);
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(&llf_lock
, LK_SHARED
);
386 TAILQ_FOREACH(lf
, &linker_files
, link
) {
387 if (!strcmp(lf
->filename
, koname
))
389 if (!strcmp(lf
->filename
, filename
))
392 lockmgr(&llf_lock
, LK_RELEASE
);
395 kfree(koname
, M_LINKER
);
400 linker_find_file_by_id(int fileid
)
402 linker_file_t lf
= NULL
;
404 lockmgr(&llf_lock
, LK_SHARED
);
405 TAILQ_FOREACH(lf
, &linker_files
, link
) {
406 if (lf
->id
== fileid
)
409 lockmgr(&llf_lock
, LK_RELEASE
);
415 linker_file_foreach(linker_predicate_t
*predicate
, void *context
)
420 lockmgr(&llf_lock
, LK_SHARED
);
421 TAILQ_FOREACH(lf
, &linker_files
, link
) {
422 retval
= predicate(lf
, context
);
426 lockmgr(&llf_lock
, LK_RELEASE
);
432 linker_make_file(const char* pathname
, void* priv
, struct linker_file_ops
* ops
)
434 linker_file_t lf
= NULL
;
435 const char *filename
;
437 filename
= rindex(pathname
, '/');
438 if (filename
&& filename
[1])
443 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename
));
444 lockmgr(&llf_lock
, LK_EXCLUSIVE
);
445 lf
= kmalloc(sizeof(struct linker_file
), M_LINKER
, M_WAITOK
| M_ZERO
);
449 lf
->filename
= linker_strdup(filename
);
450 lf
->id
= next_file_id
++;
453 STAILQ_INIT(&lf
->common
);
454 TAILQ_INIT(&lf
->modules
);
458 TAILQ_INSERT_TAIL(&linker_files
, lf
, link
);
460 lockmgr(&llf_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(&llf_lock
, LK_EXCLUSIVE
);
482 /* Easy case of just dropping a reference. */
483 if (file
->refs
> 1) {
485 lockmgr(&llf_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(&llf_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(&llf_lock
, LK_RELEASE
);
524 linker_file_sysuninit(file
);
525 linker_file_unregister_sysctls(file
);
526 lockmgr(&llf_lock
, LK_EXCLUSIVE
);
529 TAILQ_REMOVE(&linker_files
, file
, link
);
532 lockmgr(&llf_lock
, LK_RELEASE
);
533 for (i
= 0; i
< file
->ndeps
; i
++)
534 linker_file_unload(file
->deps
[i
]);
535 lockmgr(&llf_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(&llf_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
== NULL
&& 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
!= NULL
&& 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
, '.')) {
800 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
801 error
= linker_load_module(kldname
, modname
, NULL
, NULL
, &lf
);
802 lockmgr(&kld_lock
, LK_RELEASE
);
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)
831 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
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
);
848 lockmgr(&kld_lock
, LK_RELEASE
);
857 sys_kldfind(struct kldfind_args
*uap
)
859 char *filename
= NULL
, *modulename
;
863 uap
->sysmsg_result
= -1;
865 filename
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
866 if ((error
= copyinstr(uap
->file
, filename
, MAXPATHLEN
, NULL
)) != 0)
869 modulename
= rindex(filename
, '/');
870 if (modulename
== NULL
)
871 modulename
= filename
;
873 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
874 lf
= linker_find_file_by_name(modulename
);
876 uap
->sysmsg_result
= lf
->id
;
879 lockmgr(&kld_lock
, LK_RELEASE
);
883 kfree(filename
, M_TEMP
);
891 sys_kldnext(struct kldnext_args
*uap
)
896 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
897 if (uap
->fileid
== 0) {
898 lf
= TAILQ_FIRST(&linker_files
);
900 lf
= linker_find_file_by_id(uap
->fileid
);
905 lf
= TAILQ_NEXT(lf
, link
);
908 /* Skip partially loaded files. */
909 while (lf
!= NULL
&& !(lf
->flags
& LINKER_FILE_LINKED
)) {
910 lf
= TAILQ_NEXT(lf
, link
);
914 uap
->sysmsg_result
= lf
->id
;
916 uap
->sysmsg_result
= 0;
919 lockmgr(&kld_lock
, LK_RELEASE
);
928 sys_kldstat(struct kldstat_args
*uap
)
933 struct kld_file_stat
* stat
;
936 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
937 lf
= linker_find_file_by_id(uap
->fileid
);
946 * Check the version of the user's structure.
948 if ((error
= copyin(&stat
->version
, &version
, sizeof(version
))) != 0)
950 if (version
!= sizeof(struct kld_file_stat
)) {
955 namelen
= strlen(lf
->filename
) + 1;
956 if (namelen
> MAXPATHLEN
)
957 namelen
= MAXPATHLEN
;
958 if ((error
= copyout(lf
->filename
, &stat
->name
[0], namelen
)) != 0)
960 if ((error
= copyout(&lf
->refs
, &stat
->refs
, sizeof(int))) != 0)
962 if ((error
= copyout(&lf
->id
, &stat
->id
, sizeof(int))) != 0)
964 if ((error
= copyout(&lf
->address
, &stat
->address
, sizeof(caddr_t
))) != 0)
966 if ((error
= copyout(&lf
->size
, &stat
->size
, sizeof(size_t))) != 0)
969 uap
->sysmsg_result
= 0;
972 lockmgr(&kld_lock
, LK_RELEASE
);
981 sys_kldfirstmod(struct kldfirstmod_args
*uap
)
986 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
987 lf
= linker_find_file_by_id(uap
->fileid
);
989 if (TAILQ_FIRST(&lf
->modules
))
990 uap
->sysmsg_result
= module_getid(TAILQ_FIRST(&lf
->modules
));
992 uap
->sysmsg_result
= 0;
996 lockmgr(&kld_lock
, LK_RELEASE
);
1005 sys_kldsym(struct kldsym_args
*uap
)
1007 char *symstr
= NULL
;
1009 linker_symval_t symval
;
1011 struct kld_sym_lookup lookup
;
1014 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
1015 if ((error
= copyin(uap
->data
, &lookup
, sizeof(lookup
))) != 0)
1017 if (lookup
.version
!= sizeof(lookup
) || uap
->cmd
!= KLDSYM_LOOKUP
) {
1022 symstr
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
1023 if ((error
= copyinstr(lookup
.symname
, symstr
, MAXPATHLEN
, NULL
)) != 0)
1026 if (uap
->fileid
!= 0) {
1027 lf
= linker_find_file_by_id(uap
->fileid
);
1032 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
1033 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
1034 lookup
.symvalue
= (uintptr_t)symval
.value
;
1035 lookup
.symsize
= symval
.size
;
1036 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
1040 TAILQ_FOREACH(lf
, &linker_files
, link
) {
1041 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
1042 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
1043 lookup
.symvalue
= (uintptr_t)symval
.value
;
1044 lookup
.symsize
= symval
.size
;
1045 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
1053 lockmgr(&kld_lock
, LK_RELEASE
);
1055 kfree(symstr
, M_TEMP
);
1061 * Preloaded module support
1065 modlist_lookup(const char *name
, int ver
)
1069 TAILQ_FOREACH(mod
, &found_modules
, link
) {
1070 if (strcmp(mod
->name
, name
) == 0 && (ver
== 0 || mod
->version
== ver
))
1077 modlist_lookup2(const char *name
, struct mod_depend
*verinfo
)
1079 modlist_t mod
, bestmod
;
1082 if (verinfo
== NULL
)
1083 return (modlist_lookup(name
, 0));
1085 TAILQ_FOREACH(mod
, &found_modules
, link
) {
1086 if (strcmp(mod
->name
, name
) != 0)
1089 if (ver
== verinfo
->md_ver_preferred
)
1091 if (ver
>= verinfo
->md_ver_minimum
&&
1092 ver
<= verinfo
->md_ver_maximum
&&
1093 (bestmod
== NULL
|| ver
> bestmod
->version
))
1100 linker_reference_module(const char *modname
, struct mod_depend
*verinfo
,
1101 linker_file_t
*result
)
1106 lockmgr(&llf_lock
, LK_SHARED
);
1107 if ((mod
= modlist_lookup2(modname
, verinfo
)) != NULL
) {
1108 *result
= mod
->container
;
1110 lockmgr(&llf_lock
, LK_RELEASE
);
1114 lockmgr(&llf_lock
, LK_RELEASE
);
1115 /*lockmgr(&kld_lock, LK_EXCLUSIVE);*/
1116 error
= linker_load_module(NULL
, modname
, NULL
, verinfo
, result
);
1117 /*lockmgr(&kld_lock, LK_RELEASE);*/
1123 linker_release_module(const char *modname
, struct mod_depend
*verinfo
,
1129 lockmgr(&llf_lock
, LK_SHARED
);
1131 KASSERT(modname
!= NULL
,
1132 ("linker_release_module: no file or name"));
1133 mod
= modlist_lookup2(modname
, verinfo
);
1135 lockmgr(&llf_lock
, LK_RELEASE
);
1138 lf
= mod
->container
;
1140 KASSERT(modname
== NULL
&& verinfo
== NULL
,
1141 ("linker_release_module: both file and name"));
1142 lockmgr(&llf_lock
, LK_RELEASE
);
1143 /*lockmgr(&kld_lock, LK_EXCLUSIVE);*/
1144 error
= linker_file_unload(lf
);
1145 /*lockmgr(&kld_lock, LK_RELEASE);*/
1151 modlist_newmodule(const char *modname
, int version
, linker_file_t container
)
1155 mod
= kmalloc(sizeof(struct modlist
), M_LINKER
, M_NOWAIT
| M_ZERO
);
1157 panic("no memory for module list");
1158 mod
->container
= container
;
1159 mod
->name
= modname
;
1160 mod
->version
= version
;
1161 TAILQ_INSERT_TAIL(&found_modules
, mod
, link
);
1166 linker_addmodules(linker_file_t lf
, struct mod_metadata
**start
,
1167 struct mod_metadata
**stop
, int preload
)
1169 struct mod_metadata
*mp
, **mdp
;
1170 const char *modname
;
1173 for (mdp
= start
; mdp
< stop
; mdp
++) {
1175 if (mp
->md_type
!= MDT_VERSION
)
1177 modname
= mp
->md_cval
;
1178 ver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1179 if (modlist_lookup(modname
, ver
) != NULL
) {
1180 kprintf("module %s already present!\n", modname
);
1181 /* XXX what can we do? this is a build error. :-( */
1184 modlist_newmodule(modname
, ver
, lf
);
1189 linker_preload(void* arg
)
1192 const char *modname
, *nmodname
;
1194 linker_file_t lf
, nlf
;
1197 linker_file_list_t loaded_files
;
1198 linker_file_list_t depended_files
;
1199 struct mod_metadata
*mp
, *nmp
;
1200 struct mod_metadata
**start
, **stop
, **mdp
, **nmdp
;
1201 struct mod_depend
*verinfo
;
1205 struct sysinit
**si_start
, **si_stop
;
1207 TAILQ_INIT(&loaded_files
);
1208 TAILQ_INIT(&depended_files
);
1209 TAILQ_INIT(&found_modules
);
1212 while ((modptr
= preload_search_next_name(modptr
)) != NULL
) {
1213 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
1214 modtype
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
1215 if (modname
== NULL
) {
1216 kprintf("Preloaded module at %p does not have a name!\n", modptr
);
1219 if (modtype
== NULL
) {
1220 kprintf("Preloaded module at %p does not have a type!\n", modptr
);
1225 kprintf("Preloaded %s \"%s\" at %p.\n", modtype
, modname
, modptr
);
1227 TAILQ_FOREACH(lc
, &classes
, link
) {
1228 error
= lc
->ops
->preload_file(modname
, &lf
);
1234 TAILQ_INSERT_TAIL(&loaded_files
, lf
, loaded
);
1238 * First get a list of stuff in the kernel.
1240 if (linker_file_lookup_set(linker_kernel_file
, MDT_SETNAME
, &start
,
1242 linker_addmodules(linker_kernel_file
, start
, stop
, 1);
1245 * This is a once-off kinky bubble sort to resolve relocation
1246 * dependency requirements.
1249 TAILQ_FOREACH(lf
, &loaded_files
, loaded
) {
1250 error
= linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, NULL
);
1252 * First, look to see if we would successfully link with this
1255 resolves
= 1; /* unless we know otherwise */
1257 for (mdp
= start
; mdp
< stop
; mdp
++) {
1259 if (mp
->md_type
!= MDT_DEPEND
)
1261 modname
= mp
->md_cval
;
1262 verinfo
= mp
->md_data
;
1263 for (nmdp
= start
; nmdp
< stop
; nmdp
++) {
1265 if (nmp
->md_type
!= MDT_VERSION
)
1267 nmodname
= nmp
->md_cval
;
1268 if (strcmp(modname
, nmodname
) == 0)
1271 if (nmdp
< stop
)/* it's a self reference */
1275 * ok, the module isn't here yet, we
1278 if (modlist_lookup2(modname
, verinfo
) == NULL
)
1283 * OK, if we found our modules, we can link. So, "provide"
1284 * the modules inside and add it to the end of the link order
1289 for (mdp
= start
; mdp
< stop
; mdp
++) {
1291 if (mp
->md_type
!= MDT_VERSION
)
1293 modname
= mp
->md_cval
;
1294 nver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1295 if (modlist_lookup(modname
, nver
) != NULL
) {
1296 kprintf("module %s already present!\n", modname
);
1297 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1298 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */ );
1299 /* we changed tailq next ptr */
1302 modlist_newmodule(modname
, nver
, lf
);
1305 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1306 TAILQ_INSERT_TAIL(&depended_files
, lf
, loaded
);
1308 * Since we provided modules, we need to restart the
1309 * sort so that the previous files that depend on us
1310 * have a chance. Also, we've busted the tailq next
1311 * pointer with the REMOVE.
1318 * At this point, we check to see what could not be resolved..
1320 while ((lf
= TAILQ_FIRST(&loaded_files
)) != NULL
) {
1321 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1322 kprintf("KLD file %s is missing dependencies\n", lf
->filename
);
1323 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */ );
1327 * We made it. Finish off the linking in the order we determined.
1329 TAILQ_FOREACH_MUTABLE(lf
, &depended_files
, loaded
, nlf
) {
1330 if (linker_kernel_file
) {
1331 linker_kernel_file
->refs
++;
1332 linker_file_add_dependancy(lf
, linker_kernel_file
);
1336 error
= linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, NULL
);
1338 for (mdp
= start
; mdp
< stop
; mdp
++) {
1340 if (mp
->md_type
!= MDT_DEPEND
)
1342 modname
= mp
->md_cval
;
1343 verinfo
= mp
->md_data
;
1344 mod
= modlist_lookup2(modname
, verinfo
);
1345 /* Don't count self-dependencies */
1346 if (lf
== mod
->container
)
1348 mod
->container
->refs
++;
1349 linker_file_add_dependancy(lf
, mod
->container
);
1353 * Now do relocation etc using the symbol search paths
1354 * established by the dependencies
1356 error
= lf
->ops
->preload_finish(lf
);
1358 TAILQ_REMOVE(&depended_files
, lf
, loaded
);
1359 kprintf("KLD file %s - could not finalize loading\n",
1361 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */);
1364 linker_file_register_modules(lf
);
1365 if (linker_file_lookup_set(lf
, "sysinit_set", &si_start
, &si_stop
, NULL
) == 0)
1366 sysinit_add(si_start
, si_stop
);
1367 linker_file_register_sysctls(lf
);
1368 lf
->flags
|= LINKER_FILE_LINKED
;
1370 /* woohoo! we made it! */
1373 SYSINIT(preload
, SI_BOOT2_KLD
, SI_ORDER_MIDDLE
, linker_preload
, 0);
1376 * Search for a not-loaded module by name.
1378 * Modules may be found in the following locations:
1380 * - preloaded (result is just the module name)
1381 * - on disk (result is full path to module)
1383 * If the module name is qualified in any way (contains path, etc.)
1384 * the we simply return a copy of it.
1386 * The search path can be manipulated via sysctl. Note that we use the ';'
1387 * character as a separator to be consistent with the bootloader.
1390 static char linker_path
[MAXPATHLEN
] = "/boot;/boot/modules;/;/modules";
1392 SYSCTL_STRING(_kern
, OID_AUTO
, module_path
, CTLFLAG_RW
, linker_path
,
1393 sizeof(linker_path
), "module load search path");
1394 TUNABLE_STR("module_path", linker_path
, sizeof(linker_path
));
1397 linker_search_path(const char *name
)
1399 struct nlookupdata nd
;
1400 char *cp
, *ep
, *result
;
1401 size_t name_len
, prefix_len
;
1406 const char *exts
[] = { "", ".ko", NULL
};
1409 /* qualified at all? */
1410 if (index(name
, '/'))
1411 return(linker_strdup(name
));
1413 /* traverse the linker path */
1415 name_len
= strlen(name
);
1418 /* find the end of this component */
1419 for (ep
= cp
; (*ep
!= 0) && (*ep
!= ';'); ep
++)
1421 prefix_len
= ep
- cp
;
1422 /* if this component doesn't end with a slash, add one */
1423 if (ep
== cp
|| *(ep
- 1) != '/')
1429 * +2+3 : possible separator, plus terminator + possible extension.
1431 result
= kmalloc(prefix_len
+ name_len
+ 2+3, M_LINKER
, M_WAITOK
);
1433 strncpy(result
, cp
, prefix_len
);
1435 result
[prefix_len
++] = '/';
1436 strcpy(result
+ prefix_len
, name
);
1438 result_len
= strlen(result
);
1439 for (ext
= exts
; *ext
!= NULL
; ext
++) {
1440 strcpy(result
+ result_len
, *ext
);
1443 * Attempt to open the file, and return the path if we succeed and it's
1446 error
= nlookup_init(&nd
, result
, UIO_SYSSPACE
, NLC_FOLLOW
|NLC_LOCKVP
);
1448 error
= vn_open(&nd
, NULL
, FREAD
, 0);
1450 type
= nd
.nl_open_vp
->v_type
;
1459 kfree(result
, M_LINKER
);
1469 * Find a file which contains given module and load it, if "parent" is not
1470 * NULL, register a reference to it.
1473 linker_load_module(const char *kldname
, const char *modname
,
1474 struct linker_file
*parent
, struct mod_depend
*verinfo
,
1475 struct linker_file
**lfpp
)
1477 linker_file_t lfdep
;
1478 const char *filename
;
1482 if (modname
== NULL
) {
1484 * We have to load KLD
1486 KASSERT(verinfo
== NULL
, ("linker_load_module: verinfo is not NULL"));
1487 pathname
= linker_search_path(kldname
);
1489 if (modlist_lookup2(modname
, verinfo
) != NULL
)
1491 if (kldname
!= NULL
)
1493 pathname
= linker_strdup(kldname
);
1495 else if (rootvnode
== NULL
)
1499 pathname
= linker_search_path(modname
);
1503 * Need to find a KLD with required module
1505 pathname
= linker_search_module(modname
,
1506 strlen(modname
), verinfo
);
1509 if (pathname
== NULL
)
1513 * Can't load more than one file with the same basename XXX:
1514 * Actually it should be possible to have multiple KLDs with
1515 * the same basename but different path because they can
1516 * provide different versions of the same modules.
1518 filename
= rindex(pathname
, '/');
1519 if (filename
== NULL
)
1520 filename
= pathname
;
1523 if (linker_find_file_by_name(filename
))
1527 error
= linker_load_file(pathname
, &lfdep
);
1530 if (modname
&& verinfo
&& modlist_lookup2(modname
, verinfo
) == NULL
) {
1531 linker_file_unload(lfdep
/* , LINKER_UNLOAD_FORCE */ );
1536 linker_file_add_dependancy(parent
, lfdep
);
1541 kfree(pathname
, M_LINKER
);
1546 * This routine is responsible for finding dependencies of userland initiated
1547 * kldload(2)'s of files.
1550 linker_load_dependencies(linker_file_t lf
)
1552 linker_file_t lfdep
;
1553 struct mod_metadata
**start
, **stop
, **mdp
, **nmdp
;
1554 struct mod_metadata
*mp
, *nmp
;
1555 struct mod_depend
*verinfo
;
1557 const char *modname
, *nmodname
;
1558 int ver
, error
= 0, count
;
1561 * All files are dependant on /kernel.
1563 if (linker_kernel_file
) {
1564 linker_kernel_file
->refs
++;
1565 linker_file_add_dependancy(lf
, linker_kernel_file
);
1567 if (linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, &count
) != 0)
1569 for (mdp
= start
; mdp
< stop
; mdp
++) {
1571 if (mp
->md_type
!= MDT_VERSION
)
1573 modname
= mp
->md_cval
;
1574 ver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1575 mod
= modlist_lookup(modname
, ver
);
1577 kprintf("interface %s.%d already present in the KLD '%s'!\n",
1578 modname
, ver
, mod
->container
->filename
);
1583 for (mdp
= start
; mdp
< stop
; mdp
++) {
1585 if (mp
->md_type
!= MDT_DEPEND
)
1587 modname
= mp
->md_cval
;
1588 verinfo
= mp
->md_data
;
1590 for (nmdp
= start
; nmdp
< stop
; nmdp
++) {
1592 if (nmp
->md_type
!= MDT_VERSION
)
1594 nmodname
= nmp
->md_cval
;
1595 if (strcmp(modname
, nmodname
) == 0)
1598 if (nmdp
< stop
) /* early exit, it's a self reference */
1600 mod
= modlist_lookup2(modname
, verinfo
);
1601 if (mod
) { /* woohoo, it's loaded already */
1602 lfdep
= mod
->container
;
1604 linker_file_add_dependancy(lf
, lfdep
);
1607 error
= linker_load_module(NULL
, modname
, lf
, verinfo
, NULL
);
1609 kprintf("KLD %s: depends on %s - not available or version mismatch\n",
1610 lf
->filename
, modname
);
1617 linker_addmodules(lf
, start
, stop
, 0);