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.38 2007/06/07 22:58:11 corecode 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>
40 #include <sys/module.h>
41 #include <sys/linker.h>
42 #include <sys/fcntl.h>
43 #include <sys/libkern.h>
44 #include <sys/nlookup.h>
45 #include <sys/vnode.h>
46 #include <sys/sysctl.h>
48 #include <vm/vm_zone.h>
50 #ifdef _KERNEL_VIRTUAL
58 /* Metadata from the static kernel */
59 SET_DECLARE(modmetadata_set
, struct mod_metadata
);
60 MALLOC_DEFINE(M_LINKER
, "kld", "kernel linker");
62 linker_file_t linker_current_file
;
63 linker_file_t linker_kernel_file
;
65 static struct lock lock
; /* lock for the file list */
66 static linker_class_list_t classes
;
67 static linker_file_list_t linker_files
;
68 static int next_file_id
= 1;
71 linker_init(void* arg
)
73 lockinit(&lock
, "klink", 0, 0);
75 TAILQ_INIT(&linker_files
);
78 SYSINIT(linker
, SI_BOOT2_KLD
, SI_ORDER_FIRST
, linker_init
, 0);
81 linker_add_class(const char* desc
, void* priv
,
82 struct linker_class_ops
* ops
)
86 lc
= kmalloc(sizeof(struct linker_class
), M_LINKER
, M_NOWAIT
);
89 bzero(lc
, sizeof(*lc
));
94 TAILQ_INSERT_HEAD(&classes
, lc
, link
);
100 linker_file_sysinit(linker_file_t lf
)
102 struct sysinit
** start
, ** stop
;
103 struct sysinit
** sipp
;
104 struct sysinit
** xipp
;
105 struct sysinit
* save
;
106 const moduledata_t
*moddata
;
109 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
112 if (linker_file_lookup_set(lf
, "sysinit_set", &start
, &stop
, NULL
) != 0)
113 return 0; /* XXX is this correct ? No sysinit ? */
116 for (sipp
= start
; sipp
< stop
; sipp
++) {
117 if ((*sipp
)->func
== module_register_init
) {
118 moddata
= (*sipp
)->udata
;
119 error
= module_register(moddata
, lf
);
121 kprintf("linker_file_sysinit \"%s\" failed to register! %d\n",
122 lf
->filename
, error
);
129 * Perform a bubble sort of the system initialization objects by
130 * their subsystem (primary key) and order (secondary key).
132 * Since some things care about execution order, this is the
133 * operation which ensures continued function.
135 for (sipp
= start
; sipp
< stop
; sipp
++) {
136 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
137 if ((*sipp
)->subsystem
< (*xipp
)->subsystem
||
138 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
139 (*sipp
)->order
<= (*xipp
)->order
))
149 * Traverse the (now) ordered list of system initialization tasks.
150 * Perform each task, and continue on to the next task.
152 for (sipp
= start
; sipp
< stop
; sipp
++) {
153 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
154 continue; /* skip dummy task(s)*/
157 (*((*sipp
)->func
))((*sipp
)->udata
);
159 return 0; /* no errors */
163 linker_file_sysuninit(linker_file_t lf
)
165 struct sysinit
** start
, ** stop
;
166 struct sysinit
** sipp
;
167 struct sysinit
** xipp
;
168 struct sysinit
* save
;
170 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
173 if (linker_file_lookup_set(lf
, "sysuninit_set", &start
, &stop
, NULL
) != 0)
177 * Perform a reverse bubble sort of the system initialization objects
178 * by their subsystem (primary key) and order (secondary key).
180 * Since some things care about execution order, this is the
181 * operation which ensures continued function.
183 for (sipp
= start
; sipp
< stop
; sipp
++) {
184 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
185 if ((*sipp
)->subsystem
> (*xipp
)->subsystem
||
186 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
187 (*sipp
)->order
>= (*xipp
)->order
))
197 * Traverse the (now) ordered list of system initialization tasks.
198 * Perform each task, and continue on to the next task.
200 for (sipp
= start
; sipp
< stop
; sipp
++) {
201 if ((*sipp
)->subsystem
== SI_SPECIAL_DUMMY
)
202 continue; /* skip dummy task(s)*/
205 (*((*sipp
)->func
))((*sipp
)->udata
);
210 linker_file_register_sysctls(linker_file_t lf
)
212 struct sysctl_oid
**start
, **stop
, **oidp
;
214 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
217 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
219 for (oidp
= start
; oidp
< stop
; oidp
++)
220 sysctl_register_oid(*oidp
);
224 linker_file_unregister_sysctls(linker_file_t lf
)
226 struct sysctl_oid
**start
, **stop
, **oidp
;
228 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
231 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
233 for (oidp
= start
; oidp
< stop
; oidp
++)
234 sysctl_unregister_oid(*oidp
);
238 linker_load_file(const char* filename
, linker_file_t
* result
)
242 int foundfile
, error
= 0;
245 /* Refuse to load modules if securelevel raised */
246 if (securelevel
> 0 || kernel_mem_readonly
)
249 lf
= linker_find_file_by_name(filename
);
251 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename
));
256 if (find_mod_metadata(filename
)) {
257 if (linker_kernel_file
)
258 ++linker_kernel_file
->refs
;
259 *result
= linker_kernel_file
;
263 koname
= kmalloc(strlen(filename
) + 4, M_LINKER
, M_WAITOK
);
264 if (koname
== NULL
) {
268 ksprintf(koname
, "%s.ko", filename
);
271 for (lc
= TAILQ_FIRST(&classes
); lc
; lc
= TAILQ_NEXT(lc
, link
)) {
272 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
273 filename
, lc
->desc
));
275 error
= lc
->ops
->load_file(koname
, &lf
); /* First with .ko */
276 if (lf
== NULL
&& error
== ENOENT
)
277 error
= lc
->ops
->load_file(filename
, &lf
); /* Then try without */
279 * If we got something other than ENOENT, then it exists but we cannot
280 * load it for some other reason.
285 linker_file_register_sysctls(lf
);
286 error
= linker_file_sysinit(lf
);
293 * Less than ideal, but tells the user whether it failed to load or
294 * the module was not found.
298 * Format not recognized or otherwise unloadable.
299 * When loading a module that is statically built into
300 * the kernel EEXIST percolates back up as the return
301 * value. Preserve this so that apps like sysinstall
302 * can recognize this special case and not post bogus
308 error
= ENOENT
; /* Nothing found */
313 kfree(koname
, M_LINKER
);
318 linker_find_file_by_name(const char* filename
)
320 linker_file_t lf
= 0;
324 for (i
= strlen(filename
); i
> 0 && filename
[i
-1] != '/'; --i
)
328 koname
= kmalloc(strlen(filename
) + 4, M_LINKER
, M_WAITOK
);
331 ksprintf(koname
, "%s.ko", filename
);
333 lockmgr(&lock
, LK_SHARED
);
334 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
335 if (!strcmp(lf
->filename
, koname
))
337 if (!strcmp(lf
->filename
, filename
))
340 lockmgr(&lock
, LK_RELEASE
);
344 kfree(koname
, M_LINKER
);
349 linker_find_file_by_id(int fileid
)
351 linker_file_t lf
= 0;
353 lockmgr(&lock
, LK_SHARED
);
354 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
))
355 if (lf
->id
== fileid
)
357 lockmgr(&lock
, LK_RELEASE
);
363 linker_make_file(const char* pathname
, void* priv
, struct linker_file_ops
* ops
)
365 linker_file_t lf
= 0;
367 const char *filename
;
369 filename
= rindex(pathname
, '/');
370 if (filename
&& filename
[1])
375 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename
));
376 lockmgr(&lock
, LK_EXCLUSIVE
);
377 namelen
= strlen(filename
) + 1;
378 lf
= kmalloc(sizeof(struct linker_file
) + namelen
, M_LINKER
, M_WAITOK
);
381 bzero(lf
, sizeof(*lf
));
386 lf
->filename
= (char*) (lf
+ 1);
387 strcpy(lf
->filename
, filename
);
388 lf
->id
= next_file_id
++;
391 STAILQ_INIT(&lf
->common
);
392 TAILQ_INIT(&lf
->modules
);
396 TAILQ_INSERT_TAIL(&linker_files
, lf
, link
);
399 lockmgr(&lock
, LK_RELEASE
);
404 linker_file_unload(linker_file_t file
)
407 struct common_symbol
* cp
;
411 /* Refuse to unload modules if securelevel raised */
412 if (securelevel
> 0 || kernel_mem_readonly
)
415 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file
->refs
));
416 lockmgr(&lock
, LK_EXCLUSIVE
);
417 if (file
->refs
== 1) {
418 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
420 * Temporarily bump file->refs to prevent recursive unloading
425 * Inform any modules associated with this file.
427 mod
= TAILQ_FIRST(&file
->modules
);
430 * Give the module a chance to veto the unload. Note that the
431 * act of unloading the module may cause other modules in the
432 * same file list to be unloaded recursively.
434 if ((error
= module_unload(mod
)) != 0) {
435 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
437 lockmgr(&lock
, LK_RELEASE
);
443 * Recursive relationships may prevent the release from
444 * immediately removing the module, or may remove other
445 * modules in the list.
447 next
= module_getfnext(mod
);
453 * Since we intend to destroy the file structure, we expect all
454 * modules to have been removed by now.
456 for (mod
= TAILQ_FIRST(&file
->modules
);
458 mod
= module_getfnext(mod
)
460 kprintf("linker_file_unload: module %p still has refs!\n", mod
);
466 if (file
->refs
> 0) {
467 lockmgr(&lock
, LK_RELEASE
);
471 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
472 if (file
->flags
& LINKER_FILE_LINKED
) {
473 linker_file_sysuninit(file
);
474 linker_file_unregister_sysctls(file
);
477 TAILQ_REMOVE(&linker_files
, file
, link
);
478 lockmgr(&lock
, LK_RELEASE
);
480 for (i
= 0; i
< file
->ndeps
; i
++)
481 linker_file_unload(file
->deps
[i
]);
482 kfree(file
->deps
, M_LINKER
);
484 for (cp
= STAILQ_FIRST(&file
->common
); cp
;
485 cp
= STAILQ_FIRST(&file
->common
)) {
486 STAILQ_REMOVE(&file
->common
, cp
, common_symbol
, link
);
490 file
->ops
->unload(file
);
491 kfree(file
, M_LINKER
);
498 linker_file_add_dependancy(linker_file_t file
, linker_file_t dep
)
500 linker_file_t
* newdeps
;
502 newdeps
= kmalloc((file
->ndeps
+ 1) * sizeof(linker_file_t
*),
506 bzero(newdeps
, (file
->ndeps
+ 1) * sizeof(linker_file_t
*));
509 bcopy(file
->deps
, newdeps
, file
->ndeps
* sizeof(linker_file_t
*));
510 kfree(file
->deps
, M_LINKER
);
512 file
->deps
= newdeps
;
513 file
->deps
[file
->ndeps
] = dep
;
520 * Locate a linker set and its contents.
521 * This is a helper function to avoid linker_if.h exposure elsewhere.
522 * Note: firstp and lastp are really void ***
525 linker_file_lookup_set(linker_file_t file
, const char *name
,
526 void *firstp
, void *lastp
, int *countp
)
528 return file
->ops
->lookup_set(file
, name
, firstp
, lastp
, countp
);
532 linker_file_lookup_symbol(linker_file_t file
, const char* name
, int deps
, caddr_t
*raddr
)
535 linker_symval_t symval
;
537 size_t common_size
= 0;
540 KLD_DPF(SYM
, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
543 if (file
->ops
->lookup_symbol(file
, name
, &sym
) == 0) {
544 file
->ops
->symbol_values(file
, sym
, &symval
);
547 * XXX Assume a common symbol if its value is 0 and it has a non-zero
548 * size, otherwise it could be an absolute symbol with a value of 0.
550 if (symval
.value
== 0 && symval
.size
!= 0) {
552 * For commons, first look them up in the dependancies and
553 * only allocate space if not found there.
555 common_size
= symval
.size
;
557 KLD_DPF(SYM
, ("linker_file_lookup_symbol: symbol.value=%x\n", symval
.value
));
558 *raddr
= symval
.value
;
563 for (i
= 0; i
< file
->ndeps
; i
++) {
564 if (linker_file_lookup_symbol(file
->deps
[i
], name
, 0, raddr
) == 0) {
565 KLD_DPF(SYM
, ("linker_file_lookup_symbol: deps value=%x\n", *raddr
));
570 /* If we have not found it in the dependencies, search globally */
571 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
572 /* But skip the current file if it's on the list */
575 /* And skip the files we searched above */
576 for (i
= 0; i
< file
->ndeps
; i
++)
577 if (lf
== file
->deps
[i
])
581 if (linker_file_lookup_symbol(lf
, name
, 0, raddr
) == 0) {
582 KLD_DPF(SYM
, ("linker_file_lookup_symbol: global value=%x\n", *raddr
));
588 if (common_size
> 0) {
590 * This is a common symbol which was not found in the
591 * dependancies. We maintain a simple common symbol table in
594 struct common_symbol
* cp
;
596 for (cp
= STAILQ_FIRST(&file
->common
); cp
;
597 cp
= STAILQ_NEXT(cp
, link
))
598 if (!strcmp(cp
->name
, name
)) {
599 KLD_DPF(SYM
, ("linker_file_lookup_symbol: old common value=%x\n", cp
->address
));
600 *raddr
= cp
->address
;
605 * Round the symbol size up to align.
607 common_size
= (common_size
+ sizeof(int) - 1) & -sizeof(int);
608 cp
= kmalloc(sizeof(struct common_symbol
)
613 KLD_DPF(SYM
, ("linker_file_lookup_symbol: nomem\n"));
616 bzero(cp
, sizeof(struct common_symbol
) + common_size
+ strlen(name
)+ 1);
618 cp
->address
= (caddr_t
) (cp
+ 1);
619 cp
->name
= cp
->address
+ common_size
;
620 strcpy(cp
->name
, name
);
621 bzero(cp
->address
, common_size
);
622 STAILQ_INSERT_TAIL(&file
->common
, cp
, link
);
624 KLD_DPF(SYM
, ("linker_file_lookup_symbol: new common value=%x\n", cp
->address
));
625 *raddr
= cp
->address
;
629 #ifdef _KERNEL_VIRTUAL
630 *raddr
= dlsym(RTLD_NEXT
, name
);
631 if (*raddr
!= NULL
) {
632 KLD_DPF(SYM
, ("linker_file_lookup_symbol: found dlsym=%x\n", *raddr
));
637 KLD_DPF(SYM
, ("linker_file_lookup_symbol: fail\n"));
643 * DDB Helpers. DDB has to look across multiple files with their own
644 * symbol tables and string tables.
646 * Note that we do not obey list locking protocols here. We really don't
647 * need DDB to hang because somebody's got the lock held. We'll take the
648 * chance that the files list is inconsistant instead.
652 linker_ddb_lookup(const char *symstr
, c_linker_sym_t
*sym
)
656 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
657 if (lf
->ops
->lookup_symbol(lf
, symstr
, sym
) == 0)
664 linker_ddb_search_symbol(caddr_t value
, c_linker_sym_t
*sym
, long *diffp
)
667 u_long off
= (uintptr_t)value
;
668 u_long diff
, bestdiff
;
674 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
675 if (lf
->ops
->search_symbol(lf
, value
, &es
, &diff
) != 0)
677 if (es
!= 0 && diff
< bestdiff
) {
696 linker_ddb_symbol_values(c_linker_sym_t sym
, linker_symval_t
*symval
)
700 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
701 if (lf
->ops
->symbol_values(lf
, sym
, symval
) == 0)
714 sys_kldload(struct kldload_args
*uap
)
716 struct thread
*td
= curthread
;
717 char* filename
= NULL
, *modulename
;
721 uap
->sysmsg_result
= -1;
723 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
726 if ((error
= suser(td
)) != 0)
729 filename
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
730 if ((error
= copyinstr(uap
->file
, filename
, MAXPATHLEN
, NULL
)) != 0)
733 /* Can't load more than one module with the same name */
734 modulename
= rindex(filename
, '/');
735 if (modulename
== NULL
)
736 modulename
= filename
;
739 if (linker_find_file_by_name(modulename
)) {
744 if ((error
= linker_load_file(filename
, &lf
)) != 0)
748 uap
->sysmsg_result
= lf
->id
;
752 kfree(filename
, M_TEMP
);
757 sys_kldunload(struct kldunload_args
*uap
)
759 struct thread
*td
= curthread
;
763 if (securelevel
> 0 || kernel_mem_readonly
) /* redundant, but that's OK */
766 if ((error
= suser(td
)) != 0)
769 lf
= linker_find_file_by_id(uap
->fileid
);
771 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf
->userrefs
));
772 if (lf
->userrefs
== 0) {
773 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
778 error
= linker_file_unload(lf
);
789 sys_kldfind(struct kldfind_args
*uap
)
791 char *filename
= NULL
, *modulename
;
795 uap
->sysmsg_result
= -1;
797 filename
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
798 if ((error
= copyinstr(uap
->file
, filename
, MAXPATHLEN
, NULL
)) != 0)
801 modulename
= rindex(filename
, '/');
802 if (modulename
== NULL
)
803 modulename
= filename
;
805 lf
= linker_find_file_by_name(modulename
);
807 uap
->sysmsg_result
= lf
->id
;
813 kfree(filename
, M_TEMP
);
818 sys_kldnext(struct kldnext_args
*uap
)
823 if (uap
->fileid
== 0) {
824 if (TAILQ_FIRST(&linker_files
))
825 uap
->sysmsg_result
= TAILQ_FIRST(&linker_files
)->id
;
827 uap
->sysmsg_result
= 0;
831 lf
= linker_find_file_by_id(uap
->fileid
);
833 if (TAILQ_NEXT(lf
, link
))
834 uap
->sysmsg_result
= TAILQ_NEXT(lf
, link
)->id
;
836 uap
->sysmsg_result
= 0;
844 sys_kldstat(struct kldstat_args
*uap
)
849 struct kld_file_stat
* stat
;
852 lf
= linker_find_file_by_id(uap
->fileid
);
861 * Check the version of the user's structure.
863 if ((error
= copyin(&stat
->version
, &version
, sizeof(version
))) != 0)
865 if (version
!= sizeof(struct kld_file_stat
)) {
870 namelen
= strlen(lf
->filename
) + 1;
871 if (namelen
> MAXPATHLEN
)
872 namelen
= MAXPATHLEN
;
873 if ((error
= copyout(lf
->filename
, &stat
->name
[0], namelen
)) != 0)
875 if ((error
= copyout(&lf
->refs
, &stat
->refs
, sizeof(int))) != 0)
877 if ((error
= copyout(&lf
->id
, &stat
->id
, sizeof(int))) != 0)
879 if ((error
= copyout(&lf
->address
, &stat
->address
, sizeof(caddr_t
))) != 0)
881 if ((error
= copyout(&lf
->size
, &stat
->size
, sizeof(size_t))) != 0)
884 uap
->sysmsg_result
= 0;
891 sys_kldfirstmod(struct kldfirstmod_args
*uap
)
896 lf
= linker_find_file_by_id(uap
->fileid
);
898 if (TAILQ_FIRST(&lf
->modules
))
899 uap
->sysmsg_result
= module_getid(TAILQ_FIRST(&lf
->modules
));
901 uap
->sysmsg_result
= 0;
909 sys_kldsym(struct kldsym_args
*uap
)
913 linker_symval_t symval
;
915 struct kld_sym_lookup lookup
;
918 if ((error
= copyin(uap
->data
, &lookup
, sizeof(lookup
))) != 0)
920 if (lookup
.version
!= sizeof(lookup
) || uap
->cmd
!= KLDSYM_LOOKUP
) {
925 symstr
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
926 if ((error
= copyinstr(lookup
.symname
, symstr
, MAXPATHLEN
, NULL
)) != 0)
929 if (uap
->fileid
!= 0) {
930 lf
= linker_find_file_by_id(uap
->fileid
);
935 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
936 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
937 lookup
.symvalue
= (uintptr_t)symval
.value
;
938 lookup
.symsize
= symval
.size
;
939 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
943 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
944 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
945 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
946 lookup
.symvalue
= (uintptr_t)symval
.value
;
947 lookup
.symsize
= symval
.size
;
948 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
957 kfree(symstr
, M_TEMP
);
962 * Look for module metadata in the static kernel
964 struct mod_metadata
*
965 find_mod_metadata(const char *modname
)
968 struct mod_metadata
**mdp
;
969 struct mod_metadata
*mdt
;
972 * Strip path prefixes and any dot extension. MDT_MODULE names
973 * are just the module name without a path or ".ko".
975 for (len
= strlen(modname
) - 1; len
>= 0; --len
) {
976 if (modname
[len
] == '/')
980 for (len
= 0; modname
[len
] && modname
[len
] != '.'; ++len
)
984 * Look for the module declaration
986 SET_FOREACH(mdp
, modmetadata_set
) {
988 if (mdt
->md_type
!= MDT_MODULE
)
990 if (strlen(mdt
->md_cval
) == len
&&
991 strncmp(mdt
->md_cval
, modname
, len
) == 0) {
999 * Preloaded module support
1002 linker_preload(void* arg
)
1010 struct sysinit
**sipp
;
1011 const moduledata_t
*moddata
;
1012 struct sysinit
**si_start
, **si_stop
;
1015 while ((modptr
= preload_search_next_name(modptr
)) != NULL
) {
1016 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
1017 modtype
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
1018 if (modname
== NULL
) {
1019 kprintf("Preloaded module at %p does not have a name!\n", modptr
);
1022 if (modtype
== NULL
) {
1023 kprintf("Preloaded module at %p does not have a type!\n", modptr
);
1028 * This is a hack at the moment, but what's in FreeBSD-5 is even
1029 * worse so I'd rather the hack.
1031 kprintf("Preloaded %s \"%s\" at %p", modtype
, modname
, modptr
);
1032 if (find_mod_metadata(modname
)) {
1033 kprintf(" (ignored, already in static kernel)\n");
1038 lf
= linker_find_file_by_name(modname
);
1045 for (lc
= TAILQ_FIRST(&classes
); lc
; lc
= TAILQ_NEXT(lc
, link
)) {
1046 error
= lc
->ops
->load_file(modname
, &lf
);
1055 if (linker_file_lookup_set(lf
, "sysinit_set", &si_start
, &si_stop
, NULL
) == 0) {
1057 * This is to set the sysinit moduledata so that the module
1058 * can attach itself to the correct containing file.
1059 * The sysinit could be run at *any* time.
1061 for (sipp
= si_start
; sipp
< si_stop
; sipp
++) {
1062 if ((*sipp
)->func
== module_register_init
) {
1063 moddata
= (*sipp
)->udata
;
1064 error
= module_register(moddata
, lf
);
1066 kprintf("Preloaded %s \"%s\" failed to register: %d\n",
1067 modtype
, modname
, error
);
1070 sysinit_add(si_start
, si_stop
);
1072 linker_file_register_sysctls(lf
);
1077 SYSINIT(preload
, SI_BOOT2_KLD
, SI_ORDER_MIDDLE
, linker_preload
, 0);
1080 * Search for a not-loaded module by name.
1082 * Modules may be found in the following locations:
1084 * - preloaded (result is just the module name)
1085 * - on disk (result is full path to module)
1087 * If the module name is qualified in any way (contains path, etc.)
1088 * the we simply return a copy of it.
1090 * The search path can be manipulated via sysctl. Note that we use the ';'
1091 * character as a separator to be consistent with the bootloader.
1094 static char linker_path
[MAXPATHLEN
] = "/;/boot;/modules";
1096 SYSCTL_STRING(_kern
, OID_AUTO
, module_path
, CTLFLAG_RW
, linker_path
,
1097 sizeof(linker_path
), "module load search path");
1098 TUNABLE_STR("module_path", linker_path
, sizeof(linker_path
));
1101 linker_strdup(const char *str
)
1105 if ((result
= kmalloc((strlen(str
) + 1), M_LINKER
, M_WAITOK
)) != NULL
)
1106 strcpy(result
, str
);
1111 linker_search_path(const char *name
)
1113 struct nlookupdata nd
;
1114 char *cp
, *ep
, *result
;
1115 size_t name_len
, prefix_len
;
1120 /* qualified at all? */
1121 if (index(name
, '/'))
1122 return(linker_strdup(name
));
1124 /* traverse the linker path */
1126 name_len
= strlen(name
);
1129 /* find the end of this component */
1130 for (ep
= cp
; (*ep
!= 0) && (*ep
!= ';'); ep
++)
1132 prefix_len
= ep
- cp
;
1133 /* if this component doesn't end with a slash, add one */
1134 if (ep
== cp
|| *(ep
- 1) != '/')
1140 * +2 : possible separator, plus terminator.
1142 result
= kmalloc(prefix_len
+ name_len
+ 2, M_LINKER
, M_WAITOK
);
1144 strncpy(result
, cp
, prefix_len
);
1146 result
[prefix_len
++] = '/';
1147 strcpy(result
+ prefix_len
, name
);
1150 * Attempt to open the file, and return the path if we succeed and it's
1153 error
= nlookup_init(&nd
, result
, UIO_SYSSPACE
, NLC_FOLLOW
|NLC_LOCKVP
);
1155 error
= vn_open(&nd
, NULL
, FREAD
, 0);
1157 type
= nd
.nl_open_vp
->v_type
;
1164 kfree(result
, M_LINKER
);