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/sysmsg.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_basename(const char *path
)
101 const char *filename
;
103 filename
= strrchr(path
, '/');
104 if (filename
== NULL
)
112 linker_init(void* arg
)
114 lockinit(&llf_lock
, "klink", 0, 0);
115 lockinit(&kld_lock
, "kldlk", 0, LK_CANRECURSE
);
116 TAILQ_INIT(&classes
);
117 TAILQ_INIT(&linker_files
);
120 SYSINIT(linker
, SI_BOOT2_KLD
, SI_ORDER_FIRST
, linker_init
, 0);
123 linker_add_class(const char* desc
, void* priv
,
124 struct linker_class_ops
* ops
)
128 lc
= kmalloc(sizeof(struct linker_class
), M_LINKER
, M_NOWAIT
| M_ZERO
);
135 TAILQ_INSERT_HEAD(&classes
, lc
, link
);
141 linker_file_sysinit(linker_file_t lf
)
143 struct sysinit
** start
, ** stop
;
144 struct sysinit
** sipp
;
145 struct sysinit
** xipp
;
146 struct sysinit
* save
;
148 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
151 if (linker_file_lookup_set(lf
, "sysinit_set", &start
, &stop
, NULL
) != 0)
155 * Perform a bubble sort of the system initialization objects by
156 * their subsystem (primary key) and order (secondary key).
158 * Since some things care about execution order, this is the
159 * operation which ensures continued function.
161 for (sipp
= start
; sipp
< stop
; sipp
++) {
162 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
163 if ((*sipp
)->subsystem
< (*xipp
)->subsystem
||
164 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
165 (*sipp
)->order
<= (*xipp
)->order
))
175 * Traverse the (now) ordered list of system initialization tasks.
176 * Perform each task, and continue on to the next task.
178 for (sipp
= start
; sipp
< stop
; sipp
++) {
179 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
180 continue; /* skip dummy task(s)*/
183 (*((*sipp
)->func
))((*sipp
)->udata
);
188 linker_file_sysuninit(linker_file_t lf
)
190 struct sysinit
** start
, ** stop
;
191 struct sysinit
** sipp
;
192 struct sysinit
** xipp
;
193 struct sysinit
* save
;
195 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
198 if (linker_file_lookup_set(lf
, "sysuninit_set", &start
, &stop
, NULL
) != 0)
202 * Perform a reverse bubble sort of the system initialization objects
203 * by their subsystem (primary key) and order (secondary key).
205 * Since some things care about execution order, this is the
206 * operation which ensures continued function.
208 for (sipp
= start
; sipp
< stop
; sipp
++) {
209 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
210 if ((*sipp
)->subsystem
> (*xipp
)->subsystem
||
211 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
212 (*sipp
)->order
>= (*xipp
)->order
))
222 * Traverse the (now) ordered list of system initialization tasks.
223 * Perform each task, and continue on to the next task.
225 for (sipp
= start
; sipp
< stop
; sipp
++) {
226 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
227 continue; /* skip dummy task(s)*/
230 (*((*sipp
)->func
))((*sipp
)->udata
);
235 linker_file_register_sysctls(linker_file_t lf
)
237 struct sysctl_oid
**start
, **stop
, **oidp
;
239 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
242 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
244 for (oidp
= start
; oidp
< stop
; oidp
++)
245 sysctl_register_oid(*oidp
);
249 linker_file_unregister_sysctls(linker_file_t lf
)
251 struct sysctl_oid
**start
, **stop
, **oidp
;
253 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
256 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
258 for (oidp
= start
; oidp
< stop
; oidp
++)
259 sysctl_unregister_oid(*oidp
);
263 linker_file_register_modules(linker_file_t lf
)
265 struct mod_metadata
**start
, **stop
, **mdp
;
266 const moduledata_t
*moddata
;
267 int first_error
, error
;
269 KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
272 if (linker_file_lookup_set(lf
, "modmetadata_set", &start
, &stop
, NULL
) != 0) {
274 * This fallback should be unnecessary, but if we get booted
275 * from boot2 instead of loader and we are missing our
276 * metadata then we have to try the best we can.
278 if (lf
== linker_kernel_file
) {
279 start
= SET_BEGIN(modmetadata_set
);
280 stop
= SET_LIMIT(modmetadata_set
);
285 for (mdp
= start
; mdp
< stop
; mdp
++) {
286 if ((*mdp
)->md_type
!= MDT_MODULE
)
288 moddata
= (*mdp
)->md_data
;
289 KLD_DPF(FILE, ("Registering module %s in %s\n", moddata
->name
, lf
->filename
));
290 error
= module_register(moddata
, lf
);
292 kprintf("Module %s failed to register: %d\n", moddata
->name
, error
);
293 if (first_error
== 0)
297 return (first_error
);
301 linker_init_kernel_modules(void)
304 linker_file_register_modules(linker_kernel_file
);
307 SYSINIT(linker_kernel
, SI_BOOT2_KLD
, SI_ORDER_ANY
, linker_init_kernel_modules
, 0);
310 linker_load_file(const char *filename
, linker_file_t
*result
)
314 int foundfile
, error
= 0;
316 /* Refuse to load modules if securelevel raised */
317 if (securelevel
> 0 || kernel_mem_readonly
)
320 lf
= linker_find_file_by_name(filename
);
322 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename
));
330 TAILQ_FOREACH(lc
, &classes
, link
) {
331 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
332 filename
, lc
->desc
));
334 error
= lc
->ops
->load_file(filename
, &lf
);
336 * If we got something other than ENOENT, then it exists but we cannot
337 * load it for some other reason.
342 error
= linker_file_register_modules(lf
);
343 if (error
== EEXIST
) {
344 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */);
347 linker_file_register_sysctls(lf
);
348 linker_file_sysinit(lf
);
349 lf
->flags
|= LINKER_FILE_LINKED
;
355 * Less than ideal, but tells the user whether it failed to load or
356 * the module was not found.
360 * If the file type has not been recognized by the last try
361 * printout a message before to fail.
364 kprintf("linker_load_file: Unsupported file type\n");
367 * Format not recognized or otherwise unloadable.
368 * When loading a module that is statically built into
369 * the kernel EEXIST percolates back up as the return
370 * value. Preserve this so that apps can recognize this
376 error
= ENOENT
; /* Nothing found */
385 linker_find_file_by_name(const char* filename
)
387 linker_file_t lf
= NULL
;
391 for (i
= strlen(filename
); i
> 0 && filename
[i
-1] != '/'; --i
)
395 koname
= kmalloc(strlen(filename
) + 4, M_LINKER
, M_WAITOK
);
396 ksprintf(koname
, "%s.ko", filename
);
398 lockmgr(&llf_lock
, LK_SHARED
);
399 TAILQ_FOREACH(lf
, &linker_files
, link
) {
400 if (!strcmp(lf
->filename
, koname
))
402 if (!strcmp(lf
->filename
, filename
))
405 lockmgr(&llf_lock
, LK_RELEASE
);
408 kfree(koname
, M_LINKER
);
413 linker_find_file_by_id(int fileid
)
415 linker_file_t lf
= NULL
;
417 lockmgr(&llf_lock
, LK_SHARED
);
418 TAILQ_FOREACH(lf
, &linker_files
, link
) {
419 if (lf
->id
== fileid
)
422 lockmgr(&llf_lock
, LK_RELEASE
);
428 linker_file_foreach(linker_predicate_t
*predicate
, void *context
)
433 lockmgr(&llf_lock
, LK_SHARED
);
434 TAILQ_FOREACH(lf
, &linker_files
, link
) {
435 retval
= predicate(lf
, context
);
439 lockmgr(&llf_lock
, LK_RELEASE
);
445 linker_make_file(const char* pathname
, void* priv
, struct linker_file_ops
* ops
)
447 linker_file_t lf
= NULL
;
448 const char *filename
;
450 filename
= linker_basename(pathname
);
451 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s, pathname=%s\n",
452 filename
, pathname
));
454 lockmgr(&llf_lock
, LK_EXCLUSIVE
);
455 lf
= kmalloc(sizeof(struct linker_file
), M_LINKER
, M_WAITOK
| M_ZERO
);
459 lf
->filename
= linker_strdup(filename
);
460 lf
->pathname
= linker_strdup(pathname
);
461 lf
->id
= next_file_id
++;
464 STAILQ_INIT(&lf
->common
);
465 TAILQ_INIT(&lf
->modules
);
469 TAILQ_INSERT_TAIL(&linker_files
, lf
, link
);
471 lockmgr(&llf_lock
, LK_RELEASE
);
477 linker_file_unload(linker_file_t file
)
480 modlist_t ml
, nextml
;
481 struct common_symbol
* cp
;
485 /* Refuse to unload modules if securelevel raised */
486 if (securelevel
> 0 || kernel_mem_readonly
)
489 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file
->refs
));
491 lockmgr(&llf_lock
, LK_EXCLUSIVE
);
493 /* Easy case of just dropping a reference. */
494 if (file
->refs
> 1) {
496 lockmgr(&llf_lock
, LK_RELEASE
);
500 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
503 * Inform any modules associated with this file.
505 mod
= TAILQ_FIRST(&file
->modules
);
506 for (mod
= TAILQ_FIRST(&file
->modules
); mod
; mod
= next
) {
507 next
= module_getfnext(mod
);
510 * Give the module a chance to veto the unload. Note that the
511 * act of unloading the module may cause other modules in the
512 * same file list to be unloaded recursively.
514 if ((error
= module_unload(mod
)) != 0) {
515 KLD_DPF(FILE, ("linker_file_unload: module %p vetoes unload\n",
517 lockmgr(&llf_lock
, LK_RELEASE
);
524 TAILQ_FOREACH_MUTABLE(ml
, &found_modules
, link
, nextml
) {
525 if (ml
->container
== file
) {
526 TAILQ_REMOVE(&found_modules
, ml
, link
);
531 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
532 if (file
->flags
& LINKER_FILE_LINKED
) {
533 file
->flags
&= ~LINKER_FILE_LINKED
;
534 lockmgr(&llf_lock
, LK_RELEASE
);
535 linker_file_sysuninit(file
);
536 linker_file_unregister_sysctls(file
);
537 lockmgr(&llf_lock
, LK_EXCLUSIVE
);
540 TAILQ_REMOVE(&linker_files
, file
, link
);
543 lockmgr(&llf_lock
, LK_RELEASE
);
544 for (i
= 0; i
< file
->ndeps
; i
++)
545 linker_file_unload(file
->deps
[i
]);
546 lockmgr(&llf_lock
, LK_EXCLUSIVE
);
547 kfree(file
->deps
, M_LINKER
);
551 while ((cp
= STAILQ_FIRST(&file
->common
)) != NULL
) {
552 STAILQ_REMOVE_HEAD(&file
->common
, link
);
556 file
->ops
->unload(file
);
558 if (file
->filename
) {
559 kfree(file
->filename
, M_LINKER
);
560 file
->filename
= NULL
;
562 if (file
->pathname
) {
563 kfree(file
->pathname
, M_LINKER
);
564 file
->pathname
= NULL
;
567 kfree(file
, M_LINKER
);
569 lockmgr(&llf_lock
, LK_RELEASE
);
576 linker_file_add_dependancy(linker_file_t file
, linker_file_t dep
)
578 linker_file_t
* newdeps
;
580 newdeps
= kmalloc((file
->ndeps
+ 1) * sizeof(linker_file_t
*),
581 M_LINKER
, M_WAITOK
| M_ZERO
);
584 bcopy(file
->deps
, newdeps
, file
->ndeps
* sizeof(linker_file_t
*));
585 kfree(file
->deps
, M_LINKER
);
587 file
->deps
= newdeps
;
588 file
->deps
[file
->ndeps
] = dep
;
593 * Locate a linker set and its contents.
594 * This is a helper function to avoid linker_if.h exposure elsewhere.
595 * Note: firstp and lastp are really void ***
598 linker_file_lookup_set(linker_file_t file
, const char *name
,
599 void *firstp
, void *lastp
, int *countp
)
601 return file
->ops
->lookup_set(file
, name
, firstp
, lastp
, countp
);
605 linker_file_lookup_symbol(linker_file_t file
, const char* name
, int deps
, caddr_t
*raddr
)
608 linker_symval_t symval
;
610 size_t common_size
= 0;
613 KLD_DPF(SYM
, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
616 if (file
->ops
->lookup_symbol(file
, name
, &sym
) == 0) {
617 file
->ops
->symbol_values(file
, sym
, &symval
);
620 * XXX Assume a common symbol if its value is 0 and it has a non-zero
621 * size, otherwise it could be an absolute symbol with a value of 0.
623 if (symval
.value
== NULL
&& symval
.size
!= 0) {
625 * For commons, first look them up in the dependancies and
626 * only allocate space if not found there.
628 common_size
= symval
.size
;
630 KLD_DPF(SYM
, ("linker_file_lookup_symbol: symbol.value=%p\n", symval
.value
));
631 *raddr
= symval
.value
;
636 for (i
= 0; i
< file
->ndeps
; i
++) {
637 if (linker_file_lookup_symbol(file
->deps
[i
], name
, 0, raddr
) == 0) {
638 KLD_DPF(SYM
, ("linker_file_lookup_symbol: deps value=%p\n", *raddr
));
643 /* If we have not found it in the dependencies, search globally */
644 TAILQ_FOREACH(lf
, &linker_files
, link
) {
645 /* But skip the current file if it's on the list */
648 /* And skip the files we searched above */
649 for (i
= 0; i
< file
->ndeps
; i
++)
650 if (lf
== file
->deps
[i
])
654 if (linker_file_lookup_symbol(lf
, name
, 0, raddr
) == 0) {
655 KLD_DPF(SYM
, ("linker_file_lookup_symbol: global value=%p\n", *raddr
));
661 if (common_size
> 0) {
663 * This is a common symbol which was not found in the
664 * dependancies. We maintain a simple common symbol table in
667 struct common_symbol
* cp
;
669 STAILQ_FOREACH(cp
, &file
->common
, link
)
670 if (!strcmp(cp
->name
, name
)) {
671 KLD_DPF(SYM
, ("linker_file_lookup_symbol: old common value=%p\n", cp
->address
));
672 *raddr
= cp
->address
;
677 * Round the symbol size up to align.
679 common_size
= (common_size
+ sizeof(int) - 1) & -sizeof(int);
680 cp
= kmalloc(sizeof(struct common_symbol
)
683 M_LINKER
, M_WAITOK
| M_ZERO
);
685 cp
->address
= (caddr_t
) (cp
+ 1);
686 cp
->name
= cp
->address
+ common_size
;
687 strcpy(cp
->name
, name
);
688 bzero(cp
->address
, common_size
);
689 STAILQ_INSERT_TAIL(&file
->common
, cp
, link
);
691 KLD_DPF(SYM
, ("linker_file_lookup_symbol: new common value=%p\n", cp
->address
));
692 *raddr
= cp
->address
;
696 #ifdef _KERNEL_VIRTUAL
697 *raddr
= dlsym(RTLD_NEXT
, name
);
698 if (*raddr
!= NULL
) {
699 KLD_DPF(SYM
, ("linker_file_lookup_symbol: found dlsym=%p\n", *raddr
));
704 KLD_DPF(SYM
, ("linker_file_lookup_symbol: fail\n"));
710 * DDB Helpers. DDB has to look across multiple files with their own
711 * symbol tables and string tables.
713 * Note that we do not obey list locking protocols here. We really don't
714 * need DDB to hang because somebody's got the lock held. We'll take the
715 * chance that the files list is inconsistant instead.
719 linker_ddb_lookup(const char *symstr
, c_linker_sym_t
*sym
)
723 TAILQ_FOREACH(lf
, &linker_files
, link
) {
724 if (lf
->ops
->lookup_symbol(lf
, symstr
, sym
) == 0)
731 linker_ddb_search_symbol(caddr_t value
, c_linker_sym_t
*sym
, long *diffp
)
734 u_long off
= (uintptr_t)value
;
735 u_long diff
, bestdiff
;
741 TAILQ_FOREACH(lf
, &linker_files
, link
) {
742 if (lf
->ops
->search_symbol(lf
, value
, &es
, &diff
) != 0)
744 if (es
!= NULL
&& diff
< bestdiff
) {
763 linker_ddb_symbol_values(c_linker_sym_t sym
, linker_symval_t
*symval
)
767 TAILQ_FOREACH(lf
, &linker_files
, link
) {
768 if (lf
->ops
->symbol_values(lf
, sym
, symval
) == 0)
782 sys_kldload(struct sysmsg
*sysmsg
, const struct kldload_args
*uap
)
784 struct thread
*td
= curthread
;
786 char *kldname
, *modname
;
790 sysmsg
->sysmsg_result
= -1;
792 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
795 if ((error
= priv_check(td
, PRIV_KLD_LOAD
)) != 0)
798 file
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
799 if ((error
= copyinstr(uap
->file
, file
, MAXPATHLEN
, NULL
)) != 0)
803 * If file does not contain a qualified name or any dot in it
804 * (kldname.ko, or kldname.ver.ko) treat it as an interface
807 if (strchr(file
, '/') || strchr(file
, '.')) {
815 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
816 error
= linker_load_module(kldname
, modname
, NULL
, NULL
, &lf
);
817 lockmgr(&kld_lock
, LK_RELEASE
);
822 sysmsg
->sysmsg_result
= lf
->id
;
834 sys_kldunload(struct sysmsg
*sysmsg
, const struct kldunload_args
*uap
)
836 struct thread
*td
= curthread
;
840 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
843 if ((error
= priv_check(td
, PRIV_KLD_UNLOAD
)) != 0)
846 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
847 lf
= linker_find_file_by_id(uap
->fileid
);
849 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf
->userrefs
));
850 if (lf
->userrefs
== 0) {
851 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
856 error
= linker_file_unload(lf
);
863 lockmgr(&kld_lock
, LK_RELEASE
);
872 sys_kldfind(struct sysmsg
*sysmsg
, const struct kldfind_args
*uap
)
875 const char *filename
;
879 sysmsg
->sysmsg_result
= -1;
881 pathname
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
882 if ((error
= copyinstr(uap
->file
, pathname
, MAXPATHLEN
, NULL
)) != 0)
885 filename
= linker_basename(pathname
);
887 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
888 lf
= linker_find_file_by_name(filename
);
890 sysmsg
->sysmsg_result
= lf
->id
;
893 lockmgr(&kld_lock
, LK_RELEASE
);
897 kfree(pathname
, M_TEMP
);
905 sys_kldnext(struct sysmsg
*sysmsg
, const struct kldnext_args
*uap
)
910 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
911 if (uap
->fileid
== 0) {
912 lf
= TAILQ_FIRST(&linker_files
);
914 lf
= linker_find_file_by_id(uap
->fileid
);
919 lf
= TAILQ_NEXT(lf
, link
);
922 /* Skip partially loaded files. */
923 while (lf
!= NULL
&& !(lf
->flags
& LINKER_FILE_LINKED
)) {
924 lf
= TAILQ_NEXT(lf
, link
);
928 sysmsg
->sysmsg_result
= lf
->id
;
930 sysmsg
->sysmsg_result
= 0;
933 lockmgr(&kld_lock
, LK_RELEASE
);
942 sys_kldstat(struct sysmsg
*sysmsg
, const struct kldstat_args
*uap
)
947 struct kld_file_stat
* stat
;
950 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
951 lf
= linker_find_file_by_id(uap
->fileid
);
960 * Check the version of the user's structure.
962 if ((error
= copyin(&stat
->version
, &version
, sizeof(version
))) != 0)
964 if (version
!= sizeof(struct kld_file_stat
)) {
969 namelen
= strlen(lf
->filename
) + 1;
970 if (namelen
> MAXPATHLEN
)
971 namelen
= MAXPATHLEN
;
972 if ((error
= copyout(lf
->filename
, stat
->name
, namelen
)) != 0)
975 namelen
= strlen(lf
->pathname
) + 1;
976 if (namelen
> MAXPATHLEN
)
977 namelen
= MAXPATHLEN
;
978 if ((error
= copyout(lf
->pathname
, stat
->pathname
, namelen
)) != 0)
981 if ((error
= copyout(&lf
->refs
, &stat
->refs
, sizeof(int))) != 0)
983 if ((error
= copyout(&lf
->id
, &stat
->id
, sizeof(int))) != 0)
985 if ((error
= copyout(&lf
->address
, &stat
->address
, sizeof(caddr_t
))) != 0)
987 if ((error
= copyout(&lf
->size
, &stat
->size
, sizeof(size_t))) != 0)
990 sysmsg
->sysmsg_result
= 0;
993 lockmgr(&kld_lock
, LK_RELEASE
);
1002 sys_kldfirstmod(struct sysmsg
*sysmsg
, const struct kldfirstmod_args
*uap
)
1007 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
1008 lf
= linker_find_file_by_id(uap
->fileid
);
1010 if (TAILQ_FIRST(&lf
->modules
))
1011 sysmsg
->sysmsg_result
= module_getid(TAILQ_FIRST(&lf
->modules
));
1013 sysmsg
->sysmsg_result
= 0;
1017 lockmgr(&kld_lock
, LK_RELEASE
);
1026 sys_kldsym(struct sysmsg
*sysmsg
, const struct kldsym_args
*uap
)
1028 char *symstr
= NULL
;
1030 linker_symval_t symval
;
1032 struct kld_sym_lookup lookup
;
1035 lockmgr(&kld_lock
, LK_EXCLUSIVE
);
1036 if ((error
= copyin(uap
->data
, &lookup
, sizeof(lookup
))) != 0)
1038 if (lookup
.version
!= sizeof(lookup
) || uap
->cmd
!= KLDSYM_LOOKUP
) {
1043 symstr
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
1044 if ((error
= copyinstr(lookup
.symname
, symstr
, MAXPATHLEN
, NULL
)) != 0)
1047 if (uap
->fileid
!= 0) {
1048 lf
= linker_find_file_by_id(uap
->fileid
);
1053 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
1054 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
1055 lookup
.symvalue
= (uintptr_t)symval
.value
;
1056 lookup
.symsize
= symval
.size
;
1057 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
1061 TAILQ_FOREACH(lf
, &linker_files
, link
) {
1062 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
1063 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
1064 lookup
.symvalue
= (uintptr_t)symval
.value
;
1065 lookup
.symsize
= symval
.size
;
1066 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
1074 lockmgr(&kld_lock
, LK_RELEASE
);
1076 kfree(symstr
, M_TEMP
);
1082 * Preloaded module support
1086 modlist_lookup(const char *name
, int ver
)
1090 TAILQ_FOREACH(mod
, &found_modules
, link
) {
1091 if (strcmp(mod
->name
, name
) == 0 && (ver
== 0 || mod
->version
== ver
))
1098 modlist_lookup2(const char *name
, struct mod_depend
*verinfo
)
1100 modlist_t mod
, bestmod
;
1103 if (verinfo
== NULL
)
1104 return (modlist_lookup(name
, 0));
1106 TAILQ_FOREACH(mod
, &found_modules
, link
) {
1107 if (strcmp(mod
->name
, name
) != 0)
1110 if (ver
== verinfo
->md_ver_preferred
)
1112 if (ver
>= verinfo
->md_ver_minimum
&&
1113 ver
<= verinfo
->md_ver_maximum
&&
1114 (bestmod
== NULL
|| ver
> bestmod
->version
))
1121 linker_reference_module(const char *modname
, struct mod_depend
*verinfo
,
1122 linker_file_t
*result
)
1127 lockmgr(&llf_lock
, LK_SHARED
);
1128 if ((mod
= modlist_lookup2(modname
, verinfo
)) != NULL
) {
1129 *result
= mod
->container
;
1131 lockmgr(&llf_lock
, LK_RELEASE
);
1135 lockmgr(&llf_lock
, LK_RELEASE
);
1136 /*lockmgr(&kld_lock, LK_EXCLUSIVE);*/
1137 error
= linker_load_module(NULL
, modname
, NULL
, verinfo
, result
);
1138 /*lockmgr(&kld_lock, LK_RELEASE);*/
1144 linker_release_module(const char *modname
, struct mod_depend
*verinfo
,
1150 lockmgr(&llf_lock
, LK_SHARED
);
1152 KASSERT(modname
!= NULL
,
1153 ("linker_release_module: no file or name"));
1154 mod
= modlist_lookup2(modname
, verinfo
);
1156 lockmgr(&llf_lock
, LK_RELEASE
);
1159 lf
= mod
->container
;
1161 KASSERT(modname
== NULL
&& verinfo
== NULL
,
1162 ("linker_release_module: both file and name"));
1163 lockmgr(&llf_lock
, LK_RELEASE
);
1164 /*lockmgr(&kld_lock, LK_EXCLUSIVE);*/
1165 error
= linker_file_unload(lf
);
1166 /*lockmgr(&kld_lock, LK_RELEASE);*/
1172 modlist_newmodule(const char *modname
, int version
, linker_file_t container
)
1176 mod
= kmalloc(sizeof(struct modlist
), M_LINKER
, M_NOWAIT
| M_ZERO
);
1178 panic("no memory for module list");
1179 mod
->container
= container
;
1180 mod
->name
= modname
;
1181 mod
->version
= version
;
1182 TAILQ_INSERT_TAIL(&found_modules
, mod
, link
);
1187 linker_addmodules(linker_file_t lf
, struct mod_metadata
**start
,
1188 struct mod_metadata
**stop
, int preload
)
1190 struct mod_metadata
*mp
, **mdp
;
1191 const char *modname
;
1194 for (mdp
= start
; mdp
< stop
; mdp
++) {
1196 if (mp
->md_type
!= MDT_VERSION
)
1198 modname
= mp
->md_cval
;
1199 ver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1200 if (modlist_lookup(modname
, ver
) != NULL
) {
1201 kprintf("module %s already present!\n", modname
);
1202 /* XXX what can we do? this is a build error. :-( */
1205 modlist_newmodule(modname
, ver
, lf
);
1210 linker_preload(void* arg
)
1213 const char *modname
, *nmodname
;
1215 linker_file_t lf
, nlf
;
1218 linker_file_list_t loaded_files
;
1219 linker_file_list_t depended_files
;
1220 struct mod_metadata
*mp
, *nmp
;
1221 struct mod_metadata
**start
, **stop
, **mdp
, **nmdp
;
1222 struct mod_depend
*verinfo
;
1226 struct sysinit
**si_start
, **si_stop
;
1228 TAILQ_INIT(&loaded_files
);
1229 TAILQ_INIT(&depended_files
);
1230 TAILQ_INIT(&found_modules
);
1233 while ((modptr
= preload_search_next_name(modptr
)) != NULL
) {
1234 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
1235 modtype
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
1236 if (modname
== NULL
) {
1237 kprintf("Preloaded module at %p does not have a name!\n", modptr
);
1240 if (modtype
== NULL
) {
1241 kprintf("Preloaded module at %p does not have a type!\n", modptr
);
1246 kprintf("Preloaded %s \"%s\" at %p.\n", modtype
, modname
, modptr
);
1248 TAILQ_FOREACH(lc
, &classes
, link
) {
1249 error
= lc
->ops
->preload_file(modname
, &lf
);
1255 TAILQ_INSERT_TAIL(&loaded_files
, lf
, loaded
);
1259 * First get a list of stuff in the kernel.
1261 if (linker_file_lookup_set(linker_kernel_file
, MDT_SETNAME
, &start
,
1263 linker_addmodules(linker_kernel_file
, start
, stop
, 1);
1266 * This is a once-off kinky bubble sort to resolve relocation
1267 * dependency requirements.
1270 TAILQ_FOREACH(lf
, &loaded_files
, loaded
) {
1271 error
= linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, NULL
);
1273 * First, look to see if we would successfully link with this
1276 resolves
= 1; /* unless we know otherwise */
1278 for (mdp
= start
; mdp
< stop
; mdp
++) {
1280 if (mp
->md_type
!= MDT_DEPEND
)
1282 modname
= mp
->md_cval
;
1283 verinfo
= mp
->md_data
;
1284 for (nmdp
= start
; nmdp
< stop
; nmdp
++) {
1286 if (nmp
->md_type
!= MDT_VERSION
)
1288 nmodname
= nmp
->md_cval
;
1289 if (strcmp(modname
, nmodname
) == 0)
1292 if (nmdp
< stop
)/* it's a self reference */
1296 * ok, the module isn't here yet, we
1299 if (modlist_lookup2(modname
, verinfo
) == NULL
)
1304 * OK, if we found our modules, we can link. So, "provide"
1305 * the modules inside and add it to the end of the link order
1310 for (mdp
= start
; mdp
< stop
; mdp
++) {
1312 if (mp
->md_type
!= MDT_VERSION
)
1314 modname
= mp
->md_cval
;
1315 nver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1316 if (modlist_lookup(modname
, nver
) != NULL
) {
1317 kprintf("module %s already present!\n", modname
);
1318 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1319 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */ );
1320 /* we changed tailq next ptr */
1323 modlist_newmodule(modname
, nver
, lf
);
1326 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1327 TAILQ_INSERT_TAIL(&depended_files
, lf
, loaded
);
1329 * Since we provided modules, we need to restart the
1330 * sort so that the previous files that depend on us
1331 * have a chance. Also, we've busted the tailq next
1332 * pointer with the REMOVE.
1339 * At this point, we check to see what could not be resolved..
1341 while ((lf
= TAILQ_FIRST(&loaded_files
)) != NULL
) {
1342 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1343 kprintf("KLD file %s is missing dependencies\n", lf
->filename
);
1344 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */ );
1348 * We made it. Finish off the linking in the order we determined.
1350 TAILQ_FOREACH_MUTABLE(lf
, &depended_files
, loaded
, nlf
) {
1351 if (linker_kernel_file
) {
1352 linker_kernel_file
->refs
++;
1353 linker_file_add_dependancy(lf
, linker_kernel_file
);
1357 error
= linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, NULL
);
1359 for (mdp
= start
; mdp
< stop
; mdp
++) {
1361 if (mp
->md_type
!= MDT_DEPEND
)
1363 modname
= mp
->md_cval
;
1364 verinfo
= mp
->md_data
;
1365 mod
= modlist_lookup2(modname
, verinfo
);
1366 /* Don't count self-dependencies */
1367 if (lf
== mod
->container
)
1369 mod
->container
->refs
++;
1370 linker_file_add_dependancy(lf
, mod
->container
);
1374 * Now do relocation etc using the symbol search paths
1375 * established by the dependencies
1377 error
= lf
->ops
->preload_finish(lf
);
1379 TAILQ_REMOVE(&depended_files
, lf
, loaded
);
1380 kprintf("KLD file %s - could not finalize loading\n",
1382 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */);
1385 linker_file_register_modules(lf
);
1386 if (linker_file_lookup_set(lf
, "sysinit_set", &si_start
, &si_stop
, NULL
) == 0)
1387 sysinit_add(si_start
, si_stop
);
1388 linker_file_register_sysctls(lf
);
1389 lf
->flags
|= LINKER_FILE_LINKED
;
1391 /* woohoo! we made it! */
1394 SYSINIT(preload
, SI_BOOT2_KLD
, SI_ORDER_MIDDLE
, linker_preload
, 0);
1397 * Search for a not-loaded module by name.
1399 * Modules may be found in the following locations:
1401 * - preloaded (result is just the module name)
1402 * - on disk (result is full path to module)
1404 * The search path can be manipulated via sysctl. Note that we use the ';'
1405 * character as a separator to be consistent with the bootloader.
1408 static char linker_path
[MAXPATHLEN
] = "/boot/kernel;/boot/modules.local";
1410 SYSCTL_STRING(_kern
, OID_AUTO
, module_path
, CTLFLAG_RW
, linker_path
,
1411 sizeof(linker_path
), "module load search path");
1412 TUNABLE_STR("module_path", linker_path
, sizeof(linker_path
));
1415 linker_search_path(const char *name
)
1417 struct nlookupdata nd
;
1418 char *cp
, *ep
, *result
;
1419 char *buf
, *retbuf
, *freebuf
;
1420 size_t name_len
, prefix_len
;
1425 const char *exts
[] = { "", ".ko", NULL
};
1428 /* already full path */
1430 return linker_strdup(name
);
1432 /* resolve to full path */
1433 if (strchr(name
, '/') != NULL
) {
1436 /* adapted from sys___realpath() */
1437 error
= nlookup_init(&nd
, name
, UIO_SYSSPACE
, NLC_FOLLOW
);
1439 nd
.nl_flags
|= NLC_SHAREDLOCK
;
1440 error
= nlookup(&nd
);
1443 if (nd
.nl_flags
& NLC_NCPISLOCKED
) {
1444 nd
.nl_flags
&= ~NLC_NCPISLOCKED
;
1445 cache_unlock(&nd
.nl_nch
);
1447 error
= cache_fullpath(curproc
, &nd
.nl_nch
, NULL
,
1448 &retbuf
, &freebuf
, 0);
1450 result
= linker_strdup(retbuf
);
1451 kfree(freebuf
, M_TEMP
);
1459 /* traverse the linker path */
1460 buf
= kmalloc(MAXPATHLEN
, M_LINKER
, M_WAITOK
);
1462 name_len
= strlen(name
);
1464 /* find the end of this component */
1465 for (ep
= cp
; (*ep
!= 0) && (*ep
!= ';'); ep
++)
1467 prefix_len
= ep
- cp
;
1468 /* if this component doesn't end with a slash, add one */
1469 if (ep
== cp
|| *(ep
- 1) != '/')
1475 strncpy(result
, cp
, prefix_len
);
1477 result
[prefix_len
++] = '/';
1478 strcpy(result
+ prefix_len
, name
);
1480 result_len
= strlen(result
);
1481 for (ext
= exts
; *ext
!= NULL
; ext
++) {
1482 strcpy(result
+ result_len
, *ext
);
1485 * Attempt to open the file, and return the path if we succeed and it's
1488 error
= nlookup_init(&nd
, result
, UIO_SYSSPACE
, NLC_FOLLOW
|NLC_LOCKVP
);
1490 error
= vn_open(&nd
, NULL
, FREAD
, 0);
1492 type
= nd
.nl_open_vp
->v_type
;
1506 kfree(buf
, M_LINKER
);
1511 * Find a file which contains given module and load it, if "parent" is not
1512 * NULL, register a reference to it.
1515 linker_load_module(const char *kldname
, const char *modname
,
1516 struct linker_file
*parent
, struct mod_depend
*verinfo
,
1517 struct linker_file
**lfpp
)
1519 linker_file_t lfdep
;
1520 const char *filename
;
1524 if (modname
== NULL
) {
1526 * We have to load KLD
1528 KASSERT(verinfo
== NULL
, ("linker_load_module: verinfo is not NULL"));
1529 pathname
= linker_search_path(kldname
);
1531 if (modlist_lookup2(modname
, verinfo
) != NULL
)
1533 if (kldname
!= NULL
) {
1534 pathname
= linker_strdup(kldname
);
1535 } else if (rootvnode
== NULL
) {
1538 pathname
= linker_search_path(modname
);
1542 * Need to find a KLD with required module
1544 pathname
= linker_search_module(modname
,
1545 strlen(modname
), verinfo
);
1548 if (pathname
== NULL
)
1552 * Can't load more than one file with the same basename XXX:
1553 * Actually it should be possible to have multiple KLDs with
1554 * the same basename but different path because they can
1555 * provide different versions of the same modules.
1557 filename
= linker_basename(pathname
);
1558 if (linker_find_file_by_name(filename
))
1562 error
= linker_load_file(pathname
, &lfdep
);
1565 if (modname
&& verinfo
&& modlist_lookup2(modname
, verinfo
) == NULL
) {
1566 linker_file_unload(lfdep
/* , LINKER_UNLOAD_FORCE */ );
1571 linker_file_add_dependancy(parent
, lfdep
);
1576 kfree(pathname
, M_LINKER
);
1581 * This routine is responsible for finding dependencies of userland initiated
1582 * kldload(2)'s of files.
1585 linker_load_dependencies(linker_file_t lf
)
1587 linker_file_t lfdep
;
1588 struct mod_metadata
**start
, **stop
, **mdp
, **nmdp
;
1589 struct mod_metadata
*mp
, *nmp
;
1590 struct mod_depend
*verinfo
;
1592 const char *modname
, *nmodname
;
1593 int ver
, error
= 0, count
;
1596 * All files are dependant on /kernel.
1598 if (linker_kernel_file
) {
1599 linker_kernel_file
->refs
++;
1600 linker_file_add_dependancy(lf
, linker_kernel_file
);
1602 if (linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, &count
) != 0)
1604 for (mdp
= start
; mdp
< stop
; mdp
++) {
1606 if (mp
->md_type
!= MDT_VERSION
)
1608 modname
= mp
->md_cval
;
1609 ver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1610 mod
= modlist_lookup(modname
, ver
);
1612 kprintf("interface %s.%d already present in the KLD '%s'!\n",
1613 modname
, ver
, mod
->container
->filename
);
1618 for (mdp
= start
; mdp
< stop
; mdp
++) {
1620 if (mp
->md_type
!= MDT_DEPEND
)
1622 modname
= mp
->md_cval
;
1623 verinfo
= mp
->md_data
;
1625 for (nmdp
= start
; nmdp
< stop
; nmdp
++) {
1627 if (nmp
->md_type
!= MDT_VERSION
)
1629 nmodname
= nmp
->md_cval
;
1630 if (strcmp(modname
, nmodname
) == 0)
1633 if (nmdp
< stop
) /* early exit, it's a self reference */
1635 mod
= modlist_lookup2(modname
, verinfo
);
1636 if (mod
) { /* woohoo, it's loaded already */
1637 lfdep
= mod
->container
;
1639 linker_file_add_dependancy(lf
, lfdep
);
1642 error
= linker_load_module(NULL
, modname
, lf
, verinfo
, NULL
);
1644 kprintf("KLD %s: depends on %s - not available or version mismatch\n",
1645 lf
->filename
, modname
);
1652 linker_addmodules(lf
, start
, stop
, 0);