kern_linker.c revision 42316
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.17 1998/11/11 13:04:39 peter 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 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    for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
259	KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
260		       filename, lc->desc));
261	error = lc->ops->load_file(koname, &lf);
262	if (lf == NULL && error && error != ENOENT)
263	    goto out;
264	if (lf == NULL)
265	    error = lc->ops->load_file(filename, &lf);
266	if (lf == NULL && error && error != ENOENT)
267	    goto out;
268	if (lf) {
269	    linker_file_sysinit(lf);
270
271	    *result = lf;
272	    error = 0;
273	    goto out;
274	}
275    }
276    error = ENOEXEC;		/* format not recognised */
277
278out:
279    if (koname)
280	free(koname, M_LINKER);
281    return error;
282}
283
284linker_file_t
285linker_find_file_by_name(const char* filename)
286{
287    linker_file_t lf = 0;
288    char *koname;
289
290    koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
291    if (koname == NULL)
292	goto out;
293    sprintf(koname, "%s.ko", filename);
294
295    lockmgr(&lock, LK_SHARED, 0, curproc);
296    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
297	if (!strcmp(lf->filename, koname))
298	    break;
299	if (!strcmp(lf->filename, filename))
300	    break;
301    }
302    lockmgr(&lock, LK_RELEASE, 0, curproc);
303
304out:
305    if (koname)
306	free(koname, M_LINKER);
307    return lf;
308}
309
310linker_file_t
311linker_find_file_by_id(int fileid)
312{
313    linker_file_t lf = 0;
314
315    lockmgr(&lock, LK_SHARED, 0, curproc);
316    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link))
317	if (lf->id == fileid)
318	    break;
319    lockmgr(&lock, LK_RELEASE, 0, curproc);
320
321    return lf;
322}
323
324linker_file_t
325linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
326{
327    linker_file_t lf = 0;
328    int namelen;
329    const char *filename;
330
331    filename = rindex(pathname, '/');
332    if (filename && filename[1])
333	filename++;
334    else
335	filename = pathname;
336
337    KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
338    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
339    namelen = strlen(filename) + 1;
340    lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
341    if (!lf)
342	goto out;
343    bzero(lf, sizeof(*lf));
344
345    lf->refs = 1;
346    lf->userrefs = 0;
347    lf->filename = (char*) (lf + 1);
348    strcpy(lf->filename, filename);
349    lf->id = next_file_id++;
350    lf->ndeps = 0;
351    lf->deps = NULL;
352    STAILQ_INIT(&lf->common);
353    TAILQ_INIT(&lf->modules);
354
355    lf->priv = priv;
356    lf->ops = ops;
357    TAILQ_INSERT_TAIL(&files, lf, link);
358
359out:
360    lockmgr(&lock, LK_RELEASE, 0, curproc);
361    return lf;
362}
363
364int
365linker_file_unload(linker_file_t file)
366{
367    module_t mod, next;
368    struct common_symbol* cp;
369    int error = 0;
370    int i;
371
372    KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
373    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
374    if (file->refs == 1) {
375	KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
376	/*
377	 * Inform any modules associated with this file.
378	 */
379	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
380	    next = module_getfnext(mod);
381
382	    /*
383	     * Give the module a chance to veto the unload.
384	     */
385	    if (error = module_unload(mod)) {
386		KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
387			       mod));
388		lockmgr(&lock, LK_RELEASE, 0, curproc);
389		goto out;
390	    }
391
392	    module_release(mod);
393	}
394    }
395
396    file->refs--;
397    if (file->refs > 0) {
398	lockmgr(&lock, LK_RELEASE, 0, curproc);
399	goto out;
400    }
401
402    linker_file_sysuninit(file);
403
404    TAILQ_REMOVE(&files, file, link);
405    lockmgr(&lock, LK_RELEASE, 0, curproc);
406
407    for (i = 0; i < file->ndeps; i++)
408	linker_file_unload(file->deps[i]);
409    free(file->deps, M_LINKER);
410
411    for (cp = STAILQ_FIRST(&file->common); cp;
412	 cp = STAILQ_FIRST(&file->common)) {
413	STAILQ_REMOVE(&file->common, cp, common_symbol, link);
414	free(cp, M_LINKER);
415    }
416
417    file->ops->unload(file);
418    free(file, M_LINKER);
419
420out:
421    return error;
422}
423
424int
425linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
426{
427    linker_file_t* newdeps;
428
429    newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
430		     M_LINKER, M_WAITOK);
431    if (newdeps == NULL)
432	return ENOMEM;
433    bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
434
435    if (file->deps) {
436	bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
437	free(file->deps, M_LINKER);
438    }
439    file->deps = newdeps;
440    file->deps[file->ndeps] = dep;
441    file->ndeps++;
442
443    return 0;
444}
445
446caddr_t
447linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
448{
449    linker_sym_t sym;
450    linker_symval_t symval;
451    caddr_t address;
452    size_t common_size = 0;
453    int i;
454
455    KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
456		  file, name, deps));
457
458    if (file->ops->lookup_symbol(file, name, &sym) == 0) {
459	file->ops->symbol_values(file, sym, &symval);
460	if (symval.value == 0)
461	    /*
462	     * For commons, first look them up in the dependancies and
463	     * only allocate space if not found there.
464	     */
465	    common_size = symval.size;
466	else {
467	    KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
468	    return symval.value;
469	}
470    }
471
472    if (deps)
473	for (i = 0; i < file->ndeps; i++) {
474	    address = linker_file_lookup_symbol(file->deps[i], name, 0);
475	    if (address) {
476		KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
477		return address;
478	    }
479	}
480
481    if (common_size > 0) {
482	/*
483	 * This is a common symbol which was not found in the
484	 * dependancies.  We maintain a simple common symbol table in
485	 * the file object.
486	 */
487	struct common_symbol* cp;
488
489	for (cp = STAILQ_FIRST(&file->common); cp;
490	     cp = STAILQ_NEXT(cp, link))
491	    if (!strcmp(cp->name, name)) {
492		KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
493		return cp->address;
494	    }
495
496	/*
497	 * Round the symbol size up to align.
498	 */
499	common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
500	cp = malloc(sizeof(struct common_symbol)
501		    + common_size
502		    + strlen(name) + 1,
503		    M_LINKER, M_WAITOK);
504	if (!cp) {
505	    KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
506	    return 0;
507	}
508	bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
509
510	cp->address = (caddr_t) (cp + 1);
511	cp->name = cp->address + common_size;
512	strcpy(cp->name, name);
513	bzero(cp->address, common_size);
514	STAILQ_INSERT_TAIL(&file->common, cp, link);
515
516	KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
517	return cp->address;
518    }
519
520    KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
521    return 0;
522}
523
524#ifdef DDB
525/*
526 * DDB Helpers.  DDB has to look across multiple files with their own
527 * symbol tables and string tables.
528 *
529 * Note that we do not obey list locking protocols here.  We really don't
530 * need DDB to hang because somebody's got the lock held.  We'll take the
531 * chance that the files list is inconsistant instead.
532 */
533
534int
535linker_ddb_lookup(char *symstr, linker_sym_t *sym)
536{
537    linker_file_t lf;
538
539    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
540	if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
541	    return 0;
542    }
543    return ENOENT;
544}
545
546int
547linker_ddb_search_symbol(caddr_t value, linker_sym_t *sym, long *diffp)
548{
549    linker_file_t lf;
550    u_long off = (u_long)value;
551    u_long diff, bestdiff;
552    linker_sym_t best;
553    linker_sym_t es;
554
555    best = 0;
556    bestdiff = off;
557    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
558	if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
559	    continue;
560	if (es != 0 && diff < bestdiff) {
561	    best = es;
562	    bestdiff = diff;
563	}
564	if (bestdiff == 0)
565	    break;
566    }
567    if (best) {
568	*sym = best;
569	*diffp = bestdiff;
570	return 0;
571    } else {
572	*sym = 0;
573	*diffp = off;
574	return ENOENT;
575    }
576}
577
578int
579linker_ddb_symbol_values(linker_sym_t sym, linker_symval_t *symval)
580{
581    linker_file_t lf;
582
583    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
584	if (lf->ops->symbol_values(lf, sym, symval) == 0)
585	    return 0;
586    }
587    return ENOENT;
588}
589
590#endif
591
592/*
593 * Syscalls.
594 */
595
596int
597kldload(struct proc* p, struct kldload_args* uap)
598{
599    char* filename = NULL, *modulename;
600    linker_file_t lf;
601    int error = 0;
602
603    p->p_retval[0] = -1;
604
605    if (securelevel > 0)
606	return EPERM;
607
608    if (error = suser(p->p_ucred, &p->p_acflag))
609	return error;
610
611    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
612    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
613	goto out;
614
615    /* Can't load more than one module with the same name */
616    modulename = rindex(filename, '/');
617    if (modulename == NULL)
618	modulename = filename;
619    if (linker_find_file_by_name(modulename)) {
620	error = EEXIST;
621	goto out;
622    }
623
624    if (error = linker_load_file(filename, &lf))
625	goto out;
626
627    lf->userrefs++;
628    p->p_retval[0] = lf->id;
629
630out:
631    if (filename)
632	free(filename, M_TEMP);
633    return error;
634}
635
636int
637kldunload(struct proc* p, struct kldunload_args* uap)
638{
639    linker_file_t lf;
640    int error = 0;
641
642    if (securelevel > 0)
643	return EPERM;
644
645    if (error = suser(p->p_ucred, &p->p_acflag))
646	return error;
647
648    lf = linker_find_file_by_id(SCARG(uap, fileid));
649    if (lf) {
650	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
651	if (lf->userrefs == 0) {
652	    printf("linkerunload: attempt to unload file which was not loaded by user\n");
653	    error = EBUSY;
654	    goto out;
655	}
656	lf->userrefs--;
657	error = linker_file_unload(lf);
658    } else
659	error = ENOENT;
660
661out:
662    return error;
663}
664
665int
666kldfind(struct proc* p, struct kldfind_args* uap)
667{
668    char* filename = NULL, *modulename;
669    linker_file_t lf;
670    int error = 0;
671
672    p->p_retval[0] = -1;
673
674    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
675    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
676	goto out;
677
678    modulename = rindex(filename, '/');
679    if (modulename == NULL)
680	modulename = filename;
681
682    lf = linker_find_file_by_name(modulename);
683    if (lf)
684	p->p_retval[0] = lf->id;
685    else
686	error = ENOENT;
687
688out:
689    if (filename)
690	free(filename, M_TEMP);
691    return error;
692}
693
694int
695kldnext(struct proc* p, struct kldnext_args* uap)
696{
697    linker_file_t lf;
698    int error = 0;
699
700    if (SCARG(uap, fileid) == 0) {
701	if (TAILQ_FIRST(&files))
702	    p->p_retval[0] = TAILQ_FIRST(&files)->id;
703	else
704	    p->p_retval[0] = 0;
705	return 0;
706    }
707
708    lf = linker_find_file_by_id(SCARG(uap, fileid));
709    if (lf) {
710	if (TAILQ_NEXT(lf, link))
711	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
712	else
713	    p->p_retval[0] = 0;
714    } else
715	error = ENOENT;
716
717    return error;
718}
719
720int
721kldstat(struct proc* p, struct kldstat_args* uap)
722{
723    linker_file_t lf;
724    int error = 0;
725    int version;
726    struct kld_file_stat* stat;
727    int namelen;
728
729    lf = linker_find_file_by_id(SCARG(uap, fileid));
730    if (!lf) {
731	error = ENOENT;
732	goto out;
733    }
734
735    stat = SCARG(uap, stat);
736
737    /*
738     * Check the version of the user's structure.
739     */
740    if (error = copyin(&stat->version, &version, sizeof(version)))
741	goto out;
742    if (version != sizeof(struct kld_file_stat)) {
743	error = EINVAL;
744	goto out;
745    }
746
747    namelen = strlen(lf->filename) + 1;
748    if (namelen > MAXPATHLEN)
749	namelen = MAXPATHLEN;
750    if (error = copyout(lf->filename, &stat->name[0], namelen))
751	goto out;
752    if (error = copyout(&lf->refs, &stat->refs, sizeof(int)))
753	goto out;
754    if (error = copyout(&lf->id, &stat->id, sizeof(int)))
755	goto out;
756    if (error = copyout(&lf->address, &stat->address, sizeof(caddr_t)))
757	goto out;
758    if (error = copyout(&lf->size, &stat->size, sizeof(size_t)))
759	goto out;
760
761    p->p_retval[0] = 0;
762
763out:
764    return error;
765}
766
767int
768kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
769{
770    linker_file_t lf;
771    int error = 0;
772
773    lf = linker_find_file_by_id(SCARG(uap, fileid));
774    if (lf) {
775	if (TAILQ_FIRST(&lf->modules))
776	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
777	else
778	    p->p_retval[0] = 0;
779    } else
780	error = ENOENT;
781
782    return error;
783}
784
785int
786kldsym(struct proc *p, struct kldsym_args *uap)
787{
788    char *symstr = NULL;
789    linker_sym_t sym;
790    linker_symval_t symval;
791    linker_file_t lf;
792    struct kld_sym_lookup lookup;
793    int error = 0;
794
795    if (error = copyin(SCARG(uap, data), &lookup, sizeof(lookup)))
796	goto out;
797    if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
798	error = EINVAL;
799	goto out;
800    }
801
802    symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
803    if (error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL))
804	goto out;
805
806    if (SCARG(uap, fileid) != 0) {
807	lf = linker_find_file_by_id(SCARG(uap, fileid));
808	if (lf == NULL) {
809	    error = ENOENT;
810	    goto out;
811	}
812	if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
813	    lf->ops->symbol_values(lf, sym, &symval) == 0) {
814	    lookup.symvalue = (u_long)symval.value;
815	    lookup.symsize = symval.size;
816	    error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
817	} else
818	    error = ENOENT;
819    } else {
820	for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
821	    if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
822		lf->ops->symbol_values(lf, sym, &symval) == 0) {
823		lookup.symvalue = (u_long)symval.value;
824		lookup.symsize = symval.size;
825		error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
826		break;
827	    }
828	}
829	if (!lf)
830	    error = ENOENT;
831    }
832out:
833    if (symstr)
834	free(symstr, M_TEMP);
835    return error;
836}
837
838/*
839 * Preloaded module support
840 */
841
842static void
843linker_preload(void* arg)
844{
845    caddr_t		modptr;
846    char		*modname;
847    char		*modtype;
848    linker_file_t	lf;
849    linker_class_t	lc;
850    int			error;
851    struct linker_set	*sysinits;
852    struct sysinit	**sipp;
853    moduledata_t	*moddata;
854
855    modptr = NULL;
856    while ((modptr = preload_search_next_name(modptr)) != NULL) {
857	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
858	modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
859	if (modname == NULL) {
860	    printf("Preloaded module at %p does not have a name!\n", modptr);
861	    continue;
862	}
863	if (modtype == NULL) {
864	    printf("Preloaded module at %p does not have a type!\n", modptr);
865	    continue;
866	}
867	printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
868	lf = linker_find_file_by_name(modname);
869	if (lf) {
870	    lf->userrefs++;
871	    continue;
872	}
873	lf = NULL;
874	for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
875	    error = lc->ops->load_file(modname, &lf);
876	    if (error) {
877		lf = NULL;
878		break;
879	    }
880	}
881	if (lf) {
882	    lf->userrefs++;
883
884	    sysinits = (struct linker_set*)
885		linker_file_lookup_symbol(lf, "sysinit_set", 0);
886	    if (sysinits) {
887		/* HACK ALERT!
888		 * This is to set the sysinit moduledata so that the module
889		 * can attach itself to the correct containing file.
890		 * The sysinit could be run at *any* time.
891		 */
892		for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
893		    if ((*sipp)->func == module_register_init) {
894			moddata = (*sipp)->udata;
895			moddata->_file = lf;
896		    }
897		}
898		sysinit_add((struct sysinit **)sysinits->ls_items);
899	    }
900	}
901    }
902}
903
904SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
905
906/*
907 * Search for a not-loaded module by name.
908 *
909 * Modules may be found in the following locations:
910 *
911 * - preloaded (result is just the module name)
912 * - on disk (result is full path to module)
913 *
914 * If the module name is qualified in any way (contains path, etc.)
915 * the we simply return a copy of it.
916 *
917 * The search path can be manipulated via sysctl.  Note that we use the ';'
918 * character as a separator to be consistent with the bootloader.
919 */
920
921static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/";
922
923SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
924	      sizeof(linker_path), "module load search path");
925
926static char *
927linker_strdup(const char *str)
928{
929    char	*result;
930
931    if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
932	strcpy(result, str);
933    return(result);
934}
935
936char *
937linker_search_path(const char *name)
938{
939    struct nameidata	nd;
940    struct proc		*p = curproc;	/* XXX */
941    char		*cp, *ep, *result;
942    int			error;
943    enum vtype		type;
944
945    /* qualified at all? */
946    if (index(name, '/'))
947	return(linker_strdup(name));
948
949    /* traverse the linker path */
950    cp = linker_path;
951    for (;;) {
952
953	/* find the end of this component */
954	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
955	    ;
956	result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
957	if (result == NULL)	/* actually ENOMEM */
958	    return(NULL);
959
960	strncpy(result, cp, ep - cp);
961	strcpy(result + (ep - cp), name);
962
963	/*
964	 * Attempt to open the file, and return the path if we succeed and it's
965	 * a regular file.
966	 */
967	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
968	error = vn_open(&nd, FREAD, 0);
969	if (error == 0) {
970	    type = nd.ni_vp->v_type;
971	    VOP_UNLOCK(nd.ni_vp, 0, p);
972	    vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
973	    if (type == VREG)
974		return(result);
975	}
976	free(result, M_LINKER);
977
978	if (*ep == 0)
979	    break;
980	cp = ep + 1;
981    }
982    return(NULL);
983}
984