kern_linker.c revision 42755
1/*-
2 * Copyright (c) 1997 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 *	$Id: kern_linker.c,v 1.18 1999/01/05 20:24:28 msmith Exp $
27 */
28
29#include "opt_ddb.h"
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/sysproto.h>
36#include <sys/sysent.h>
37#include <sys/proc.h>
38#include <sys/lock.h>
39#include <machine/cpu.h>
40#include <machine/bootinfo.h>
41#include <sys/module.h>
42#include <sys/linker.h>
43#include <sys/unistd.h>
44#include <sys/fcntl.h>
45#include <sys/libkern.h>
46#include <sys/namei.h>
47#include <sys/vnode.h>
48#include <sys/sysctl.h>
49
50#ifdef KLD_DEBUG
51int kld_debug = 0;
52#endif
53
54MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
55linker_file_t linker_current_file;
56linker_file_t linker_kernel_file;
57
58static struct lock lock;	/* lock for the file list */
59static linker_class_list_t classes;
60static linker_file_list_t files;
61static int next_file_id = 1;
62
63static void
64linker_init(void* arg)
65{
66    lockinit(&lock, PVM, "klink", 0, 0);
67    TAILQ_INIT(&classes);
68    TAILQ_INIT(&files);
69}
70
71SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
72
73int
74linker_add_class(const char* desc, void* priv,
75		 struct linker_class_ops* ops)
76{
77    linker_class_t lc;
78
79    lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT);
80    if (!lc)
81	return ENOMEM;
82    bzero(lc, sizeof(*lc));
83
84    lc->desc = desc;
85    lc->priv = priv;
86    lc->ops = ops;
87    TAILQ_INSERT_HEAD(&classes, lc, link);
88
89    return 0;
90}
91
92static void
93linker_file_sysinit(linker_file_t lf)
94{
95    struct linker_set* sysinits;
96    struct sysinit** sipp;
97    struct sysinit** xipp;
98    struct sysinit* save;
99    moduledata_t *moddata;
100
101    KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
102		   lf->filename));
103
104    sysinits = (struct linker_set*)
105	linker_file_lookup_symbol(lf, "sysinit_set", 0);
106
107    KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits));
108    if (!sysinits)
109	return;
110
111    /* HACK ALERT! */
112    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
113	if ((*sipp)->func == module_register_init) {
114	    moddata = (*sipp)->udata;
115	    moddata->_file = lf;
116	}
117    }
118
119    /*
120     * Perform a bubble sort of the system initialization objects by
121     * their subsystem (primary key) and order (secondary key).
122     *
123     * Since some things care about execution order, this is the
124     * operation which ensures continued function.
125     */
126    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
127	for (xipp = sipp + 1; *xipp; xipp++) {
128	    if ((*sipp)->subsystem <= (*xipp)->subsystem ||
129		 ((*sipp)->subsystem == (*xipp)->subsystem &&
130		  (*sipp)->order <= (*xipp)->order))
131		continue;	/* skip*/
132	    save = *sipp;
133	    *sipp = *xipp;
134	    *xipp = save;
135	}
136    }
137
138
139    /*
140     * Traverse the (now) ordered list of system initialization tasks.
141     * Perform each task, and continue on to the next task.
142     */
143    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
144	if ((*sipp)->subsystem == SI_SUB_DUMMY)
145	    continue;	/* skip dummy task(s)*/
146
147	switch ((*sipp)->type) {
148	case SI_TYPE_DEFAULT:
149	    /* no special processing*/
150	    (*((*sipp)->func))((*sipp)->udata);
151	    break;
152
153	case SI_TYPE_KTHREAD:
154#if !defined(SMP)
155	    /* kernel thread*/
156	    if (fork1(&proc0, RFFDG|RFPROC|RFMEM))
157		panic("fork kernel thread");
158	    cpu_set_fork_handler(pfind(proc0.p_retval[0]),
159		(*sipp)->func, (*sipp)->udata);
160	    break;
161#endif
162
163	case SI_TYPE_KPROCESS:
164	    /* kernel thread*/
165	    if (fork1(&proc0, RFFDG|RFPROC))
166		panic("fork kernel process");
167	    cpu_set_fork_handler(pfind(proc0.p_retval[0]),
168		(*sipp)->func, (*sipp)->udata);
169	    break;
170
171	default:
172	    panic ("linker_file_sysinit: unrecognized init type");
173	}
174    }
175}
176
177static void
178linker_file_sysuninit(linker_file_t lf)
179{
180    struct linker_set* sysuninits;
181    struct sysinit** sipp;
182    struct sysinit** xipp;
183    struct sysinit* save;
184
185    KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
186		   lf->filename));
187
188    sysuninits = (struct linker_set*)
189	linker_file_lookup_symbol(lf, "sysuninit_set", 0);
190
191    KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits));
192    if (!sysuninits)
193	return;
194
195    /*
196     * Perform a reverse bubble sort of the system initialization objects
197     * by their subsystem (primary key) and order (secondary key).
198     *
199     * Since some things care about execution order, this is the
200     * operation which ensures continued function.
201     */
202    for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
203	for (xipp = sipp + 1; *xipp; xipp++) {
204	    if ((*sipp)->subsystem >= (*xipp)->subsystem ||
205		 ((*sipp)->subsystem == (*xipp)->subsystem &&
206		  (*sipp)->order >= (*xipp)->order))
207		continue;	/* skip*/
208	    save = *sipp;
209	    *sipp = *xipp;
210	    *xipp = save;
211	}
212    }
213
214
215    /*
216     * Traverse the (now) ordered list of system initialization tasks.
217     * Perform each task, and continue on to the next task.
218     */
219    for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
220	if ((*sipp)->subsystem == SI_SUB_DUMMY)
221	    continue;	/* skip dummy task(s)*/
222
223	switch ((*sipp)->type) {
224	case SI_TYPE_DEFAULT:
225	    /* no special processing*/
226	    (*((*sipp)->func))((*sipp)->udata);
227	    break;
228
229	default:
230	    panic("linker_file_sysuninit: unrecognized uninit type");
231	}
232    }
233}
234
235int
236linker_load_file(const char* filename, linker_file_t* result)
237{
238    linker_class_t lc;
239    linker_file_t lf;
240    int foundfile, error = 0;
241    char *koname = NULL;
242
243    lf = linker_find_file_by_name(filename);
244    if (lf) {
245	KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
246	*result = lf;
247	lf->refs++;
248	goto out;
249    }
250
251    koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
252    if (koname == NULL) {
253	error = ENOMEM;
254	goto out;
255    }
256    sprintf(koname, "%s.ko", filename);
257    lf = NULL;
258    foundfile = 0;
259    for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
260	KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
261		       filename, lc->desc));
262
263	error = lc->ops->load_file(koname, &lf);	/* First with .ko */
264	if (lf == NULL && error == ENOENT)
265	    error = lc->ops->load_file(filename, &lf);	/* Then try without */
266	/*
267	 * If we got something other than ENOENT, then it exists but we cannot
268	 * load it for some other reason.
269	 */
270	if (error != ENOENT)
271	    foundfile = 1;
272	if (lf) {
273	    linker_file_sysinit(lf);
274
275	    *result = lf;
276	    error = 0;
277	    goto out;
278	}
279    }
280    /*
281     * Less than ideal, but tells the user whether it failed to load or
282     * the module was not found.
283     */
284    if (foundfile)
285	error = ENOEXEC;	/* Format not recognised (or unloadable) */
286    else
287	error = ENOENT;		/* Nothing found */
288
289out:
290    if (koname)
291	free(koname, M_LINKER);
292    return error;
293}
294
295linker_file_t
296linker_find_file_by_name(const char* filename)
297{
298    linker_file_t lf = 0;
299    char *koname;
300
301    koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
302    if (koname == NULL)
303	goto out;
304    sprintf(koname, "%s.ko", filename);
305
306    lockmgr(&lock, LK_SHARED, 0, curproc);
307    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
308	if (!strcmp(lf->filename, koname))
309	    break;
310	if (!strcmp(lf->filename, filename))
311	    break;
312    }
313    lockmgr(&lock, LK_RELEASE, 0, curproc);
314
315out:
316    if (koname)
317	free(koname, M_LINKER);
318    return lf;
319}
320
321linker_file_t
322linker_find_file_by_id(int fileid)
323{
324    linker_file_t lf = 0;
325
326    lockmgr(&lock, LK_SHARED, 0, curproc);
327    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link))
328	if (lf->id == fileid)
329	    break;
330    lockmgr(&lock, LK_RELEASE, 0, curproc);
331
332    return lf;
333}
334
335linker_file_t
336linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
337{
338    linker_file_t lf = 0;
339    int namelen;
340    const char *filename;
341
342    filename = rindex(pathname, '/');
343    if (filename && filename[1])
344	filename++;
345    else
346	filename = pathname;
347
348    KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
349    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
350    namelen = strlen(filename) + 1;
351    lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
352    if (!lf)
353	goto out;
354    bzero(lf, sizeof(*lf));
355
356    lf->refs = 1;
357    lf->userrefs = 0;
358    lf->filename = (char*) (lf + 1);
359    strcpy(lf->filename, filename);
360    lf->id = next_file_id++;
361    lf->ndeps = 0;
362    lf->deps = NULL;
363    STAILQ_INIT(&lf->common);
364    TAILQ_INIT(&lf->modules);
365
366    lf->priv = priv;
367    lf->ops = ops;
368    TAILQ_INSERT_TAIL(&files, lf, link);
369
370out:
371    lockmgr(&lock, LK_RELEASE, 0, curproc);
372    return lf;
373}
374
375int
376linker_file_unload(linker_file_t file)
377{
378    module_t mod, next;
379    struct common_symbol* cp;
380    int error = 0;
381    int i;
382
383    KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
384    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
385    if (file->refs == 1) {
386	KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
387	/*
388	 * Inform any modules associated with this file.
389	 */
390	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
391	    next = module_getfnext(mod);
392
393	    /*
394	     * Give the module a chance to veto the unload.
395	     */
396	    if (error = module_unload(mod)) {
397		KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
398			       mod));
399		lockmgr(&lock, LK_RELEASE, 0, curproc);
400		goto out;
401	    }
402
403	    module_release(mod);
404	}
405    }
406
407    file->refs--;
408    if (file->refs > 0) {
409	lockmgr(&lock, LK_RELEASE, 0, curproc);
410	goto out;
411    }
412
413    linker_file_sysuninit(file);
414
415    TAILQ_REMOVE(&files, file, link);
416    lockmgr(&lock, LK_RELEASE, 0, curproc);
417
418    for (i = 0; i < file->ndeps; i++)
419	linker_file_unload(file->deps[i]);
420    free(file->deps, M_LINKER);
421
422    for (cp = STAILQ_FIRST(&file->common); cp;
423	 cp = STAILQ_FIRST(&file->common)) {
424	STAILQ_REMOVE(&file->common, cp, common_symbol, link);
425	free(cp, M_LINKER);
426    }
427
428    file->ops->unload(file);
429    free(file, M_LINKER);
430
431out:
432    return error;
433}
434
435int
436linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
437{
438    linker_file_t* newdeps;
439
440    newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
441		     M_LINKER, M_WAITOK);
442    if (newdeps == NULL)
443	return ENOMEM;
444    bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
445
446    if (file->deps) {
447	bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
448	free(file->deps, M_LINKER);
449    }
450    file->deps = newdeps;
451    file->deps[file->ndeps] = dep;
452    file->ndeps++;
453
454    return 0;
455}
456
457caddr_t
458linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
459{
460    linker_sym_t sym;
461    linker_symval_t symval;
462    caddr_t address;
463    size_t common_size = 0;
464    int i;
465
466    KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
467		  file, name, deps));
468
469    if (file->ops->lookup_symbol(file, name, &sym) == 0) {
470	file->ops->symbol_values(file, sym, &symval);
471	if (symval.value == 0)
472	    /*
473	     * For commons, first look them up in the dependancies and
474	     * only allocate space if not found there.
475	     */
476	    common_size = symval.size;
477	else {
478	    KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
479	    return symval.value;
480	}
481    }
482
483    if (deps)
484	for (i = 0; i < file->ndeps; i++) {
485	    address = linker_file_lookup_symbol(file->deps[i], name, 0);
486	    if (address) {
487		KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
488		return address;
489	    }
490	}
491
492    if (common_size > 0) {
493	/*
494	 * This is a common symbol which was not found in the
495	 * dependancies.  We maintain a simple common symbol table in
496	 * the file object.
497	 */
498	struct common_symbol* cp;
499
500	for (cp = STAILQ_FIRST(&file->common); cp;
501	     cp = STAILQ_NEXT(cp, link))
502	    if (!strcmp(cp->name, name)) {
503		KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
504		return cp->address;
505	    }
506
507	/*
508	 * Round the symbol size up to align.
509	 */
510	common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
511	cp = malloc(sizeof(struct common_symbol)
512		    + common_size
513		    + strlen(name) + 1,
514		    M_LINKER, M_WAITOK);
515	if (!cp) {
516	    KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
517	    return 0;
518	}
519	bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
520
521	cp->address = (caddr_t) (cp + 1);
522	cp->name = cp->address + common_size;
523	strcpy(cp->name, name);
524	bzero(cp->address, common_size);
525	STAILQ_INSERT_TAIL(&file->common, cp, link);
526
527	KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
528	return cp->address;
529    }
530
531    KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
532    return 0;
533}
534
535#ifdef DDB
536/*
537 * DDB Helpers.  DDB has to look across multiple files with their own
538 * symbol tables and string tables.
539 *
540 * Note that we do not obey list locking protocols here.  We really don't
541 * need DDB to hang because somebody's got the lock held.  We'll take the
542 * chance that the files list is inconsistant instead.
543 */
544
545int
546linker_ddb_lookup(char *symstr, linker_sym_t *sym)
547{
548    linker_file_t lf;
549
550    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
551	if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
552	    return 0;
553    }
554    return ENOENT;
555}
556
557int
558linker_ddb_search_symbol(caddr_t value, linker_sym_t *sym, long *diffp)
559{
560    linker_file_t lf;
561    u_long off = (u_long)value;
562    u_long diff, bestdiff;
563    linker_sym_t best;
564    linker_sym_t es;
565
566    best = 0;
567    bestdiff = off;
568    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
569	if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
570	    continue;
571	if (es != 0 && diff < bestdiff) {
572	    best = es;
573	    bestdiff = diff;
574	}
575	if (bestdiff == 0)
576	    break;
577    }
578    if (best) {
579	*sym = best;
580	*diffp = bestdiff;
581	return 0;
582    } else {
583	*sym = 0;
584	*diffp = off;
585	return ENOENT;
586    }
587}
588
589int
590linker_ddb_symbol_values(linker_sym_t sym, linker_symval_t *symval)
591{
592    linker_file_t lf;
593
594    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
595	if (lf->ops->symbol_values(lf, sym, symval) == 0)
596	    return 0;
597    }
598    return ENOENT;
599}
600
601#endif
602
603/*
604 * Syscalls.
605 */
606
607int
608kldload(struct proc* p, struct kldload_args* uap)
609{
610    char* filename = NULL, *modulename;
611    linker_file_t lf;
612    int error = 0;
613
614    p->p_retval[0] = -1;
615
616    if (securelevel > 0)
617	return EPERM;
618
619    if (error = suser(p->p_ucred, &p->p_acflag))
620	return error;
621
622    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
623    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
624	goto out;
625
626    /* Can't load more than one module with the same name */
627    modulename = rindex(filename, '/');
628    if (modulename == NULL)
629	modulename = filename;
630    if (linker_find_file_by_name(modulename)) {
631	error = EEXIST;
632	goto out;
633    }
634
635    if (error = linker_load_file(filename, &lf))
636	goto out;
637
638    lf->userrefs++;
639    p->p_retval[0] = lf->id;
640
641out:
642    if (filename)
643	free(filename, M_TEMP);
644    return error;
645}
646
647int
648kldunload(struct proc* p, struct kldunload_args* uap)
649{
650    linker_file_t lf;
651    int error = 0;
652
653    if (securelevel > 0)
654	return EPERM;
655
656    if (error = suser(p->p_ucred, &p->p_acflag))
657	return error;
658
659    lf = linker_find_file_by_id(SCARG(uap, fileid));
660    if (lf) {
661	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
662	if (lf->userrefs == 0) {
663	    printf("linkerunload: attempt to unload file which was not loaded by user\n");
664	    error = EBUSY;
665	    goto out;
666	}
667	lf->userrefs--;
668	error = linker_file_unload(lf);
669    } else
670	error = ENOENT;
671
672out:
673    return error;
674}
675
676int
677kldfind(struct proc* p, struct kldfind_args* uap)
678{
679    char* filename = NULL, *modulename;
680    linker_file_t lf;
681    int error = 0;
682
683    p->p_retval[0] = -1;
684
685    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
686    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
687	goto out;
688
689    modulename = rindex(filename, '/');
690    if (modulename == NULL)
691	modulename = filename;
692
693    lf = linker_find_file_by_name(modulename);
694    if (lf)
695	p->p_retval[0] = lf->id;
696    else
697	error = ENOENT;
698
699out:
700    if (filename)
701	free(filename, M_TEMP);
702    return error;
703}
704
705int
706kldnext(struct proc* p, struct kldnext_args* uap)
707{
708    linker_file_t lf;
709    int error = 0;
710
711    if (SCARG(uap, fileid) == 0) {
712	if (TAILQ_FIRST(&files))
713	    p->p_retval[0] = TAILQ_FIRST(&files)->id;
714	else
715	    p->p_retval[0] = 0;
716	return 0;
717    }
718
719    lf = linker_find_file_by_id(SCARG(uap, fileid));
720    if (lf) {
721	if (TAILQ_NEXT(lf, link))
722	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
723	else
724	    p->p_retval[0] = 0;
725    } else
726	error = ENOENT;
727
728    return error;
729}
730
731int
732kldstat(struct proc* p, struct kldstat_args* uap)
733{
734    linker_file_t lf;
735    int error = 0;
736    int version;
737    struct kld_file_stat* stat;
738    int namelen;
739
740    lf = linker_find_file_by_id(SCARG(uap, fileid));
741    if (!lf) {
742	error = ENOENT;
743	goto out;
744    }
745
746    stat = SCARG(uap, stat);
747
748    /*
749     * Check the version of the user's structure.
750     */
751    if (error = copyin(&stat->version, &version, sizeof(version)))
752	goto out;
753    if (version != sizeof(struct kld_file_stat)) {
754	error = EINVAL;
755	goto out;
756    }
757
758    namelen = strlen(lf->filename) + 1;
759    if (namelen > MAXPATHLEN)
760	namelen = MAXPATHLEN;
761    if (error = copyout(lf->filename, &stat->name[0], namelen))
762	goto out;
763    if (error = copyout(&lf->refs, &stat->refs, sizeof(int)))
764	goto out;
765    if (error = copyout(&lf->id, &stat->id, sizeof(int)))
766	goto out;
767    if (error = copyout(&lf->address, &stat->address, sizeof(caddr_t)))
768	goto out;
769    if (error = copyout(&lf->size, &stat->size, sizeof(size_t)))
770	goto out;
771
772    p->p_retval[0] = 0;
773
774out:
775    return error;
776}
777
778int
779kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
780{
781    linker_file_t lf;
782    int error = 0;
783
784    lf = linker_find_file_by_id(SCARG(uap, fileid));
785    if (lf) {
786	if (TAILQ_FIRST(&lf->modules))
787	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
788	else
789	    p->p_retval[0] = 0;
790    } else
791	error = ENOENT;
792
793    return error;
794}
795
796int
797kldsym(struct proc *p, struct kldsym_args *uap)
798{
799    char *symstr = NULL;
800    linker_sym_t sym;
801    linker_symval_t symval;
802    linker_file_t lf;
803    struct kld_sym_lookup lookup;
804    int error = 0;
805
806    if (error = copyin(SCARG(uap, data), &lookup, sizeof(lookup)))
807	goto out;
808    if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
809	error = EINVAL;
810	goto out;
811    }
812
813    symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
814    if (error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL))
815	goto out;
816
817    if (SCARG(uap, fileid) != 0) {
818	lf = linker_find_file_by_id(SCARG(uap, fileid));
819	if (lf == NULL) {
820	    error = ENOENT;
821	    goto out;
822	}
823	if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
824	    lf->ops->symbol_values(lf, sym, &symval) == 0) {
825	    lookup.symvalue = (u_long)symval.value;
826	    lookup.symsize = symval.size;
827	    error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
828	} else
829	    error = ENOENT;
830    } else {
831	for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
832	    if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
833		lf->ops->symbol_values(lf, sym, &symval) == 0) {
834		lookup.symvalue = (u_long)symval.value;
835		lookup.symsize = symval.size;
836		error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
837		break;
838	    }
839	}
840	if (!lf)
841	    error = ENOENT;
842    }
843out:
844    if (symstr)
845	free(symstr, M_TEMP);
846    return error;
847}
848
849/*
850 * Preloaded module support
851 */
852
853static void
854linker_preload(void* arg)
855{
856    caddr_t		modptr;
857    char		*modname;
858    char		*modtype;
859    linker_file_t	lf;
860    linker_class_t	lc;
861    int			error;
862    struct linker_set	*sysinits;
863    struct sysinit	**sipp;
864    moduledata_t	*moddata;
865
866    modptr = NULL;
867    while ((modptr = preload_search_next_name(modptr)) != NULL) {
868	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
869	modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
870	if (modname == NULL) {
871	    printf("Preloaded module at %p does not have a name!\n", modptr);
872	    continue;
873	}
874	if (modtype == NULL) {
875	    printf("Preloaded module at %p does not have a type!\n", modptr);
876	    continue;
877	}
878	printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
879	lf = linker_find_file_by_name(modname);
880	if (lf) {
881	    lf->userrefs++;
882	    continue;
883	}
884	lf = NULL;
885	for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
886	    error = lc->ops->load_file(modname, &lf);
887	    if (error) {
888		lf = NULL;
889		break;
890	    }
891	}
892	if (lf) {
893	    lf->userrefs++;
894
895	    sysinits = (struct linker_set*)
896		linker_file_lookup_symbol(lf, "sysinit_set", 0);
897	    if (sysinits) {
898		/* HACK ALERT!
899		 * This is to set the sysinit moduledata so that the module
900		 * can attach itself to the correct containing file.
901		 * The sysinit could be run at *any* time.
902		 */
903		for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
904		    if ((*sipp)->func == module_register_init) {
905			moddata = (*sipp)->udata;
906			moddata->_file = lf;
907		    }
908		}
909		sysinit_add((struct sysinit **)sysinits->ls_items);
910	    }
911	}
912    }
913}
914
915SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
916
917/*
918 * Search for a not-loaded module by name.
919 *
920 * Modules may be found in the following locations:
921 *
922 * - preloaded (result is just the module name)
923 * - on disk (result is full path to module)
924 *
925 * If the module name is qualified in any way (contains path, etc.)
926 * the we simply return a copy of it.
927 *
928 * The search path can be manipulated via sysctl.  Note that we use the ';'
929 * character as a separator to be consistent with the bootloader.
930 */
931
932static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/";
933
934SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
935	      sizeof(linker_path), "module load search path");
936
937static char *
938linker_strdup(const char *str)
939{
940    char	*result;
941
942    if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
943	strcpy(result, str);
944    return(result);
945}
946
947char *
948linker_search_path(const char *name)
949{
950    struct nameidata	nd;
951    struct proc		*p = curproc;	/* XXX */
952    char		*cp, *ep, *result;
953    int			error;
954    enum vtype		type;
955
956    /* qualified at all? */
957    if (index(name, '/'))
958	return(linker_strdup(name));
959
960    /* traverse the linker path */
961    cp = linker_path;
962    for (;;) {
963
964	/* find the end of this component */
965	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
966	    ;
967	result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
968	if (result == NULL)	/* actually ENOMEM */
969	    return(NULL);
970
971	strncpy(result, cp, ep - cp);
972	strcpy(result + (ep - cp), name);
973
974	/*
975	 * Attempt to open the file, and return the path if we succeed and it's
976	 * a regular file.
977	 */
978	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
979	error = vn_open(&nd, FREAD, 0);
980	if (error == 0) {
981	    type = nd.ni_vp->v_type;
982	    VOP_UNLOCK(nd.ni_vp, 0, p);
983	    vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
984	    if (type == VREG)
985		return(result);
986	}
987	free(result, M_LINKER);
988
989	if (*ep == 0)
990	    break;
991	cp = ep + 1;
992    }
993    return(NULL);
994}
995