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.21 2005/03/29 00:35:55 drhodus 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>
54 /* Metadata from the static kernel */
55 SET_DECLARE(modmetadata_set
, struct mod_metadata
);
56 MALLOC_DEFINE(M_LINKER
, "kld", "kernel linker");
58 linker_file_t linker_current_file
;
59 linker_file_t linker_kernel_file
;
61 static struct lock lock
; /* lock for the file list */
62 static linker_class_list_t classes
;
63 static linker_file_list_t linker_files
;
64 static int next_file_id
= 1;
67 linker_init(void* arg
)
69 lockinit(&lock
, 0, "klink", 0, 0);
71 TAILQ_INIT(&linker_files
);
74 SYSINIT(linker
, SI_SUB_KLD
, SI_ORDER_FIRST
, linker_init
, 0);
77 linker_add_class(const char* desc
, void* priv
,
78 struct linker_class_ops
* ops
)
82 lc
= malloc(sizeof(struct linker_class
), M_LINKER
, M_NOWAIT
);
85 bzero(lc
, sizeof(*lc
));
90 TAILQ_INSERT_HEAD(&classes
, lc
, link
);
96 linker_file_sysinit(linker_file_t lf
)
98 struct sysinit
** start
, ** stop
;
99 struct sysinit
** sipp
;
100 struct sysinit
** xipp
;
101 struct sysinit
* save
;
102 const moduledata_t
*moddata
;
105 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
108 if (linker_file_lookup_set(lf
, "sysinit_set", &start
, &stop
, NULL
) != 0)
109 return 0; /* XXX is this correct ? No sysinit ? */
112 for (sipp
= start
; sipp
< stop
; sipp
++) {
113 if ((*sipp
)->func
== module_register_init
) {
114 moddata
= (*sipp
)->udata
;
115 error
= module_register(moddata
, lf
);
117 printf("linker_file_sysinit \"%s\" failed to register! %d\n",
118 lf
->filename
, error
);
125 * Perform a bubble sort of the system initialization objects by
126 * their subsystem (primary key) and order (secondary key).
128 * Since some things care about execution order, this is the
129 * operation which ensures continued function.
131 for (sipp
= start
; sipp
< stop
; sipp
++) {
132 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
133 if ((*sipp
)->subsystem
< (*xipp
)->subsystem
||
134 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
135 (*sipp
)->order
<= (*xipp
)->order
))
145 * Traverse the (now) ordered list of system initialization tasks.
146 * Perform each task, and continue on to the next task.
148 for (sipp
= start
; sipp
< stop
; sipp
++) {
149 if ((*sipp
)->subsystem
== SI_SUB_DUMMY
)
150 continue; /* skip dummy task(s)*/
153 (*((*sipp
)->func
))((*sipp
)->udata
);
155 return 0; /* no errors */
159 linker_file_sysuninit(linker_file_t lf
)
161 struct sysinit
** start
, ** stop
;
162 struct sysinit
** sipp
;
163 struct sysinit
** xipp
;
164 struct sysinit
* save
;
166 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
169 if (linker_file_lookup_set(lf
, "sysuninit_set", &start
, &stop
, NULL
) != 0)
173 * Perform a reverse bubble sort of the system initialization objects
174 * by their subsystem (primary key) and order (secondary key).
176 * Since some things care about execution order, this is the
177 * operation which ensures continued function.
179 for (sipp
= start
; sipp
< stop
; sipp
++) {
180 for (xipp
= sipp
+ 1; xipp
< stop
; xipp
++) {
181 if ((*sipp
)->subsystem
> (*xipp
)->subsystem
||
182 ((*sipp
)->subsystem
== (*xipp
)->subsystem
&&
183 (*sipp
)->order
>= (*xipp
)->order
))
193 * Traverse the (now) ordered list of system initialization tasks.
194 * Perform each task, and continue on to the next task.
196 for (sipp
= start
; sipp
< stop
; sipp
++) {
197 if ((*sipp
)->subsystem
== SI_SUB_DUMMY
)
198 continue; /* skip dummy task(s)*/
201 (*((*sipp
)->func
))((*sipp
)->udata
);
206 linker_file_register_sysctls(linker_file_t lf
)
208 struct sysctl_oid
**start
, **stop
, **oidp
;
210 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
213 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
215 for (oidp
= start
; oidp
< stop
; oidp
++)
216 sysctl_register_oid(*oidp
);
220 linker_file_unregister_sysctls(linker_file_t lf
)
222 struct sysctl_oid
**start
, **stop
, **oidp
;
224 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
227 if (linker_file_lookup_set(lf
, "sysctl_set", &start
, &stop
, NULL
) != 0)
229 for (oidp
= start
; oidp
< stop
; oidp
++)
230 sysctl_unregister_oid(*oidp
);
234 linker_load_file(const char* filename
, linker_file_t
* result
)
238 int foundfile
, error
= 0;
241 /* Refuse to load modules if securelevel raised */
245 lf
= linker_find_file_by_name(filename
);
247 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename
));
252 if (find_mod_metadata(filename
)) {
253 if (linker_kernel_file
)
254 ++linker_kernel_file
->refs
;
255 *result
= linker_kernel_file
;
259 koname
= malloc(strlen(filename
) + 4, M_LINKER
, M_WAITOK
);
260 if (koname
== NULL
) {
264 sprintf(koname
, "%s.ko", filename
);
267 for (lc
= TAILQ_FIRST(&classes
); lc
; lc
= TAILQ_NEXT(lc
, link
)) {
268 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
269 filename
, lc
->desc
));
271 error
= lc
->ops
->load_file(koname
, &lf
); /* First with .ko */
272 if (lf
== NULL
&& error
== ENOENT
)
273 error
= lc
->ops
->load_file(filename
, &lf
); /* Then try without */
275 * If we got something other than ENOENT, then it exists but we cannot
276 * load it for some other reason.
281 linker_file_register_sysctls(lf
);
282 error
= linker_file_sysinit(lf
);
289 * Less than ideal, but tells the user whether it failed to load or
290 * the module was not found.
294 * Format not recognized or otherwise unloadable.
295 * When loading a module that is statically built into
296 * the kernel EEXIST percolates back up as the return
297 * value. Preserve this so that apps like sysinstall
298 * can recognize this special case and not post bogus
304 error
= ENOENT
; /* Nothing found */
309 free(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
= malloc(strlen(filename
) + 4, M_LINKER
, M_WAITOK
);
327 sprintf(koname
, "%s.ko", filename
);
329 lockmgr(&lock
, LK_SHARED
, NULL
, curthread
);
330 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
331 if (!strcmp(lf
->filename
, koname
))
333 if (!strcmp(lf
->filename
, filename
))
336 lockmgr(&lock
, LK_RELEASE
, NULL
, curthread
);
340 free(koname
, M_LINKER
);
345 linker_find_file_by_id(int fileid
)
347 linker_file_t lf
= 0;
349 lockmgr(&lock
, LK_SHARED
, NULL
, curthread
);
350 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
))
351 if (lf
->id
== fileid
)
353 lockmgr(&lock
, LK_RELEASE
, NULL
, curthread
);
359 linker_make_file(const char* pathname
, void* priv
, struct linker_file_ops
* ops
)
361 linker_file_t lf
= 0;
363 const char *filename
;
365 filename
= rindex(pathname
, '/');
366 if (filename
&& filename
[1])
371 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename
));
372 lockmgr(&lock
, LK_EXCLUSIVE
, NULL
, curthread
);
373 namelen
= strlen(filename
) + 1;
374 lf
= malloc(sizeof(struct linker_file
) + namelen
, M_LINKER
, M_WAITOK
);
377 bzero(lf
, sizeof(*lf
));
382 lf
->filename
= (char*) (lf
+ 1);
383 strcpy(lf
->filename
, filename
);
384 lf
->id
= next_file_id
++;
387 STAILQ_INIT(&lf
->common
);
388 TAILQ_INIT(&lf
->modules
);
392 TAILQ_INSERT_TAIL(&linker_files
, lf
, link
);
395 lockmgr(&lock
, LK_RELEASE
, NULL
, curthread
);
400 linker_file_unload(linker_file_t file
)
403 struct common_symbol
* cp
;
407 /* Refuse to unload modules if securelevel raised */
411 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file
->refs
));
412 lockmgr(&lock
, LK_EXCLUSIVE
, NULL
, curthread
);
413 if (file
->refs
== 1) {
414 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
416 * Temporarily bump file->refs to prevent recursive unloading
421 * Inform any modules associated with this file.
423 mod
= TAILQ_FIRST(&file
->modules
);
426 * Give the module a chance to veto the unload. Note that the
427 * act of unloading the module may cause other modules in the
428 * same file list to be unloaded recursively.
430 if ((error
= module_unload(mod
)) != 0) {
431 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
433 lockmgr(&lock
, LK_RELEASE
, NULL
, curthread
);
438 * Recursive relationships may prevent the release from
439 * immediately removing the module, or may remove other
440 * modules in the list.
442 next
= module_getfnext(mod
);
448 * Since we intend to destroy the file structure, we expect all
449 * modules to have been removed by now.
451 for (mod
= TAILQ_FIRST(&file
->modules
);
453 mod
= module_getfnext(mod
)
455 printf("linker_file_unload: module %p still has refs!\n", mod
);
461 if (file
->refs
> 0) {
462 lockmgr(&lock
, LK_RELEASE
, NULL
, curthread
);
466 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
467 if (file
->flags
& LINKER_FILE_LINKED
) {
468 linker_file_sysuninit(file
);
469 linker_file_unregister_sysctls(file
);
472 TAILQ_REMOVE(&linker_files
, file
, link
);
473 lockmgr(&lock
, LK_RELEASE
, NULL
, curthread
);
475 for (i
= 0; i
< file
->ndeps
; i
++)
476 linker_file_unload(file
->deps
[i
]);
477 free(file
->deps
, M_LINKER
);
479 for (cp
= STAILQ_FIRST(&file
->common
); cp
;
480 cp
= STAILQ_FIRST(&file
->common
)) {
481 STAILQ_REMOVE(&file
->common
, cp
, common_symbol
, link
);
485 file
->ops
->unload(file
);
486 free(file
, M_LINKER
);
493 linker_file_add_dependancy(linker_file_t file
, linker_file_t dep
)
495 linker_file_t
* newdeps
;
497 newdeps
= malloc((file
->ndeps
+ 1) * sizeof(linker_file_t
*),
501 bzero(newdeps
, (file
->ndeps
+ 1) * sizeof(linker_file_t
*));
504 bcopy(file
->deps
, newdeps
, file
->ndeps
* sizeof(linker_file_t
*));
505 free(file
->deps
, M_LINKER
);
507 file
->deps
= newdeps
;
508 file
->deps
[file
->ndeps
] = dep
;
515 * Locate a linker set and its contents.
516 * This is a helper function to avoid linker_if.h exposure elsewhere.
517 * Note: firstp and lastp are really void ***
520 linker_file_lookup_set(linker_file_t file
, const char *name
,
521 void *firstp
, void *lastp
, int *countp
)
523 return file
->ops
->lookup_set(file
, name
, firstp
, lastp
, countp
);
527 linker_file_lookup_symbol(linker_file_t file
, const char* name
, int deps
, caddr_t
*raddr
)
530 linker_symval_t symval
;
532 size_t common_size
= 0;
535 KLD_DPF(SYM
, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
538 if (file
->ops
->lookup_symbol(file
, name
, &sym
) == 0) {
539 file
->ops
->symbol_values(file
, sym
, &symval
);
542 * XXX Assume a common symbol if its value is 0 and it has a non-zero
543 * size, otherwise it could be an absolute symbol with a value of 0.
545 if (symval
.value
== 0 && symval
.size
!= 0) {
547 * For commons, first look them up in the dependancies and
548 * only allocate space if not found there.
550 common_size
= symval
.size
;
552 KLD_DPF(SYM
, ("linker_file_lookup_symbol: symbol.value=%x\n", symval
.value
));
553 *raddr
= symval
.value
;
558 for (i
= 0; i
< file
->ndeps
; i
++) {
559 if (linker_file_lookup_symbol(file
->deps
[i
], name
, 0, raddr
) == 0) {
560 KLD_DPF(SYM
, ("linker_file_lookup_symbol: deps value=%x\n", *raddr
));
565 /* If we have not found it in the dependencies, search globally */
566 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
567 /* But skip the current file if it's on the list */
570 /* And skip the files we searched above */
571 for (i
= 0; i
< file
->ndeps
; i
++)
572 if (lf
== file
->deps
[i
])
576 if (linker_file_lookup_symbol(lf
, name
, 0, raddr
) == 0) {
577 KLD_DPF(SYM
, ("linker_file_lookup_symbol: global value=%x\n", *raddr
));
583 if (common_size
> 0) {
585 * This is a common symbol which was not found in the
586 * dependancies. We maintain a simple common symbol table in
589 struct common_symbol
* cp
;
591 for (cp
= STAILQ_FIRST(&file
->common
); cp
;
592 cp
= STAILQ_NEXT(cp
, link
))
593 if (!strcmp(cp
->name
, name
)) {
594 KLD_DPF(SYM
, ("linker_file_lookup_symbol: old common value=%x\n", cp
->address
));
595 *raddr
= cp
->address
;
600 * Round the symbol size up to align.
602 common_size
= (common_size
+ sizeof(int) - 1) & -sizeof(int);
603 cp
= malloc(sizeof(struct common_symbol
)
608 KLD_DPF(SYM
, ("linker_file_lookup_symbol: nomem\n"));
611 bzero(cp
, sizeof(struct common_symbol
) + common_size
+ strlen(name
)+ 1);
613 cp
->address
= (caddr_t
) (cp
+ 1);
614 cp
->name
= cp
->address
+ common_size
;
615 strcpy(cp
->name
, name
);
616 bzero(cp
->address
, common_size
);
617 STAILQ_INSERT_TAIL(&file
->common
, cp
, link
);
619 KLD_DPF(SYM
, ("linker_file_lookup_symbol: new common value=%x\n", cp
->address
));
620 *raddr
= cp
->address
;
624 KLD_DPF(SYM
, ("linker_file_lookup_symbol: fail\n"));
630 * DDB Helpers. DDB has to look across multiple files with their own
631 * symbol tables and string tables.
633 * Note that we do not obey list locking protocols here. We really don't
634 * need DDB to hang because somebody's got the lock held. We'll take the
635 * chance that the files list is inconsistant instead.
639 linker_ddb_lookup(const char *symstr
, c_linker_sym_t
*sym
)
643 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
644 if (lf
->ops
->lookup_symbol(lf
, symstr
, sym
) == 0)
651 linker_ddb_search_symbol(caddr_t value
, c_linker_sym_t
*sym
, long *diffp
)
654 u_long off
= (uintptr_t)value
;
655 u_long diff
, bestdiff
;
661 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
662 if (lf
->ops
->search_symbol(lf
, value
, &es
, &diff
) != 0)
664 if (es
!= 0 && diff
< bestdiff
) {
683 linker_ddb_symbol_values(c_linker_sym_t sym
, linker_symval_t
*symval
)
687 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
688 if (lf
->ops
->symbol_values(lf
, sym
, symval
) == 0)
701 kldload(struct kldload_args
*uap
)
703 struct thread
*td
= curthread
;
704 char* filename
= NULL
, *modulename
;
708 uap
->sysmsg_result
= -1;
710 if (securelevel
> 0) /* redundant, but that's OK */
713 if ((error
= suser(td
)) != 0)
716 filename
= malloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
717 if ((error
= copyinstr(uap
->file
, filename
, MAXPATHLEN
, NULL
)) != 0)
720 /* Can't load more than one module with the same name */
721 modulename
= rindex(filename
, '/');
722 if (modulename
== NULL
)
723 modulename
= filename
;
726 if (linker_find_file_by_name(modulename
)) {
731 if ((error
= linker_load_file(filename
, &lf
)) != 0)
735 uap
->sysmsg_result
= lf
->id
;
739 free(filename
, M_TEMP
);
744 kldunload(struct kldunload_args
*uap
)
746 struct thread
*td
= curthread
;
750 if (securelevel
> 0) /* redundant, but that's OK */
753 if ((error
= suser(td
)) != 0)
756 lf
= linker_find_file_by_id(uap
->fileid
);
758 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf
->userrefs
));
759 if (lf
->userrefs
== 0) {
760 printf("linkerunload: attempt to unload file that was loaded by the kernel\n");
765 error
= linker_file_unload(lf
);
776 kldfind(struct kldfind_args
*uap
)
778 char *filename
= NULL
, *modulename
;
782 uap
->sysmsg_result
= -1;
784 filename
= malloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
785 if ((error
= copyinstr(uap
->file
, filename
, MAXPATHLEN
, NULL
)) != 0)
788 modulename
= rindex(filename
, '/');
789 if (modulename
== NULL
)
790 modulename
= filename
;
792 lf
= linker_find_file_by_name(modulename
);
794 uap
->sysmsg_result
= lf
->id
;
800 free(filename
, M_TEMP
);
805 kldnext(struct kldnext_args
*uap
)
810 if (uap
->fileid
== 0) {
811 if (TAILQ_FIRST(&linker_files
))
812 uap
->sysmsg_result
= TAILQ_FIRST(&linker_files
)->id
;
814 uap
->sysmsg_result
= 0;
818 lf
= linker_find_file_by_id(uap
->fileid
);
820 if (TAILQ_NEXT(lf
, link
))
821 uap
->sysmsg_result
= TAILQ_NEXT(lf
, link
)->id
;
823 uap
->sysmsg_result
= 0;
831 kldstat(struct kldstat_args
*uap
)
836 struct kld_file_stat
* stat
;
839 lf
= linker_find_file_by_id(uap
->fileid
);
848 * Check the version of the user's structure.
850 if ((error
= copyin(&stat
->version
, &version
, sizeof(version
))) != 0)
852 if (version
!= sizeof(struct kld_file_stat
)) {
857 namelen
= strlen(lf
->filename
) + 1;
858 if (namelen
> MAXPATHLEN
)
859 namelen
= MAXPATHLEN
;
860 if ((error
= copyout(lf
->filename
, &stat
->name
[0], namelen
)) != 0)
862 if ((error
= copyout(&lf
->refs
, &stat
->refs
, sizeof(int))) != 0)
864 if ((error
= copyout(&lf
->id
, &stat
->id
, sizeof(int))) != 0)
866 if ((error
= copyout(&lf
->address
, &stat
->address
, sizeof(caddr_t
))) != 0)
868 if ((error
= copyout(&lf
->size
, &stat
->size
, sizeof(size_t))) != 0)
871 uap
->sysmsg_result
= 0;
878 kldfirstmod(struct kldfirstmod_args
*uap
)
883 lf
= linker_find_file_by_id(uap
->fileid
);
885 if (TAILQ_FIRST(&lf
->modules
))
886 uap
->sysmsg_result
= module_getid(TAILQ_FIRST(&lf
->modules
));
888 uap
->sysmsg_result
= 0;
896 kldsym(struct kldsym_args
*uap
)
900 linker_symval_t symval
;
902 struct kld_sym_lookup lookup
;
905 if ((error
= copyin(uap
->data
, &lookup
, sizeof(lookup
))) != 0)
907 if (lookup
.version
!= sizeof(lookup
) || uap
->cmd
!= KLDSYM_LOOKUP
) {
912 symstr
= malloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
913 if ((error
= copyinstr(lookup
.symname
, symstr
, MAXPATHLEN
, NULL
)) != 0)
916 if (uap
->fileid
!= 0) {
917 lf
= linker_find_file_by_id(uap
->fileid
);
922 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
923 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
924 lookup
.symvalue
= (uintptr_t)symval
.value
;
925 lookup
.symsize
= symval
.size
;
926 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
930 for (lf
= TAILQ_FIRST(&linker_files
); lf
; lf
= TAILQ_NEXT(lf
, link
)) {
931 if (lf
->ops
->lookup_symbol(lf
, symstr
, &sym
) == 0 &&
932 lf
->ops
->symbol_values(lf
, sym
, &symval
) == 0) {
933 lookup
.symvalue
= (uintptr_t)symval
.value
;
934 lookup
.symsize
= symval
.size
;
935 error
= copyout(&lookup
, uap
->data
, sizeof(lookup
));
944 free(symstr
, M_TEMP
);
949 * Look for module metadata in the static kernel
951 struct mod_metadata
*
952 find_mod_metadata(const char *modname
)
955 struct mod_metadata
**mdp
;
956 struct mod_metadata
*mdt
;
959 * Strip path prefixes and any dot extension. MDT_MODULE names
960 * are just the module name without a path or ".ko".
962 for (len
= strlen(modname
) - 1; len
>= 0; --len
) {
963 if (modname
[len
] == '/')
967 for (len
= 0; modname
[len
] && modname
[len
] != '.'; ++len
)
971 * Look for the module declaration
973 SET_FOREACH(mdp
, modmetadata_set
) {
975 if (mdt
->md_type
!= MDT_MODULE
)
977 if (strlen(mdt
->md_cval
) == len
&&
978 strncmp(mdt
->md_cval
, modname
, len
) == 0
987 * Preloaded module support
990 linker_preload(void* arg
)
998 struct sysinit
**sipp
;
999 const moduledata_t
*moddata
;
1000 struct sysinit
**si_start
, **si_stop
;
1003 while ((modptr
= preload_search_next_name(modptr
)) != NULL
) {
1004 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
1005 modtype
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
1006 if (modname
== NULL
) {
1007 printf("Preloaded module at %p does not have a name!\n", modptr
);
1010 if (modtype
== NULL
) {
1011 printf("Preloaded module at %p does not have a type!\n", modptr
);
1016 * This is a hack at the moment, but what's in FreeBSD-5 is even
1017 * worse so I'd rather the hack.
1019 printf("Preloaded %s \"%s\" at %p", modtype
, modname
, modptr
);
1020 if (find_mod_metadata(modname
)) {
1021 printf(" (ignored, already in static kernel)\n");
1026 lf
= linker_find_file_by_name(modname
);
1032 for (lc
= TAILQ_FIRST(&classes
); lc
; lc
= TAILQ_NEXT(lc
, link
)) {
1033 error
= lc
->ops
->load_file(modname
, &lf
);
1042 if (linker_file_lookup_set(lf
, "sysinit_set", &si_start
, &si_stop
, NULL
) == 0) {
1044 * This is to set the sysinit moduledata so that the module
1045 * can attach itself to the correct containing file.
1046 * The sysinit could be run at *any* time.
1048 for (sipp
= si_start
; sipp
< si_stop
; sipp
++) {
1049 if ((*sipp
)->func
== module_register_init
) {
1050 moddata
= (*sipp
)->udata
;
1051 error
= module_register(moddata
, lf
);
1053 printf("Preloaded %s \"%s\" failed to register: %d\n",
1054 modtype
, modname
, error
);
1057 sysinit_add(si_start
, si_stop
);
1059 linker_file_register_sysctls(lf
);
1064 SYSINIT(preload
, SI_SUB_KLD
, SI_ORDER_MIDDLE
, linker_preload
, 0);
1067 * Search for a not-loaded module by name.
1069 * Modules may be found in the following locations:
1071 * - preloaded (result is just the module name)
1072 * - on disk (result is full path to module)
1074 * If the module name is qualified in any way (contains path, etc.)
1075 * the we simply return a copy of it.
1077 * The search path can be manipulated via sysctl. Note that we use the ';'
1078 * character as a separator to be consistent with the bootloader.
1081 static char linker_path
[MAXPATHLEN
] = "/;/boot/;/modules/";
1083 SYSCTL_STRING(_kern
, OID_AUTO
, module_path
, CTLFLAG_RW
, linker_path
,
1084 sizeof(linker_path
), "module load search path");
1087 linker_strdup(const char *str
)
1091 if ((result
= malloc((strlen(str
) + 1), M_LINKER
, M_WAITOK
)) != NULL
)
1092 strcpy(result
, str
);
1097 linker_search_path(const char *name
)
1099 struct nlookupdata nd
;
1100 char *cp
, *ep
, *result
;
1104 /* qualified at all? */
1105 if (index(name
, '/'))
1106 return(linker_strdup(name
));
1108 /* traverse the linker path */
1112 /* find the end of this component */
1113 for (ep
= cp
; (*ep
!= 0) && (*ep
!= ';'); ep
++)
1115 result
= malloc((strlen(name
) + (ep
- cp
) + 1), M_LINKER
, M_WAITOK
);
1116 if (result
== NULL
) /* actually ENOMEM */
1119 strncpy(result
, cp
, ep
- cp
);
1120 strcpy(result
+ (ep
- cp
), name
);
1123 * Attempt to open the file, and return the path if we succeed and it's
1126 error
= nlookup_init(&nd
, result
, UIO_SYSSPACE
, NLC_FOLLOW
|NLC_LOCKVP
);
1128 error
= vn_open(&nd
, NULL
, FREAD
, 0);
1130 type
= nd
.nl_open_vp
->v_type
;
1137 free(result
, M_LINKER
);