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;
73 linker_init(void* arg
)
75 lockinit(&lock
, "klink", 0, 0);
77 TAILQ_INIT(&linker_files
);
80 SYSINIT(linker
, SI_BOOT2_KLD
, SI_ORDER_FIRST
, linker_init
, 0);
83 linker_add_class(const char* desc
, void* priv
,
84 struct linker_class_ops
* ops
)
88 lc
= kmalloc(sizeof(struct linker_class
), M_LINKER
, M_NOWAIT
| M_ZERO
);
95 TAILQ_INSERT_HEAD(&classes
, lc
, link
);
101 linker_file_sysinit(linker_file_t lf
)
103 struct sysinit
** start
, ** stop
;
104 struct sysinit
** sipp
;
105 struct sysinit
** xipp
;
106 struct sysinit
* save
;
107 const moduledata_t
*moddata
;
110 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
113 if (linker_file_lookup_set(lf
, "sysinit_set", &start
, &stop
, NULL
) != 0)
114 return 0; /* XXX is this correct ? No sysinit ? */
117 for (sipp
= start
; sipp
< stop
; sipp
++) {
118 if ((*sipp
)->func
== module_register_init
) {
119 moddata
= (*sipp
)->udata
;
120 error
= module_register(moddata
, lf
);
122 kprintf("linker_file_sysinit \"%s\" failed to register! %d\n",
123 lf
->filename
, error
);
130 * Perform a bubble sort of the system initialization objects by
131 * their subsystem (primary key) and order (secondary key).
133 * Since some things care about execution order, this is the
134 * operation which ensures continued function.
136 for (sipp
= start
; sipp
< stop
; sipp
++) {
137 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
138 if ((*sipp
)->subsystem
< (*xipp
)->subsystem
||
139 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
140 (*sipp
)->order
<= (*xipp
)->order
))
150 * Traverse the (now) ordered list of system initialization tasks.
151 * Perform each task, and continue on to the next task.
153 for (sipp
= start
; sipp
< stop
; sipp
++) {
154 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
155 continue; /* skip dummy task(s)*/
158 (*((*sipp
)->func
))((*sipp
)->udata
);
160 return 0; /* no errors */
164 linker_file_sysuninit(linker_file_t lf
)
166 struct sysinit
** start
, ** stop
;
167 struct sysinit
** sipp
;
168 struct sysinit
** xipp
;
169 struct sysinit
* save
;
171 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
174 if (linker_file_lookup_set(lf
, "sysuninit_set", &start
, &stop
, NULL
) != 0)
178 * Perform a reverse bubble sort of the system initialization objects
179 * by their subsystem (primary key) and order (secondary key).
181 * Since some things care about execution order, this is the
182 * operation which ensures continued function.
184 for (sipp
= start
; sipp
< stop
; sipp
++) {
185 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
186 if ((*sipp
)->subsystem
> (*xipp
)->subsystem
||
187 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
188 (*sipp
)->order
>= (*xipp
)->order
))
198 * Traverse the (now) ordered list of system initialization tasks.
199 * Perform each task, and continue on to the next task.
201 for (sipp
= start
; sipp
< stop
; sipp
++) {
202 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
203 continue; /* skip dummy task(s)*/
206 (*((*sipp
)->func
))((*sipp
)->udata
);
211 linker_file_register_sysctls(linker_file_t lf
)
213 struct sysctl_oid
**start
, **stop
, **oidp
;
215 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
218 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
220 for (oidp
= start
; oidp
< stop
; oidp
++)
221 sysctl_register_oid(*oidp
);
225 linker_file_unregister_sysctls(linker_file_t lf
)
227 struct sysctl_oid
**start
, **stop
, **oidp
;
229 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
232 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
234 for (oidp
= start
; oidp
< stop
; oidp
++)
235 sysctl_unregister_oid(*oidp
);
239 linker_load_file(const char* filename
, linker_file_t
* result
)
243 int foundfile
, error
= 0;
246 /* Refuse to load modules if securelevel raised */
247 if (securelevel
> 0 || kernel_mem_readonly
)
250 lf
= linker_find_file_by_name(filename
);
252 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename
));
257 if (find_mod_metadata(filename
)) {
258 if (linker_kernel_file
)
259 ++linker_kernel_file
->refs
;
260 *result
= linker_kernel_file
;
264 koname
= kmalloc(strlen(filename
) + 4, M_LINKER
, M_WAITOK
);
265 ksprintf(koname
, "%s.ko", filename
);
268 TAILQ_FOREACH(lc
, &classes
, link
) {
269 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
270 filename
, lc
->desc
));
272 error
= lc
->ops
->load_file(koname
, &lf
); /* First with .ko */
273 if (lf
== NULL
&& error
== ENOENT
)
274 error
= lc
->ops
->load_file(filename
, &lf
); /* Then try without */
276 * If we got something other than ENOENT, then it exists but we cannot
277 * load it for some other reason.
282 linker_file_register_sysctls(lf
);
283 error
= linker_file_sysinit(lf
);
290 * Less than ideal, but tells the user whether it failed to load or
291 * the module was not found.
295 * Format not recognized or otherwise unloadable.
296 * When loading a module that is statically built into
297 * the kernel EEXIST percolates back up as the return
298 * value. Preserve this so that apps can recognize this
304 error
= ENOENT
; /* Nothing found */
309 kfree(koname
, M_LINKER
);
314 linker_find_file_by_name(const char* filename
)
316 linker_file_t lf
= 0;
320 for (i
= strlen(filename
); i
> 0 && filename
[i
-1] != '/'; --i
)
324 koname
= kmalloc(strlen(filename
) + 4, M_LINKER
, M_WAITOK
);
325 ksprintf(koname
, "%s.ko", filename
);
327 lockmgr(&lock
, LK_SHARED
);
328 TAILQ_FOREACH(lf
, &linker_files
, link
) {
329 if (!strcmp(lf
->filename
, koname
))
331 if (!strcmp(lf
->filename
, filename
))
334 lockmgr(&lock
, LK_RELEASE
);
337 kfree(koname
, M_LINKER
);
342 linker_find_file_by_id(int fileid
)
344 linker_file_t lf
= 0;
346 lockmgr(&lock
, LK_SHARED
);
347 TAILQ_FOREACH(lf
, &linker_files
, link
)
348 if (lf
->id
== fileid
)
350 lockmgr(&lock
, LK_RELEASE
);
356 linker_make_file(const char* pathname
, void* priv
, struct linker_file_ops
* ops
)
358 linker_file_t lf
= 0;
360 const char *filename
;
362 filename
= rindex(pathname
, '/');
363 if (filename
&& filename
[1])
368 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename
));
369 lockmgr(&lock
, LK_EXCLUSIVE
);
370 namelen
= strlen(filename
) + 1;
371 lf
= kmalloc(sizeof(struct linker_file
) + namelen
, M_LINKER
, M_WAITOK
);
372 bzero(lf
, sizeof(*lf
));
377 lf
->filename
= (char*) (lf
+ 1);
378 strcpy(lf
->filename
, filename
);
379 lf
->id
= next_file_id
++;
382 STAILQ_INIT(&lf
->common
);
383 TAILQ_INIT(&lf
->modules
);
387 TAILQ_INSERT_TAIL(&linker_files
, lf
, link
);
389 lockmgr(&lock
, LK_RELEASE
);
394 linker_file_unload(linker_file_t file
)
397 struct common_symbol
* cp
;
401 /* Refuse to unload modules if securelevel raised */
402 if (securelevel
> 0 || kernel_mem_readonly
)
405 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file
->refs
));
406 lockmgr(&lock
, LK_EXCLUSIVE
);
407 if (file
->refs
== 1) {
408 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
410 * Temporarily bump file->refs to prevent recursive unloading
415 * Inform any modules associated with this file.
417 mod
= TAILQ_FIRST(&file
->modules
);
420 * Give the module a chance to veto the unload. Note that the
421 * act of unloading the module may cause other modules in the
422 * same file list to be unloaded recursively.
424 if ((error
= module_unload(mod
)) != 0) {
425 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
427 lockmgr(&lock
, LK_RELEASE
);
433 * Recursive relationships may prevent the release from
434 * immediately removing the module, or may remove other
435 * modules in the list.
437 next
= module_getfnext(mod
);
443 * Since we intend to destroy the file structure, we expect all
444 * modules to have been removed by now.
446 for (mod
= TAILQ_FIRST(&file
->modules
);
448 mod
= module_getfnext(mod
)
450 kprintf("linker_file_unload: module %p still has refs!\n", mod
);
456 if (file
->refs
> 0) {
457 lockmgr(&lock
, LK_RELEASE
);
461 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
462 if (file
->flags
& LINKER_FILE_LINKED
) {
463 linker_file_sysuninit(file
);
464 linker_file_unregister_sysctls(file
);
467 TAILQ_REMOVE(&linker_files
, file
, link
);
468 lockmgr(&lock
, LK_RELEASE
);
470 for (i
= 0; i
< file
->ndeps
; i
++)
471 linker_file_unload(file
->deps
[i
]);
472 kfree(file
->deps
, M_LINKER
);
474 for (cp
= STAILQ_FIRST(&file
->common
); cp
;
475 cp
= STAILQ_FIRST(&file
->common
)) {
476 STAILQ_REMOVE(&file
->common
, cp
, common_symbol
, link
);
480 file
->ops
->unload(file
);
481 kfree(file
, M_LINKER
);
488 linker_file_add_dependancy(linker_file_t file
, linker_file_t dep
)
490 linker_file_t
* newdeps
;
492 newdeps
= kmalloc((file
->ndeps
+ 1) * sizeof(linker_file_t
*),
493 M_LINKER
, M_WAITOK
| M_ZERO
);
496 bcopy(file
->deps
, newdeps
, file
->ndeps
* sizeof(linker_file_t
*));
497 kfree(file
->deps
, M_LINKER
);
499 file
->deps
= newdeps
;
500 file
->deps
[file
->ndeps
] = dep
;
505 * Locate a linker set and its contents.
506 * This is a helper function to avoid linker_if.h exposure elsewhere.
507 * Note: firstp and lastp are really void ***
510 linker_file_lookup_set(linker_file_t file
, const char *name
,
511 void *firstp
, void *lastp
, int *countp
)
513 return file
->ops
->lookup_set(file
, name
, firstp
, lastp
, countp
);
517 linker_file_lookup_symbol(linker_file_t file
, const char* name
, int deps
, caddr_t
*raddr
)
520 linker_symval_t symval
;
522 size_t common_size
= 0;
525 KLD_DPF(SYM
, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
528 if (file
->ops
->lookup_symbol(file
, name
, &sym
) == 0) {
529 file
->ops
->symbol_values(file
, sym
, &symval
);
532 * XXX Assume a common symbol if its value is 0 and it has a non-zero
533 * size, otherwise it could be an absolute symbol with a value of 0.
535 if (symval
.value
== 0 && symval
.size
!= 0) {
537 * For commons, first look them up in the dependancies and
538 * only allocate space if not found there.
540 common_size
= symval
.size
;
542 KLD_DPF(SYM
, ("linker_file_lookup_symbol: symbol.value=%x\n", symval
.value
));
543 *raddr
= symval
.value
;
548 for (i
= 0; i
< file
->ndeps
; i
++) {
549 if (linker_file_lookup_symbol(file
->deps
[i
], name
, 0, raddr
) == 0) {
550 KLD_DPF(SYM
, ("linker_file_lookup_symbol: deps value=%x\n", *raddr
));
555 /* If we have not found it in the dependencies, search globally */
556 TAILQ_FOREACH(lf
, &linker_files
, link
) {
557 /* But skip the current file if it's on the list */
560 /* And skip the files we searched above */
561 for (i
= 0; i
< file
->ndeps
; i
++)
562 if (lf
== file
->deps
[i
])
566 if (linker_file_lookup_symbol(lf
, name
, 0, raddr
) == 0) {
567 KLD_DPF(SYM
, ("linker_file_lookup_symbol: global value=%x\n", *raddr
));
573 if (common_size
> 0) {
575 * This is a common symbol which was not found in the
576 * dependancies. We maintain a simple common symbol table in
579 struct common_symbol
* cp
;
581 STAILQ_FOREACH(cp
, &file
->common
, link
)
582 if (!strcmp(cp
->name
, name
)) {
583 KLD_DPF(SYM
, ("linker_file_lookup_symbol: old common value=%x\n", cp
->address
));
584 *raddr
= cp
->address
;
589 * Round the symbol size up to align.
591 common_size
= (common_size
+ sizeof(int) - 1) & -sizeof(int);
592 cp
= kmalloc(sizeof(struct common_symbol
)
595 M_LINKER
, M_WAITOK
| M_ZERO
);
597 cp
->address
= (caddr_t
) (cp
+ 1);
598 cp
->name
= cp
->address
+ common_size
;
599 strcpy(cp
->name
, name
);
600 bzero(cp
->address
, common_size
);
601 STAILQ_INSERT_TAIL(&file
->common
, cp
, link
);
603 KLD_DPF(SYM
, ("linker_file_lookup_symbol: new common value=%x\n", cp
->address
));
604 *raddr
= cp
->address
;
608 #ifdef _KERNEL_VIRTUAL
609 *raddr
= dlsym(RTLD_NEXT
, name
);
610 if (*raddr
!= NULL
) {
611 KLD_DPF(SYM
, ("linker_file_lookup_symbol: found dlsym=%x\n", *raddr
));
616 KLD_DPF(SYM
, ("linker_file_lookup_symbol: fail\n"));
622 * DDB Helpers. DDB has to look across multiple files with their own
623 * symbol tables and string tables.
625 * Note that we do not obey list locking protocols here. We really don't
626 * need DDB to hang because somebody's got the lock held. We'll take the
627 * chance that the files list is inconsistant instead.
631 linker_ddb_lookup(const char *symstr
, c_linker_sym_t
*sym
)
635 TAILQ_FOREACH(lf
, &linker_files
, link
) {
636 if (lf
->ops
->lookup_symbol(lf
, symstr
, sym
) == 0)
643 linker_ddb_search_symbol(caddr_t value
, c_linker_sym_t
*sym
, long *diffp
)
646 u_long off
= (uintptr_t)value
;
647 u_long diff
, bestdiff
;
653 TAILQ_FOREACH(lf
, &linker_files
, link
) {
654 if (lf
->ops
->search_symbol(lf
, value
, &es
, &diff
) != 0)
656 if (es
!= 0 && diff
< bestdiff
) {
675 linker_ddb_symbol_values(c_linker_sym_t sym
, linker_symval_t
*symval
)
679 TAILQ_FOREACH(lf
, &linker_files
, link
) {
680 if (lf
->ops
->symbol_values(lf
, sym
, symval
) == 0)
693 sys_kldload(struct kldload_args
*uap
)
695 struct thread
*td
= curthread
;
696 char* filename
= NULL
, *modulename
;
700 uap
->sysmsg_result
= -1;
702 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
705 if ((error
= priv_check(td
, PRIV_KLD_LOAD
)) != 0)
708 filename
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
709 if ((error
= copyinstr(uap
->file
, filename
, MAXPATHLEN
, NULL
)) != 0)
712 /* Can't load more than one module with the same name */
713 modulename
= rindex(filename
, '/');
714 if (modulename
== NULL
)
715 modulename
= filename
;
718 if (linker_find_file_by_name(modulename
)) {
723 if ((error
= linker_load_file(filename
, &lf
)) != 0)
727 uap
->sysmsg_result
= lf
->id
;
731 kfree(filename
, M_TEMP
);
736 sys_kldunload(struct kldunload_args
*uap
)
738 struct thread
*td
= curthread
;
742 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
745 if ((error
= priv_check(td
, PRIV_KLD_UNLOAD
)) != 0)
748 lf
= linker_find_file_by_id(uap
->fileid
);
750 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf
->userrefs
));
751 if (lf
->userrefs
== 0) {
752 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
757 error
= linker_file_unload(lf
);
768 sys_kldfind(struct kldfind_args
*uap
)
770 char *filename
= NULL
, *modulename
;
774 uap
->sysmsg_result
= -1;
776 filename
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
777 if ((error
= copyinstr(uap
->file
, filename
, MAXPATHLEN
, NULL
)) != 0)
780 modulename
= rindex(filename
, '/');
781 if (modulename
== NULL
)
782 modulename
= filename
;
784 lf
= linker_find_file_by_name(modulename
);
786 uap
->sysmsg_result
= lf
->id
;
792 kfree(filename
, M_TEMP
);
797 sys_kldnext(struct kldnext_args
*uap
)
802 if (uap
->fileid
== 0) {
803 if (TAILQ_FIRST(&linker_files
))
804 uap
->sysmsg_result
= TAILQ_FIRST(&linker_files
)->id
;
806 uap
->sysmsg_result
= 0;
810 lf
= linker_find_file_by_id(uap
->fileid
);
812 if (TAILQ_NEXT(lf
, link
))
813 uap
->sysmsg_result
= TAILQ_NEXT(lf
, link
)->id
;
815 uap
->sysmsg_result
= 0;
823 sys_kldstat(struct kldstat_args
*uap
)
828 struct kld_file_stat
* stat
;
831 lf
= linker_find_file_by_id(uap
->fileid
);
840 * Check the version of the user's structure.
842 if ((error
= copyin(&stat
->version
, &version
, sizeof(version
))) != 0)
844 if (version
!= sizeof(struct kld_file_stat
)) {
849 namelen
= strlen(lf
->filename
) + 1;
850 if (namelen
> MAXPATHLEN
)
851 namelen
= MAXPATHLEN
;
852 if ((error
= copyout(lf
->filename
, &stat
->name
[0], namelen
)) != 0)
854 if ((error
= copyout(&lf
->refs
, &stat
->refs
, sizeof(int))) != 0)
856 if ((error
= copyout(&lf
->id
, &stat
->id
, sizeof(int))) != 0)
858 if ((error
= copyout(&lf
->address
, &stat
->address
, sizeof(caddr_t
))) != 0)
860 if ((error
= copyout(&lf
->size
, &stat
->size
, sizeof(size_t))) != 0)
863 uap
->sysmsg_result
= 0;
870 sys_kldfirstmod(struct kldfirstmod_args
*uap
)
875 lf
= linker_find_file_by_id(uap
->fileid
);
877 if (TAILQ_FIRST(&lf
->modules
))
878 uap
->sysmsg_result
= module_getid(TAILQ_FIRST(&lf
->modules
));
880 uap
->sysmsg_result
= 0;
888 sys_kldsym(struct kldsym_args
*uap
)
892 linker_symval_t symval
;
894 struct kld_sym_lookup lookup
;
897 if ((error
= copyin(uap
->data
, &lookup
, sizeof(lookup
))) != 0)
899 if (lookup
.version
!= sizeof(lookup
) || uap
->cmd
!= KLDSYM_LOOKUP
) {
904 symstr
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
905 if ((error
= copyinstr(lookup
.symname
, symstr
, MAXPATHLEN
, NULL
)) != 0)
908 if (uap
->fileid
!= 0) {
909 lf
= linker_find_file_by_id(uap
->fileid
);
914 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
915 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
916 lookup
.symvalue
= (uintptr_t)symval
.value
;
917 lookup
.symsize
= symval
.size
;
918 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
922 TAILQ_FOREACH(lf
, &linker_files
, link
) {
923 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
924 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
925 lookup
.symvalue
= (uintptr_t)symval
.value
;
926 lookup
.symsize
= symval
.size
;
927 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
936 kfree(symstr
, M_TEMP
);
941 * Look for module metadata in the static kernel
943 struct mod_metadata
*
944 find_mod_metadata(const char *modname
)
947 struct mod_metadata
**mdp
;
948 struct mod_metadata
*mdt
;
951 * Strip path prefixes and any dot extension. MDT_MODULE names
952 * are just the module name without a path or ".ko".
954 for (len
= strlen(modname
) - 1; len
>= 0; --len
) {
955 if (modname
[len
] == '/')
959 for (len
= 0; modname
[len
] && modname
[len
] != '.'; ++len
)
963 * Look for the module declaration
965 SET_FOREACH(mdp
, modmetadata_set
) {
967 if (mdt
->md_type
!= MDT_MODULE
)
969 if (strlen(mdt
->md_cval
) == len
&&
970 strncmp(mdt
->md_cval
, modname
, len
) == 0) {
978 * Preloaded module support
981 linker_preload(void* arg
)
989 struct sysinit
**sipp
;
990 const moduledata_t
*moddata
;
991 struct sysinit
**si_start
, **si_stop
;
994 while ((modptr
= preload_search_next_name(modptr
)) != NULL
) {
995 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
996 modtype
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
997 if (modname
== NULL
) {
998 kprintf("Preloaded module at %p does not have a name!\n", modptr
);
1001 if (modtype
== NULL
) {
1002 kprintf("Preloaded module at %p does not have a type!\n", modptr
);
1007 * This is a hack at the moment, but what's in FreeBSD-5 is even
1008 * worse so I'd rather the hack.
1010 kprintf("Preloaded %s \"%s\" at %p", modtype
, modname
, modptr
);
1011 if (find_mod_metadata(modname
)) {
1012 kprintf(" (ignored, already in static kernel)\n");
1017 lf
= linker_find_file_by_name(modname
);
1024 TAILQ_FOREACH(lc
, &classes
, link
) {
1025 error
= lc
->ops
->load_file(modname
, &lf
);
1034 if (linker_file_lookup_set(lf
, "sysinit_set", &si_start
, &si_stop
, NULL
) == 0) {
1036 * This is to set the sysinit moduledata so that the module
1037 * can attach itself to the correct containing file.
1038 * The sysinit could be run at *any* time.
1040 for (sipp
= si_start
; sipp
< si_stop
; sipp
++) {
1041 if ((*sipp
)->func
== module_register_init
) {
1042 moddata
= (*sipp
)->udata
;
1043 error
= module_register(moddata
, lf
);
1045 kprintf("Preloaded %s \"%s\" failed to register: %d\n",
1046 modtype
, modname
, error
);
1049 sysinit_add(si_start
, si_stop
);
1051 linker_file_register_sysctls(lf
);
1056 SYSINIT(preload
, SI_BOOT2_KLD
, SI_ORDER_MIDDLE
, linker_preload
, 0);
1059 * Search for a not-loaded module by name.
1061 * Modules may be found in the following locations:
1063 * - preloaded (result is just the module name)
1064 * - on disk (result is full path to module)
1066 * If the module name is qualified in any way (contains path, etc.)
1067 * the we simply return a copy of it.
1069 * The search path can be manipulated via sysctl. Note that we use the ';'
1070 * character as a separator to be consistent with the bootloader.
1073 static char linker_path
[MAXPATHLEN
] = "/boot;/boot/modules;/;/modules";
1075 SYSCTL_STRING(_kern
, OID_AUTO
, module_path
, CTLFLAG_RW
, linker_path
,
1076 sizeof(linker_path
), "module load search path");
1077 TUNABLE_STR("module_path", linker_path
, sizeof(linker_path
));
1080 linker_strdup(const char *str
)
1084 result
= kmalloc(strlen(str
) + 1, M_LINKER
, M_WAITOK
);
1085 strcpy(result
, str
);
1090 linker_search_path(const char *name
)
1092 struct nlookupdata nd
;
1093 char *cp
, *ep
, *result
;
1094 size_t name_len
, prefix_len
;
1099 /* qualified at all? */
1100 if (index(name
, '/'))
1101 return(linker_strdup(name
));
1103 /* traverse the linker path */
1105 name_len
= strlen(name
);
1108 /* find the end of this component */
1109 for (ep
= cp
; (*ep
!= 0) && (*ep
!= ';'); ep
++)
1111 prefix_len
= ep
- cp
;
1112 /* if this component doesn't end with a slash, add one */
1113 if (ep
== cp
|| *(ep
- 1) != '/')
1119 * +2 : possible separator, plus terminator.
1121 result
= kmalloc(prefix_len
+ name_len
+ 2, M_LINKER
, M_WAITOK
);
1123 strncpy(result
, cp
, prefix_len
);
1125 result
[prefix_len
++] = '/';
1126 strcpy(result
+ prefix_len
, name
);
1129 * Attempt to open the file, and return the path if we succeed and it's
1132 error
= nlookup_init(&nd
, result
, UIO_SYSSPACE
, NLC_FOLLOW
|NLC_LOCKVP
);
1134 error
= vn_open(&nd
, NULL
, FREAD
, 0);
1136 type
= nd
.nl_open_vp
->v_type
;
1143 kfree(result
, M_LINKER
);