2 * Copyright (c) 1997 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/kern/kern_linker.c,v 1.41.2.3 2001/11/21 17:50:35 luigi Exp $
27 * $DragonFly: src/sys/kern/kern_linker.c,v 1.44 2008/09/01 19:39:44 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/sysproto.h>
37 #include <sys/sysent.h>
41 #include <sys/module.h>
42 #include <sys/queue.h>
43 #include <sys/linker.h>
44 #include <sys/fcntl.h>
45 #include <sys/libkern.h>
46 #include <sys/nlookup.h>
47 #include <sys/vnode.h>
48 #include <sys/sysctl.h>
50 #include <vm/vm_zone.h>
52 #ifdef _KERNEL_VIRTUAL
60 /* Metadata from the static kernel */
61 SET_DECLARE(modmetadata_set
, struct mod_metadata
);
62 MALLOC_DEFINE(M_LINKER
, "kld", "kernel linker");
64 linker_file_t linker_current_file
;
65 linker_file_t linker_kernel_file
;
67 static struct lock lock
; /* lock for the file list */
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(&lock
, "klink", 0, 0);
102 TAILQ_INIT(&classes
);
103 TAILQ_INIT(&linker_files
);
106 SYSINIT(linker
, SI_BOOT2_KLD
, SI_ORDER_FIRST
, linker_init
, 0);
109 linker_add_class(const char* desc
, void* priv
,
110 struct linker_class_ops
* ops
)
114 lc
= kmalloc(sizeof(struct linker_class
), M_LINKER
, M_NOWAIT
| M_ZERO
);
121 TAILQ_INSERT_HEAD(&classes
, lc
, link
);
127 linker_file_sysinit(linker_file_t lf
)
129 struct sysinit
** start
, ** stop
;
130 struct sysinit
** sipp
;
131 struct sysinit
** xipp
;
132 struct sysinit
* save
;
134 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
137 if (linker_file_lookup_set(lf
, "sysinit_set", &start
, &stop
, NULL
) != 0)
141 * Perform a bubble sort of the system initialization objects by
142 * their subsystem (primary key) and order (secondary key).
144 * Since some things care about execution order, this is the
145 * operation which ensures continued function.
147 for (sipp
= start
; sipp
< stop
; sipp
++) {
148 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
149 if ((*sipp
)->subsystem
< (*xipp
)->subsystem
||
150 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
151 (*sipp
)->order
<= (*xipp
)->order
))
161 * Traverse the (now) ordered list of system initialization tasks.
162 * Perform each task, and continue on to the next task.
164 for (sipp
= start
; sipp
< stop
; sipp
++) {
165 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
166 continue; /* skip dummy task(s)*/
169 (*((*sipp
)->func
))((*sipp
)->udata
);
174 linker_file_sysuninit(linker_file_t lf
)
176 struct sysinit
** start
, ** stop
;
177 struct sysinit
** sipp
;
178 struct sysinit
** xipp
;
179 struct sysinit
* save
;
181 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
184 if (linker_file_lookup_set(lf
, "sysuninit_set", &start
, &stop
, NULL
) != 0)
188 * Perform a reverse bubble sort of the system initialization objects
189 * by their subsystem (primary key) and order (secondary key).
191 * Since some things care about execution order, this is the
192 * operation which ensures continued function.
194 for (sipp
= start
; sipp
< stop
; sipp
++) {
195 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
196 if ((*sipp
)->subsystem
> (*xipp
)->subsystem
||
197 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
198 (*sipp
)->order
>= (*xipp
)->order
))
208 * Traverse the (now) ordered list of system initialization tasks.
209 * Perform each task, and continue on to the next task.
211 for (sipp
= start
; sipp
< stop
; sipp
++) {
212 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
213 continue; /* skip dummy task(s)*/
216 (*((*sipp
)->func
))((*sipp
)->udata
);
221 linker_file_register_sysctls(linker_file_t lf
)
223 struct sysctl_oid
**start
, **stop
, **oidp
;
225 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
228 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
230 for (oidp
= start
; oidp
< stop
; oidp
++)
231 sysctl_register_oid(*oidp
);
235 linker_file_unregister_sysctls(linker_file_t lf
)
237 struct sysctl_oid
**start
, **stop
, **oidp
;
239 KLD_DPF(FILE, ("linker_file_unregister_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_unregister_oid(*oidp
);
249 linker_file_register_modules(linker_file_t lf
)
251 struct mod_metadata
**start
, **stop
, **mdp
;
252 const moduledata_t
*moddata
;
253 int first_error
, error
;
255 KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
258 if (linker_file_lookup_set(lf
, "modmetadata_set", &start
, &stop
, NULL
) != 0) {
260 * This fallback should be unnecessary, but if we get booted
261 * from boot2 instead of loader and we are missing our
262 * metadata then we have to try the best we can.
264 if (lf
== linker_kernel_file
) {
265 start
= SET_BEGIN(modmetadata_set
);
266 stop
= SET_LIMIT(modmetadata_set
);
271 for (mdp
= start
; mdp
< stop
; mdp
++) {
272 if ((*mdp
)->md_type
!= MDT_MODULE
)
274 moddata
= (*mdp
)->md_data
;
275 KLD_DPF(FILE, ("Registering module %s in %s\n", moddata
->name
, lf
->filename
));
276 error
= module_register(moddata
, lf
);
278 kprintf("Module %s failed to register: %d\n", moddata
->name
, error
);
279 if (first_error
== 0)
283 return (first_error
);
287 linker_init_kernel_modules(void)
290 linker_file_register_modules(linker_kernel_file
);
293 SYSINIT(linker_kernel
, SI_BOOT2_KLD
, SI_ORDER_ANY
, linker_init_kernel_modules
, 0);
296 linker_load_file(const char *filename
, linker_file_t
*result
)
300 int foundfile
, error
= 0;
302 /* Refuse to load modules if securelevel raised */
303 if (securelevel
> 0 || kernel_mem_readonly
)
306 lf
= linker_find_file_by_name(filename
);
308 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename
));
316 TAILQ_FOREACH(lc
, &classes
, link
) {
317 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
318 filename
, lc
->desc
));
320 error
= lc
->ops
->load_file(filename
, &lf
);
322 * If we got something other than ENOENT, then it exists but we cannot
323 * load it for some other reason.
328 error
= linker_file_register_modules(lf
);
329 if (error
== EEXIST
) {
330 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */);
333 linker_file_register_sysctls(lf
);
334 linker_file_sysinit(lf
);
335 lf
->flags
|= LINKER_FILE_LINKED
;
341 * Less than ideal, but tells the user whether it failed to load or
342 * the module was not found.
346 * If the file type has not been recognized by the last try
347 * printout a message before to fail.
350 kprintf("linker_load_file: Unsupported file type\n");
353 * Format not recognized or otherwise unloadable.
354 * When loading a module that is statically built into
355 * the kernel EEXIST percolates back up as the return
356 * value. Preserve this so that apps can recognize this
362 error
= ENOENT
; /* Nothing found */
370 linker_find_file_by_name(const char* filename
)
372 linker_file_t lf
= 0;
376 for (i
= strlen(filename
); i
> 0 && filename
[i
-1] != '/'; --i
)
380 koname
= kmalloc(strlen(filename
) + 4, M_LINKER
, M_WAITOK
);
381 ksprintf(koname
, "%s.ko", filename
);
383 lockmgr(&lock
, LK_SHARED
);
384 TAILQ_FOREACH(lf
, &linker_files
, link
) {
385 if (!strcmp(lf
->filename
, koname
))
387 if (!strcmp(lf
->filename
, filename
))
390 lockmgr(&lock
, LK_RELEASE
);
393 kfree(koname
, M_LINKER
);
398 linker_find_file_by_id(int fileid
)
400 linker_file_t lf
= 0;
402 lockmgr(&lock
, LK_SHARED
);
403 TAILQ_FOREACH(lf
, &linker_files
, link
)
404 if (lf
->id
== fileid
)
406 lockmgr(&lock
, LK_RELEASE
);
412 linker_make_file(const char* pathname
, void* priv
, struct linker_file_ops
* ops
)
414 linker_file_t lf
= 0;
415 const char *filename
;
417 filename
= rindex(pathname
, '/');
418 if (filename
&& filename
[1])
423 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename
));
424 lockmgr(&lock
, LK_EXCLUSIVE
);
425 lf
= kmalloc(sizeof(struct linker_file
), M_LINKER
, M_WAITOK
);
426 bzero(lf
, sizeof(*lf
));
431 lf
->filename
= linker_strdup(filename
);
432 lf
->id
= next_file_id
++;
435 STAILQ_INIT(&lf
->common
);
436 TAILQ_INIT(&lf
->modules
);
440 TAILQ_INSERT_TAIL(&linker_files
, lf
, link
);
442 lockmgr(&lock
, LK_RELEASE
);
447 linker_file_unload(linker_file_t file
)
450 modlist_t ml
, nextml
;
451 struct common_symbol
* cp
;
455 /* Refuse to unload modules if securelevel raised */
456 if (securelevel
> 0 || kernel_mem_readonly
)
459 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file
->refs
));
461 lockmgr(&lock
, LK_EXCLUSIVE
);
463 /* Easy case of just dropping a reference. */
464 if (file
->refs
> 1) {
466 lockmgr(&lock
, LK_RELEASE
);
470 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
473 * Inform any modules associated with this file.
475 mod
= TAILQ_FIRST(&file
->modules
);
476 for (mod
= TAILQ_FIRST(&file
->modules
); mod
; mod
= next
) {
477 next
= module_getfnext(mod
);
480 * Give the module a chance to veto the unload. Note that the
481 * act of unloading the module may cause other modules in the
482 * same file list to be unloaded recursively.
484 if ((error
= module_unload(mod
)) != 0) {
485 KLD_DPF(FILE, ("linker_file_unload: module %p vetoes unload\n",
487 lockmgr(&lock
, LK_RELEASE
);
494 TAILQ_FOREACH_MUTABLE(ml
, &found_modules
, link
, nextml
) {
495 if (ml
->container
== file
) {
496 TAILQ_REMOVE(&found_modules
, ml
, link
);
501 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
502 if (file
->flags
& LINKER_FILE_LINKED
) {
503 file
->flags
&= ~LINKER_FILE_LINKED
;
504 lockmgr(&lock
, LK_RELEASE
);
505 linker_file_sysuninit(file
);
506 linker_file_unregister_sysctls(file
);
507 lockmgr(&lock
, LK_EXCLUSIVE
);
510 TAILQ_REMOVE(&linker_files
, file
, link
);
513 lockmgr(&lock
, LK_RELEASE
);
514 for (i
= 0; i
< file
->ndeps
; i
++)
515 linker_file_unload(file
->deps
[i
]);
516 lockmgr(&lock
, LK_EXCLUSIVE
);
517 kfree(file
->deps
, M_LINKER
);
521 while ((cp
= STAILQ_FIRST(&file
->common
)) != NULL
) {
522 STAILQ_REMOVE_HEAD(&file
->common
, link
);
526 file
->ops
->unload(file
);
528 if (file
->filename
) {
529 kfree(file
->filename
, M_LINKER
);
530 file
->filename
= NULL
;
533 kfree(file
, M_LINKER
);
535 lockmgr(&lock
, LK_RELEASE
);
542 linker_file_add_dependancy(linker_file_t file
, linker_file_t dep
)
544 linker_file_t
* newdeps
;
546 newdeps
= kmalloc((file
->ndeps
+ 1) * sizeof(linker_file_t
*),
547 M_LINKER
, M_WAITOK
| M_ZERO
);
550 bcopy(file
->deps
, newdeps
, file
->ndeps
* sizeof(linker_file_t
*));
551 kfree(file
->deps
, M_LINKER
);
553 file
->deps
= newdeps
;
554 file
->deps
[file
->ndeps
] = dep
;
559 * Locate a linker set and its contents.
560 * This is a helper function to avoid linker_if.h exposure elsewhere.
561 * Note: firstp and lastp are really void ***
564 linker_file_lookup_set(linker_file_t file
, const char *name
,
565 void *firstp
, void *lastp
, int *countp
)
567 return file
->ops
->lookup_set(file
, name
, firstp
, lastp
, countp
);
571 linker_file_lookup_symbol(linker_file_t file
, const char* name
, int deps
, caddr_t
*raddr
)
574 linker_symval_t symval
;
576 size_t common_size
= 0;
579 KLD_DPF(SYM
, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
582 if (file
->ops
->lookup_symbol(file
, name
, &sym
) == 0) {
583 file
->ops
->symbol_values(file
, sym
, &symval
);
586 * XXX Assume a common symbol if its value is 0 and it has a non-zero
587 * size, otherwise it could be an absolute symbol with a value of 0.
589 if (symval
.value
== 0 && symval
.size
!= 0) {
591 * For commons, first look them up in the dependancies and
592 * only allocate space if not found there.
594 common_size
= symval
.size
;
596 KLD_DPF(SYM
, ("linker_file_lookup_symbol: symbol.value=%p\n", symval
.value
));
597 *raddr
= symval
.value
;
602 for (i
= 0; i
< file
->ndeps
; i
++) {
603 if (linker_file_lookup_symbol(file
->deps
[i
], name
, 0, raddr
) == 0) {
604 KLD_DPF(SYM
, ("linker_file_lookup_symbol: deps value=%p\n", *raddr
));
609 /* If we have not found it in the dependencies, search globally */
610 TAILQ_FOREACH(lf
, &linker_files
, link
) {
611 /* But skip the current file if it's on the list */
614 /* And skip the files we searched above */
615 for (i
= 0; i
< file
->ndeps
; i
++)
616 if (lf
== file
->deps
[i
])
620 if (linker_file_lookup_symbol(lf
, name
, 0, raddr
) == 0) {
621 KLD_DPF(SYM
, ("linker_file_lookup_symbol: global value=%p\n", *raddr
));
627 if (common_size
> 0) {
629 * This is a common symbol which was not found in the
630 * dependancies. We maintain a simple common symbol table in
633 struct common_symbol
* cp
;
635 STAILQ_FOREACH(cp
, &file
->common
, link
)
636 if (!strcmp(cp
->name
, name
)) {
637 KLD_DPF(SYM
, ("linker_file_lookup_symbol: old common value=%p\n", cp
->address
));
638 *raddr
= cp
->address
;
643 * Round the symbol size up to align.
645 common_size
= (common_size
+ sizeof(int) - 1) & -sizeof(int);
646 cp
= kmalloc(sizeof(struct common_symbol
)
649 M_LINKER
, M_WAITOK
| M_ZERO
);
651 cp
->address
= (caddr_t
) (cp
+ 1);
652 cp
->name
= cp
->address
+ common_size
;
653 strcpy(cp
->name
, name
);
654 bzero(cp
->address
, common_size
);
655 STAILQ_INSERT_TAIL(&file
->common
, cp
, link
);
657 KLD_DPF(SYM
, ("linker_file_lookup_symbol: new common value=%p\n", cp
->address
));
658 *raddr
= cp
->address
;
662 #ifdef _KERNEL_VIRTUAL
663 *raddr
= dlsym(RTLD_NEXT
, name
);
664 if (*raddr
!= NULL
) {
665 KLD_DPF(SYM
, ("linker_file_lookup_symbol: found dlsym=%x\n", *raddr
));
670 KLD_DPF(SYM
, ("linker_file_lookup_symbol: fail\n"));
676 * DDB Helpers. DDB has to look across multiple files with their own
677 * symbol tables and string tables.
679 * Note that we do not obey list locking protocols here. We really don't
680 * need DDB to hang because somebody's got the lock held. We'll take the
681 * chance that the files list is inconsistant instead.
685 linker_ddb_lookup(const char *symstr
, c_linker_sym_t
*sym
)
689 TAILQ_FOREACH(lf
, &linker_files
, link
) {
690 if (lf
->ops
->lookup_symbol(lf
, symstr
, sym
) == 0)
697 linker_ddb_search_symbol(caddr_t value
, c_linker_sym_t
*sym
, long *diffp
)
700 u_long off
= (uintptr_t)value
;
701 u_long diff
, bestdiff
;
707 TAILQ_FOREACH(lf
, &linker_files
, link
) {
708 if (lf
->ops
->search_symbol(lf
, value
, &es
, &diff
) != 0)
710 if (es
!= 0 && diff
< bestdiff
) {
729 linker_ddb_symbol_values(c_linker_sym_t sym
, linker_symval_t
*symval
)
733 TAILQ_FOREACH(lf
, &linker_files
, link
) {
734 if (lf
->ops
->symbol_values(lf
, sym
, symval
) == 0)
747 sys_kldload(struct kldload_args
*uap
)
749 struct thread
*td
= curthread
;
751 char *kldname
, *modname
;
755 uap
->sysmsg_result
= -1;
757 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
760 if ((error
= priv_check(td
, PRIV_KLD_LOAD
)) != 0)
763 file
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
764 if ((error
= copyinstr(uap
->file
, file
, MAXPATHLEN
, NULL
)) != 0)
768 * If file does not contain a qualified name or any dot in it
769 * (kldname.ko, or kldname.ver.ko) treat it as an interface
772 if (index(file
, '/') || index(file
, '.')) {
780 if ((error
= linker_load_module(kldname
, modname
, NULL
, NULL
, &lf
)) != 0)
784 uap
->sysmsg_result
= lf
->id
;
793 sys_kldunload(struct kldunload_args
*uap
)
795 struct thread
*td
= curthread
;
799 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
802 if ((error
= priv_check(td
, PRIV_KLD_UNLOAD
)) != 0)
805 lf
= linker_find_file_by_id(uap
->fileid
);
807 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf
->userrefs
));
808 if (lf
->userrefs
== 0) {
809 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
814 error
= linker_file_unload(lf
);
825 sys_kldfind(struct kldfind_args
*uap
)
827 char *filename
= NULL
, *modulename
;
831 uap
->sysmsg_result
= -1;
833 filename
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
834 if ((error
= copyinstr(uap
->file
, filename
, MAXPATHLEN
, NULL
)) != 0)
837 modulename
= rindex(filename
, '/');
838 if (modulename
== NULL
)
839 modulename
= filename
;
841 lf
= linker_find_file_by_name(modulename
);
843 uap
->sysmsg_result
= lf
->id
;
849 kfree(filename
, M_TEMP
);
854 sys_kldnext(struct kldnext_args
*uap
)
859 if (uap
->fileid
== 0)
860 lf
= TAILQ_FIRST(&linker_files
);
862 lf
= linker_find_file_by_id(uap
->fileid
);
867 lf
= TAILQ_NEXT(lf
, link
);
870 /* Skip partially loaded files. */
871 while (lf
!= NULL
&& !(lf
->flags
& LINKER_FILE_LINKED
)) {
872 lf
= TAILQ_NEXT(lf
, link
);
876 uap
->sysmsg_result
= lf
->id
;
878 uap
->sysmsg_result
= 0;
885 sys_kldstat(struct kldstat_args
*uap
)
890 struct kld_file_stat
* stat
;
893 lf
= linker_find_file_by_id(uap
->fileid
);
902 * Check the version of the user's structure.
904 if ((error
= copyin(&stat
->version
, &version
, sizeof(version
))) != 0)
906 if (version
!= sizeof(struct kld_file_stat
)) {
911 namelen
= strlen(lf
->filename
) + 1;
912 if (namelen
> MAXPATHLEN
)
913 namelen
= MAXPATHLEN
;
914 if ((error
= copyout(lf
->filename
, &stat
->name
[0], namelen
)) != 0)
916 if ((error
= copyout(&lf
->refs
, &stat
->refs
, sizeof(int))) != 0)
918 if ((error
= copyout(&lf
->id
, &stat
->id
, sizeof(int))) != 0)
920 if ((error
= copyout(&lf
->address
, &stat
->address
, sizeof(caddr_t
))) != 0)
922 if ((error
= copyout(&lf
->size
, &stat
->size
, sizeof(size_t))) != 0)
925 uap
->sysmsg_result
= 0;
932 sys_kldfirstmod(struct kldfirstmod_args
*uap
)
937 lf
= linker_find_file_by_id(uap
->fileid
);
939 if (TAILQ_FIRST(&lf
->modules
))
940 uap
->sysmsg_result
= module_getid(TAILQ_FIRST(&lf
->modules
));
942 uap
->sysmsg_result
= 0;
950 sys_kldsym(struct kldsym_args
*uap
)
954 linker_symval_t symval
;
956 struct kld_sym_lookup lookup
;
959 if ((error
= copyin(uap
->data
, &lookup
, sizeof(lookup
))) != 0)
961 if (lookup
.version
!= sizeof(lookup
) || uap
->cmd
!= KLDSYM_LOOKUP
) {
966 symstr
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
967 if ((error
= copyinstr(lookup
.symname
, symstr
, MAXPATHLEN
, NULL
)) != 0)
970 if (uap
->fileid
!= 0) {
971 lf
= linker_find_file_by_id(uap
->fileid
);
976 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
977 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
978 lookup
.symvalue
= (uintptr_t)symval
.value
;
979 lookup
.symsize
= symval
.size
;
980 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
984 TAILQ_FOREACH(lf
, &linker_files
, link
) {
985 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
986 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
987 lookup
.symvalue
= (uintptr_t)symval
.value
;
988 lookup
.symsize
= symval
.size
;
989 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
998 kfree(symstr
, M_TEMP
);
1003 * Preloaded module support
1007 modlist_lookup(const char *name
, int ver
)
1011 TAILQ_FOREACH(mod
, &found_modules
, link
) {
1012 if (strcmp(mod
->name
, name
) == 0 && (ver
== 0 || mod
->version
== ver
))
1019 modlist_lookup2(const char *name
, struct mod_depend
*verinfo
)
1021 modlist_t mod
, bestmod
;
1024 if (verinfo
== NULL
)
1025 return (modlist_lookup(name
, 0));
1027 TAILQ_FOREACH(mod
, &found_modules
, link
) {
1028 if (strcmp(mod
->name
, name
) != 0)
1031 if (ver
== verinfo
->md_ver_preferred
)
1033 if (ver
>= verinfo
->md_ver_minimum
&&
1034 ver
<= verinfo
->md_ver_maximum
&&
1035 (bestmod
== NULL
|| ver
> bestmod
->version
))
1042 modlist_newmodule(const char *modname
, int version
, linker_file_t container
)
1046 mod
= kmalloc(sizeof(struct modlist
), M_LINKER
, M_NOWAIT
| M_ZERO
);
1048 panic("no memory for module list");
1049 mod
->container
= container
;
1050 mod
->name
= modname
;
1051 mod
->version
= version
;
1052 TAILQ_INSERT_TAIL(&found_modules
, mod
, link
);
1057 linker_addmodules(linker_file_t lf
, struct mod_metadata
**start
,
1058 struct mod_metadata
**stop
, int preload
)
1060 struct mod_metadata
*mp
, **mdp
;
1061 const char *modname
;
1064 for (mdp
= start
; mdp
< stop
; mdp
++) {
1066 if (mp
->md_type
!= MDT_VERSION
)
1068 modname
= mp
->md_cval
;
1069 ver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1070 if (modlist_lookup(modname
, ver
) != NULL
) {
1071 kprintf("module %s already present!\n", modname
);
1072 /* XXX what can we do? this is a build error. :-( */
1075 modlist_newmodule(modname
, ver
, lf
);
1080 linker_preload(void* arg
)
1083 const char *modname
, *nmodname
;
1085 linker_file_t lf
, nlf
;
1088 linker_file_list_t loaded_files
;
1089 linker_file_list_t depended_files
;
1090 struct mod_metadata
*mp
, *nmp
;
1091 struct mod_metadata
**start
, **stop
, **mdp
, **nmdp
;
1092 struct mod_depend
*verinfo
;
1096 struct sysinit
**si_start
, **si_stop
;
1098 TAILQ_INIT(&loaded_files
);
1099 TAILQ_INIT(&depended_files
);
1100 TAILQ_INIT(&found_modules
);
1103 while ((modptr
= preload_search_next_name(modptr
)) != NULL
) {
1104 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
1105 modtype
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
1106 if (modname
== NULL
) {
1107 kprintf("Preloaded module at %p does not have a name!\n", modptr
);
1110 if (modtype
== NULL
) {
1111 kprintf("Preloaded module at %p does not have a type!\n", modptr
);
1116 kprintf("Preloaded %s \"%s\" at %p.\n", modtype
, modname
, modptr
);
1118 TAILQ_FOREACH(lc
, &classes
, link
) {
1119 error
= lc
->ops
->preload_file(modname
, &lf
);
1125 TAILQ_INSERT_TAIL(&loaded_files
, lf
, loaded
);
1129 * First get a list of stuff in the kernel.
1131 if (linker_file_lookup_set(linker_kernel_file
, MDT_SETNAME
, &start
,
1133 linker_addmodules(linker_kernel_file
, start
, stop
, 1);
1136 * This is a once-off kinky bubble sort to resolve relocation
1137 * dependency requirements.
1140 TAILQ_FOREACH(lf
, &loaded_files
, loaded
) {
1141 error
= linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, NULL
);
1143 * First, look to see if we would successfully link with this
1146 resolves
= 1; /* unless we know otherwise */
1148 for (mdp
= start
; mdp
< stop
; mdp
++) {
1150 if (mp
->md_type
!= MDT_DEPEND
)
1152 modname
= mp
->md_cval
;
1153 verinfo
= mp
->md_data
;
1154 for (nmdp
= start
; nmdp
< stop
; nmdp
++) {
1156 if (nmp
->md_type
!= MDT_VERSION
)
1158 nmodname
= nmp
->md_cval
;
1159 if (strcmp(modname
, nmodname
) == 0)
1162 if (nmdp
< stop
)/* it's a self reference */
1166 * ok, the module isn't here yet, we
1169 if (modlist_lookup2(modname
, verinfo
) == NULL
)
1174 * OK, if we found our modules, we can link. So, "provide"
1175 * the modules inside and add it to the end of the link order
1180 for (mdp
= start
; mdp
< stop
; mdp
++) {
1182 if (mp
->md_type
!= MDT_VERSION
)
1184 modname
= mp
->md_cval
;
1185 nver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1186 if (modlist_lookup(modname
, nver
) != NULL
) {
1187 kprintf("module %s already present!\n", modname
);
1188 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1189 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */ );
1190 /* we changed tailq next ptr */
1193 modlist_newmodule(modname
, nver
, lf
);
1196 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1197 TAILQ_INSERT_TAIL(&depended_files
, lf
, loaded
);
1199 * Since we provided modules, we need to restart the
1200 * sort so that the previous files that depend on us
1201 * have a chance. Also, we've busted the tailq next
1202 * pointer with the REMOVE.
1209 * At this point, we check to see what could not be resolved..
1211 while ((lf
= TAILQ_FIRST(&loaded_files
)) != NULL
) {
1212 TAILQ_REMOVE(&loaded_files
, lf
, loaded
);
1213 kprintf("KLD file %s is missing dependencies\n", lf
->filename
);
1214 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */ );
1218 * We made it. Finish off the linking in the order we determined.
1220 TAILQ_FOREACH_MUTABLE(lf
, &depended_files
, loaded
, nlf
) {
1221 if (linker_kernel_file
) {
1222 linker_kernel_file
->refs
++;
1223 linker_file_add_dependancy(lf
, linker_kernel_file
);
1227 error
= linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, NULL
);
1229 for (mdp
= start
; mdp
< stop
; mdp
++) {
1231 if (mp
->md_type
!= MDT_DEPEND
)
1233 modname
= mp
->md_cval
;
1234 verinfo
= mp
->md_data
;
1235 mod
= modlist_lookup2(modname
, verinfo
);
1236 /* Don't count self-dependencies */
1237 if (lf
== mod
->container
)
1239 mod
->container
->refs
++;
1240 linker_file_add_dependancy(lf
, mod
->container
);
1244 * Now do relocation etc using the symbol search paths
1245 * established by the dependencies
1247 error
= lf
->ops
->preload_finish(lf
);
1249 TAILQ_REMOVE(&depended_files
, lf
, loaded
);
1250 kprintf("KLD file %s - could not finalize loading\n",
1252 linker_file_unload(lf
/* , LINKER_UNLOAD_FORCE */);
1255 linker_file_register_modules(lf
);
1256 if (linker_file_lookup_set(lf
, "sysinit_set", &si_start
, &si_stop
, NULL
) == 0)
1257 sysinit_add(si_start
, si_stop
);
1258 linker_file_register_sysctls(lf
);
1259 lf
->flags
|= LINKER_FILE_LINKED
;
1261 /* woohoo! we made it! */
1264 SYSINIT(preload
, SI_BOOT2_KLD
, SI_ORDER_MIDDLE
, linker_preload
, 0);
1267 * Search for a not-loaded module by name.
1269 * Modules may be found in the following locations:
1271 * - preloaded (result is just the module name)
1272 * - on disk (result is full path to module)
1274 * If the module name is qualified in any way (contains path, etc.)
1275 * the we simply return a copy of it.
1277 * The search path can be manipulated via sysctl. Note that we use the ';'
1278 * character as a separator to be consistent with the bootloader.
1281 static char linker_path
[MAXPATHLEN
] = "/boot;/boot/modules;/;/modules";
1283 SYSCTL_STRING(_kern
, OID_AUTO
, module_path
, CTLFLAG_RW
, linker_path
,
1284 sizeof(linker_path
), "module load search path");
1285 TUNABLE_STR("module_path", linker_path
, sizeof(linker_path
));
1288 linker_search_path(const char *name
)
1290 struct nlookupdata nd
;
1291 char *cp
, *ep
, *result
;
1292 size_t name_len
, prefix_len
;
1297 const char *exts
[] = { "", ".ko", NULL
};
1300 /* qualified at all? */
1301 if (index(name
, '/'))
1302 return(linker_strdup(name
));
1304 /* traverse the linker path */
1306 name_len
= strlen(name
);
1309 /* find the end of this component */
1310 for (ep
= cp
; (*ep
!= 0) && (*ep
!= ';'); ep
++)
1312 prefix_len
= ep
- cp
;
1313 /* if this component doesn't end with a slash, add one */
1314 if (ep
== cp
|| *(ep
- 1) != '/')
1320 * +2+3 : possible separator, plus terminator + possible extension.
1322 result
= kmalloc(prefix_len
+ name_len
+ 2+3, M_LINKER
, M_WAITOK
);
1324 strncpy(result
, cp
, prefix_len
);
1326 result
[prefix_len
++] = '/';
1327 strcpy(result
+ prefix_len
, name
);
1329 result_len
= strlen(result
);
1330 for (ext
= exts
; *ext
!= NULL
; ext
++) {
1331 strcpy(result
+ result_len
, *ext
);
1334 * Attempt to open the file, and return the path if we succeed and it's
1337 error
= nlookup_init(&nd
, result
, UIO_SYSSPACE
, NLC_FOLLOW
|NLC_LOCKVP
);
1339 error
= vn_open(&nd
, NULL
, FREAD
, 0);
1341 type
= nd
.nl_open_vp
->v_type
;
1350 kfree(result
, M_LINKER
);
1360 * Find a file which contains given module and load it, if "parent" is not
1361 * NULL, register a reference to it.
1364 linker_load_module(const char *kldname
, const char *modname
,
1365 struct linker_file
*parent
, struct mod_depend
*verinfo
,
1366 struct linker_file
**lfpp
)
1368 linker_file_t lfdep
;
1369 const char *filename
;
1373 if (modname
== NULL
) {
1375 * We have to load KLD
1377 KASSERT(verinfo
== NULL
, ("linker_load_module: verinfo is not NULL"));
1378 pathname
= linker_search_path(kldname
);
1380 if (modlist_lookup2(modname
, verinfo
) != NULL
)
1382 if (kldname
!= NULL
)
1383 pathname
= linker_strdup(kldname
);
1384 else if (rootvnode
== NULL
)
1387 pathname
= linker_search_path(modname
);
1390 * Need to find a KLD with required module
1392 pathname
= linker_search_module(modname
,
1393 strlen(modname
), verinfo
);
1396 if (pathname
== NULL
)
1400 * Can't load more than one file with the same basename XXX:
1401 * Actually it should be possible to have multiple KLDs with
1402 * the same basename but different path because they can
1403 * provide different versions of the same modules.
1405 filename
= rindex(pathname
, '/');
1406 if (filename
== NULL
)
1407 filename
= filename
;
1410 if (linker_find_file_by_name(filename
))
1414 error
= linker_load_file(pathname
, &lfdep
);
1417 if (modname
&& verinfo
&& modlist_lookup2(modname
, verinfo
) == NULL
) {
1418 linker_file_unload(lfdep
/* , LINKER_UNLOAD_FORCE */ );
1423 linker_file_add_dependancy(parent
, lfdep
);
1428 kfree(pathname
, M_LINKER
);
1433 * This routine is responsible for finding dependencies of userland initiated
1434 * kldload(2)'s of files.
1437 linker_load_dependencies(linker_file_t lf
)
1439 linker_file_t lfdep
;
1440 struct mod_metadata
**start
, **stop
, **mdp
, **nmdp
;
1441 struct mod_metadata
*mp
, *nmp
;
1442 struct mod_depend
*verinfo
;
1444 const char *modname
, *nmodname
;
1445 int ver
, error
= 0, count
;
1448 * All files are dependant on /kernel.
1450 if (linker_kernel_file
) {
1451 linker_kernel_file
->refs
++;
1452 linker_file_add_dependancy(lf
, linker_kernel_file
);
1454 if (linker_file_lookup_set(lf
, MDT_SETNAME
, &start
, &stop
, &count
) != 0)
1456 for (mdp
= start
; mdp
< stop
; mdp
++) {
1458 if (mp
->md_type
!= MDT_VERSION
)
1460 modname
= mp
->md_cval
;
1461 ver
= ((struct mod_version
*)mp
->md_data
)->mv_version
;
1462 mod
= modlist_lookup(modname
, ver
);
1464 kprintf("interface %s.%d already present in the KLD '%s'!\n",
1465 modname
, ver
, mod
->container
->filename
);
1470 for (mdp
= start
; mdp
< stop
; mdp
++) {
1472 if (mp
->md_type
!= MDT_DEPEND
)
1474 modname
= mp
->md_cval
;
1475 verinfo
= mp
->md_data
;
1477 for (nmdp
= start
; nmdp
< stop
; nmdp
++) {
1479 if (nmp
->md_type
!= MDT_VERSION
)
1481 nmodname
= nmp
->md_cval
;
1482 if (strcmp(modname
, nmodname
) == 0)
1485 if (nmdp
< stop
) /* early exit, it's a self reference */
1487 mod
= modlist_lookup2(modname
, verinfo
);
1488 if (mod
) { /* woohoo, it's loaded already */
1489 lfdep
= mod
->container
;
1491 linker_file_add_dependancy(lf
, lfdep
);
1494 error
= linker_load_module(NULL
, modname
, lf
, verinfo
, NULL
);
1496 kprintf("KLD %s: depends on %s - not available or version mismatch\n",
1497 lf
->filename
, modname
);
1504 linker_addmodules(lf
, start
, stop
, 0);