kern_linker.c revision 65507
1/*-
2 * Copyright (c) 1997-2000 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 * $FreeBSD: head/sys/kern/kern_linker.c 65507 2000-09-06 06:22:20Z obrien $
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 <sys/module.h>
40#include <sys/linker.h>
41#include <sys/fcntl.h>
42#include <sys/libkern.h>
43#include <sys/namei.h>
44#include <sys/vnode.h>
45#include <sys/sysctl.h>
46
47
48#include "linker_if.h"
49
50#ifdef KLD_DEBUG
51int kld_debug = 0;
52#endif
53
54static char *linker_search_path(const char *name);
55static const char *linker_basename(const char* path);
56
57MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
58
59linker_file_t linker_kernel_file;
60
61static struct lock lock;	/* lock for the file list */
62static linker_class_list_t classes;
63static linker_file_list_t linker_files;
64static int next_file_id = 1;
65
66/* XXX wrong name; we're looking at version provision tags here, not modules */
67typedef TAILQ_HEAD(, modlist) modlisthead_t;
68struct modlist {
69    TAILQ_ENTRY(modlist) link;		/* chain together all modules */
70    linker_file_t	container;
71    const char		*name;
72};
73typedef struct modlist	*modlist_t;
74static modlisthead_t	found_modules;
75
76static char *
77linker_strdup(const char *str)
78{
79    char	*result;
80
81    if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
82	strcpy(result, str);
83    return(result);
84}
85
86static void
87linker_init(void* arg)
88{
89    lockinit(&lock, PVM, "klink", 0, 0);
90    TAILQ_INIT(&classes);
91    TAILQ_INIT(&linker_files);
92}
93
94SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
95
96int
97linker_add_class(linker_class_t lc)
98{
99    kobj_class_compile((kobj_class_t) lc);
100    TAILQ_INSERT_TAIL(&classes, lc, link);
101    return 0;
102}
103
104static void
105linker_file_sysinit(linker_file_t lf)
106{
107    struct linker_set* sysinits;
108    struct sysinit** sipp;
109    struct sysinit** xipp;
110    struct sysinit* save;
111
112    KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
113		   lf->filename));
114
115    sysinits = (struct linker_set*)
116	linker_file_lookup_symbol(lf, "sysinit_set", 0);
117
118    KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits));
119    if (!sysinits)
120	return;
121    /*
122     * Perform a bubble sort of the system initialization objects by
123     * their subsystem (primary key) and order (secondary key).
124     *
125     * Since some things care about execution order, this is the
126     * operation which ensures continued function.
127     */
128    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
129	for (xipp = sipp + 1; *xipp; xipp++) {
130	    if ((*sipp)->subsystem < (*xipp)->subsystem ||
131		 ((*sipp)->subsystem == (*xipp)->subsystem &&
132		  (*sipp)->order <= (*xipp)->order))
133		continue;	/* skip*/
134	    save = *sipp;
135	    *sipp = *xipp;
136	    *xipp = save;
137	}
138    }
139
140
141    /*
142     * Traverse the (now) ordered list of system initialization tasks.
143     * Perform each task, and continue on to the next task.
144     */
145    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
146	if ((*sipp)->subsystem == SI_SUB_DUMMY)
147	    continue;	/* skip dummy task(s)*/
148
149	/* Call function */
150	(*((*sipp)->func))((*sipp)->udata);
151    }
152}
153
154static void
155linker_file_sysuninit(linker_file_t lf)
156{
157    struct linker_set* sysuninits;
158    struct sysinit** sipp;
159    struct sysinit** xipp;
160    struct sysinit* save;
161
162    KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
163		   lf->filename));
164
165    sysuninits = (struct linker_set*)
166	linker_file_lookup_symbol(lf, "sysuninit_set", 0);
167
168    KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits));
169    if (!sysuninits)
170	return;
171
172    /*
173     * Perform a reverse bubble sort of the system initialization objects
174     * by their subsystem (primary key) and order (secondary key).
175     *
176     * Since some things care about execution order, this is the
177     * operation which ensures continued function.
178     */
179    for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
180	for (xipp = sipp + 1; *xipp; xipp++) {
181	    if ((*sipp)->subsystem > (*xipp)->subsystem ||
182		 ((*sipp)->subsystem == (*xipp)->subsystem &&
183		  (*sipp)->order >= (*xipp)->order))
184		continue;	/* skip*/
185	    save = *sipp;
186	    *sipp = *xipp;
187	    *xipp = save;
188	}
189    }
190
191    /*
192     * Traverse the (now) ordered list of system initialization tasks.
193     * Perform each task, and continue on to the next task.
194     */
195    for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
196	if ((*sipp)->subsystem == SI_SUB_DUMMY)
197	    continue;	/* skip dummy task(s)*/
198
199	/* Call function */
200	(*((*sipp)->func))((*sipp)->udata);
201    }
202}
203
204static void
205linker_file_register_sysctls(linker_file_t lf)
206{
207    struct linker_set* sysctls;
208
209    KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
210		   lf->filename));
211
212    sysctls = (struct linker_set*)
213	linker_file_lookup_symbol(lf, "sysctl_set", 0);
214
215    KLD_DPF(FILE, ("linker_file_register_sysctls: SYSCTLs %p\n", sysctls));
216    if (!sysctls)
217	return;
218
219    sysctl_register_set(sysctls);
220}
221
222static void
223linker_file_unregister_sysctls(linker_file_t lf)
224{
225    struct linker_set* sysctls;
226
227    KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
228		   lf->filename));
229
230    sysctls = (struct linker_set*)
231	linker_file_lookup_symbol(lf, "sysctl_set", 0);
232
233    KLD_DPF(FILE, ("linker_file_unregister_sysctls: SYSCTLs %p\n", sysctls));
234    if (!sysctls)
235	return;
236
237    sysctl_unregister_set(sysctls);
238}
239
240static int
241linker_file_register_modules(linker_file_t lf)
242{
243    int error, mcount;
244    struct linker_set *modules;
245    struct mod_metadata **mdpp;
246    const moduledata_t *moddata;
247    struct sysinit **sipp;
248
249    KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
250		   lf->filename));
251
252    modules = (struct linker_set*)
253	linker_file_lookup_symbol(lf, "modmetadata_set", 0);
254    mcount = 0;
255    if (modules) {
256	for (mdpp = (struct mod_metadata**)modules->ls_items; *mdpp; mdpp++) {
257	    if ((*mdpp)->md_type != MDT_MODULE)
258		continue;
259	    mcount++;
260	    moddata = (*mdpp)->md_data;
261	    KLD_DPF(FILE, ("Registering module %s in %s\n",
262                 moddata->name, lf->filename));
263	    error = module_register(moddata, lf);
264	    if (error)
265		printf("Module %s failed to register: %d\n", moddata->name, error);
266	}
267    }
268    if (mcount)
269	return mcount;	/* Do not mix old and new style */
270
271    /* Hack - handle old kld's without metadata */
272    modules = (struct linker_set*)
273	linker_file_lookup_symbol(lf, "sysinit_set", 0);
274    if (modules) {
275	for (sipp = (struct sysinit **)modules->ls_items; *sipp; sipp++) {
276	    if ((*sipp)->func != module_register_init)
277		continue;
278	    mcount++;
279	    moddata = (*sipp)->udata;
280	    printf("Old-style KLD file %s found\n", moddata->name);
281	    error = module_register(moddata, lf);
282	    if (error)
283		printf("Old-style KLD file %s failed to register: %d\n", moddata->name, error);
284	}
285    }
286    return mcount;
287}
288
289static void
290linker_init_kernel_modules(void)
291{
292    linker_file_register_modules(linker_kernel_file);
293}
294
295SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0);
296
297int
298linker_load_file(const char* filename, linker_file_t* result)
299{
300    linker_class_t lc;
301    linker_file_t lf;
302    int foundfile, error = 0;
303
304    /* Refuse to load modules if securelevel raised */
305    if (securelevel > 0)
306	return EPERM;
307
308    lf = linker_find_file_by_name(filename);
309    if (lf) {
310	KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
311	*result = lf;
312	lf->refs++;
313	goto out;
314    }
315
316    lf = NULL;
317    foundfile = 0;
318    for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
319	KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
320		       filename, lc->desc));
321	error = LINKER_LOAD_FILE(lc, filename, &lf);
322	/*
323	 * If we got something other than ENOENT, then it exists but we cannot
324	 * load it for some other reason.
325	 */
326	if (error != ENOENT)
327	    foundfile = 1;
328	if (lf) {
329	    linker_file_register_modules(lf);
330	    linker_file_register_sysctls(lf);
331	    linker_file_sysinit(lf);
332	    lf->flags |= LINKER_FILE_LINKED;
333
334	    *result = lf;
335	    error = 0;
336	    goto out;
337	}
338    }
339    /*
340     * Less than ideal, but tells the user whether it failed to load or
341     * the module was not found.
342     */
343    if (foundfile)
344	error = ENOEXEC;	/* Format not recognised (or unloadable) */
345    else
346	error = ENOENT;		/* Nothing found */
347
348out:
349    return error;
350}
351
352linker_file_t
353linker_find_file_by_name(const char* filename)
354{
355    linker_file_t lf = 0;
356    char *koname;
357
358    koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
359    if (koname == NULL)
360	goto out;
361    sprintf(koname, "%s.ko", filename);
362
363    lockmgr(&lock, LK_SHARED, 0, curproc);
364    for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
365	if (!strcmp(lf->filename, koname))
366	    break;
367	if (!strcmp(lf->filename, filename))
368	    break;
369    }
370    lockmgr(&lock, LK_RELEASE, 0, curproc);
371
372out:
373    if (koname)
374	free(koname, M_LINKER);
375    return lf;
376}
377
378linker_file_t
379linker_find_file_by_id(int fileid)
380{
381    linker_file_t lf = 0;
382
383    lockmgr(&lock, LK_SHARED, 0, curproc);
384    for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link))
385	if (lf->id == fileid)
386	    break;
387    lockmgr(&lock, LK_RELEASE, 0, curproc);
388
389    return lf;
390}
391
392linker_file_t
393linker_make_file(const char* pathname, linker_class_t lc)
394{
395    linker_file_t lf = 0;
396    const char *filename;
397
398    filename = linker_basename(pathname);
399
400    KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
401    lockmgr(&lock, LK_EXCLUSIVE, 0, curproc);
402    lf = (linker_file_t) kobj_create((kobj_class_t) lc, M_LINKER, M_WAITOK);
403    if (!lf)
404	goto out;
405
406    lf->refs = 1;
407    lf->userrefs = 0;
408    lf->flags = 0;
409    lf->filename = linker_strdup(filename);
410    lf->id = next_file_id++;
411    lf->ndeps = 0;
412    lf->deps = NULL;
413    STAILQ_INIT(&lf->common);
414    TAILQ_INIT(&lf->modules);
415
416    TAILQ_INSERT_TAIL(&linker_files, lf, link);
417
418out:
419    lockmgr(&lock, LK_RELEASE, 0, curproc);
420    return lf;
421}
422
423int
424linker_file_unload(linker_file_t file)
425{
426    module_t mod, next;
427    modlist_t ml, nextml;
428    struct common_symbol* cp;
429    int error = 0;
430    int i;
431
432    /* Refuse to unload modules if securelevel raised */
433    if (securelevel > 0)
434	return EPERM;
435
436    KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
437    lockmgr(&lock, LK_EXCLUSIVE, 0, curproc);
438    if (file->refs == 1) {
439	KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
440	/*
441	 * Inform any modules associated with this file.
442	 */
443	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
444	    next = module_getfnext(mod);
445
446	    /*
447	     * Give the module a chance to veto the unload.
448	     */
449	    if ((error = module_unload(mod)) != 0) {
450		KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
451			       mod));
452		lockmgr(&lock, LK_RELEASE, 0, curproc);
453		goto out;
454	    }
455
456	    module_release(mod);
457	}
458    }
459
460    file->refs--;
461    if (file->refs > 0) {
462	lockmgr(&lock, LK_RELEASE, 0, curproc);
463	goto out;
464    }
465
466    for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) {
467	nextml = TAILQ_NEXT(ml, link);
468	if (ml->container == file) {
469	    TAILQ_REMOVE(&found_modules, ml, link);
470	}
471    }
472
473    /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
474    if (file->flags & LINKER_FILE_LINKED) {
475	linker_file_sysuninit(file);
476	linker_file_unregister_sysctls(file);
477    }
478
479    TAILQ_REMOVE(&linker_files, file, link);
480    lockmgr(&lock, LK_RELEASE, 0, curproc);
481
482    if (file->deps) {
483	for (i = 0; i < file->ndeps; i++)
484	    linker_file_unload(file->deps[i]);
485	free(file->deps, M_LINKER);
486	file->deps = NULL;
487    }
488
489    for (cp = STAILQ_FIRST(&file->common); cp;
490	 cp = STAILQ_FIRST(&file->common)) {
491	STAILQ_REMOVE(&file->common, cp, common_symbol, link);
492	free(cp, M_LINKER);
493    }
494
495    LINKER_UNLOAD(file);
496    if (file->filename) {
497	free(file->filename, M_LINKER);
498	file->filename = NULL;
499    }
500    kobj_delete((kobj_t) file, M_LINKER);
501
502out:
503    return error;
504}
505
506int
507linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
508{
509    linker_file_t* newdeps;
510
511    newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
512		     M_LINKER, M_WAITOK);
513    if (newdeps == NULL)
514	return ENOMEM;
515    bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
516
517    if (file->deps) {
518	bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
519	free(file->deps, M_LINKER);
520    }
521    file->deps = newdeps;
522    file->deps[file->ndeps] = dep;
523    file->ndeps++;
524
525    return 0;
526}
527
528caddr_t
529linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
530{
531    c_linker_sym_t sym;
532    linker_symval_t symval;
533    caddr_t address;
534    size_t common_size = 0;
535    int i;
536
537    KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
538		  file, name, deps));
539
540    if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
541	LINKER_SYMBOL_VALUES(file, sym, &symval);
542	if (symval.value == 0)
543	    /*
544	     * For commons, first look them up in the dependancies and
545	     * only allocate space if not found there.
546	     */
547	    common_size = symval.size;
548	else {
549	    KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
550	    return symval.value;
551	}
552    }
553
554    if (deps) {
555	for (i = 0; i < file->ndeps; i++) {
556	    address = linker_file_lookup_symbol(file->deps[i], name, 0);
557	    if (address) {
558		KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
559		return address;
560	    }
561	}
562    }
563
564    if (common_size > 0) {
565	/*
566	 * This is a common symbol which was not found in the
567	 * dependancies.  We maintain a simple common symbol table in
568	 * the file object.
569	 */
570	struct common_symbol* cp;
571
572	for (cp = STAILQ_FIRST(&file->common); cp;
573	     cp = STAILQ_NEXT(cp, link))
574	    if (!strcmp(cp->name, name)) {
575		KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
576		return cp->address;
577	    }
578
579	/*
580	 * Round the symbol size up to align.
581	 */
582	common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
583	cp = malloc(sizeof(struct common_symbol)
584		    + common_size
585		    + strlen(name) + 1,
586		    M_LINKER, M_WAITOK);
587	if (!cp) {
588	    KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
589	    return 0;
590	}
591	bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
592
593	cp->address = (caddr_t) (cp + 1);
594	cp->name = cp->address + common_size;
595	strcpy(cp->name, name);
596	bzero(cp->address, common_size);
597	STAILQ_INSERT_TAIL(&file->common, cp, link);
598
599	KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
600	return cp->address;
601    }
602
603    KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
604    return 0;
605}
606
607#ifdef DDB
608/*
609 * DDB Helpers.  DDB has to look across multiple files with their own
610 * symbol tables and string tables.
611 *
612 * Note that we do not obey list locking protocols here.  We really don't
613 * need DDB to hang because somebody's got the lock held.  We'll take the
614 * chance that the files list is inconsistant instead.
615 */
616
617int
618linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
619{
620    linker_file_t lf;
621
622    for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
623	if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
624	    return 0;
625    }
626    return ENOENT;
627}
628
629int
630linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
631{
632    linker_file_t lf;
633    u_long off = (uintptr_t)value;
634    u_long diff, bestdiff;
635    c_linker_sym_t best;
636    c_linker_sym_t es;
637
638    best = 0;
639    bestdiff = off;
640    for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
641	if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
642	    continue;
643	if (es != 0 && diff < bestdiff) {
644	    best = es;
645	    bestdiff = diff;
646	}
647	if (bestdiff == 0)
648	    break;
649    }
650    if (best) {
651	*sym = best;
652	*diffp = bestdiff;
653	return 0;
654    } else {
655	*sym = 0;
656	*diffp = off;
657	return ENOENT;
658    }
659}
660
661int
662linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
663{
664    linker_file_t lf;
665
666    for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
667	if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
668	    return 0;
669    }
670    return ENOENT;
671}
672
673#endif
674
675/*
676 * Syscalls.
677 */
678
679int
680kldload(struct proc* p, struct kldload_args* uap)
681{
682    char* pathname, *realpath;
683    const char *filename;
684    linker_file_t lf;
685    int error = 0;
686
687    p->p_retval[0] = -1;
688
689    if (securelevel > 0)	/* redundant, but that's OK */
690	return EPERM;
691
692    if ((error = suser(p)) != 0)
693	return error;
694
695    realpath = NULL;
696    pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
697    if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0)
698	goto out;
699
700    realpath = linker_search_path(pathname);
701    if (realpath == NULL) {
702	error = ENOENT;
703	goto out;
704    }
705    /* Can't load more than one file with the same name */
706    filename = linker_basename(realpath);
707    if (linker_find_file_by_name(filename)) {
708	error = EEXIST;
709	goto out;
710    }
711
712    if ((error = linker_load_file(realpath, &lf)) != 0)
713	goto out;
714
715    lf->userrefs++;
716    p->p_retval[0] = lf->id;
717
718out:
719    if (pathname)
720	free(pathname, M_TEMP);
721    if (realpath)
722	free(realpath, M_LINKER);
723    return error;
724}
725
726int
727kldunload(struct proc* p, struct kldunload_args* uap)
728{
729    linker_file_t lf;
730    int error = 0;
731
732    if (securelevel > 0)	/* redundant, but that's OK */
733	return EPERM;
734
735    if ((error = suser(p)) != 0)
736	return error;
737
738    lf = linker_find_file_by_id(SCARG(uap, fileid));
739    if (lf) {
740	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
741	if (lf->userrefs == 0) {
742	    printf("kldunload: attempt to unload file that was loaded by the kernel\n");
743	    error = EBUSY;
744	    goto out;
745	}
746	lf->userrefs--;
747	error = linker_file_unload(lf);
748	if (error)
749	    lf->userrefs++;
750    } else
751	error = ENOENT;
752
753out:
754    return error;
755}
756
757int
758kldfind(struct proc* p, struct kldfind_args* uap)
759{
760    char* pathname;
761    const char *filename;
762    linker_file_t lf;
763    int error = 0;
764
765    p->p_retval[0] = -1;
766
767    pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
768    if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0)
769	goto out;
770
771    filename = linker_basename(pathname);
772
773    lf = linker_find_file_by_name(filename);
774    if (lf)
775	p->p_retval[0] = lf->id;
776    else
777	error = ENOENT;
778
779out:
780    if (pathname)
781	free(pathname, M_TEMP);
782    return error;
783}
784
785int
786kldnext(struct proc* p, struct kldnext_args* uap)
787{
788    linker_file_t lf;
789    int error = 0;
790
791    if (SCARG(uap, fileid) == 0) {
792	if (TAILQ_FIRST(&linker_files))
793	    p->p_retval[0] = TAILQ_FIRST(&linker_files)->id;
794	else
795	    p->p_retval[0] = 0;
796	return 0;
797    }
798
799    lf = linker_find_file_by_id(SCARG(uap, fileid));
800    if (lf) {
801	if (TAILQ_NEXT(lf, link))
802	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
803	else
804	    p->p_retval[0] = 0;
805    } else
806	error = ENOENT;
807
808    return error;
809}
810
811int
812kldstat(struct proc* p, struct kldstat_args* uap)
813{
814    linker_file_t lf;
815    int error = 0;
816    int version;
817    struct kld_file_stat* stat;
818    int namelen;
819
820    lf = linker_find_file_by_id(SCARG(uap, fileid));
821    if (!lf) {
822	error = ENOENT;
823	goto out;
824    }
825
826    stat = SCARG(uap, stat);
827
828    /*
829     * Check the version of the user's structure.
830     */
831    if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
832	goto out;
833    if (version != sizeof(struct kld_file_stat)) {
834	error = EINVAL;
835	goto out;
836    }
837
838    namelen = strlen(lf->filename) + 1;
839    if (namelen > MAXPATHLEN)
840	namelen = MAXPATHLEN;
841    if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
842	goto out;
843    if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
844	goto out;
845    if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
846	goto out;
847    if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
848	goto out;
849    if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
850	goto out;
851
852    p->p_retval[0] = 0;
853
854out:
855    return error;
856}
857
858int
859kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
860{
861    linker_file_t lf;
862    int error = 0;
863
864    lf = linker_find_file_by_id(SCARG(uap, fileid));
865    if (lf) {
866	if (TAILQ_FIRST(&lf->modules))
867	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
868	else
869	    p->p_retval[0] = 0;
870    } else
871	error = ENOENT;
872
873    return error;
874}
875
876int
877kldsym(struct proc *p, struct kldsym_args *uap)
878{
879    char *symstr = NULL;
880    c_linker_sym_t sym;
881    linker_symval_t symval;
882    linker_file_t lf;
883    struct kld_sym_lookup lookup;
884    int error = 0;
885
886    if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
887	goto out;
888    if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
889	error = EINVAL;
890	goto out;
891    }
892
893    symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
894    if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
895	goto out;
896
897    if (SCARG(uap, fileid) != 0) {
898	lf = linker_find_file_by_id(SCARG(uap, fileid));
899	if (lf == NULL) {
900	    error = ENOENT;
901	    goto out;
902	}
903	if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
904	    LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
905	    lookup.symvalue = (uintptr_t)symval.value;
906	    lookup.symsize = symval.size;
907	    error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
908	} else
909	    error = ENOENT;
910    } else {
911	for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
912	    if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
913		LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
914		lookup.symvalue = (uintptr_t)symval.value;
915		lookup.symsize = symval.size;
916		error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
917		break;
918	    }
919	}
920	if (!lf)
921	    error = ENOENT;
922    }
923out:
924    if (symstr)
925	free(symstr, M_TEMP);
926    return error;
927}
928
929/*
930 * Preloaded module support
931 */
932
933static modlist_t
934modlist_lookup(const char *name)
935{
936    modlist_t mod;
937
938    for (mod = TAILQ_FIRST(&found_modules); mod; mod = TAILQ_NEXT(mod, link)) {
939	if (!strcmp(mod->name, name))
940	    return mod;
941    }
942    return NULL;
943}
944
945/*
946 * This routine is cheap and nasty but will work for data pointers.
947 */
948static void *
949linker_reloc_ptr(linker_file_t lf, void *offset)
950{
951	return lf->address + (uintptr_t)offset;
952}
953
954static void
955linker_preload(void* arg)
956{
957    caddr_t		modptr;
958    char		*modname, *nmodname;
959    char		*modtype;
960    linker_file_t	lf;
961    linker_class_t	lc;
962    int			error, mcount;
963    struct linker_set	*sysinits;
964    linker_file_list_t	loaded_files;
965    linker_file_list_t	depended_files;
966    struct linker_set	*deps;
967    struct mod_metadata	*mp, *nmp;
968    int			i, j;
969    int			resolves;
970    modlist_t		mod;
971
972    TAILQ_INIT(&loaded_files);
973    TAILQ_INIT(&depended_files);
974    TAILQ_INIT(&found_modules);
975    error = 0;
976
977    modptr = NULL;
978    while ((modptr = preload_search_next_name(modptr)) != NULL) {
979	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
980	modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
981	if (modname == NULL) {
982	    printf("Preloaded module at %p does not have a name!\n", modptr);
983	    continue;
984	}
985	if (modtype == NULL) {
986	    printf("Preloaded module at %p does not have a type!\n", modptr);
987	    continue;
988	}
989	printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
990	lf = NULL;
991	for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
992	    error = LINKER_LINK_PRELOAD(lc, modname, &lf);
993	    if (error) {
994		lf = NULL;
995		break;
996	    }
997	}
998	if (lf)
999	    TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1000    }
1001
1002    /*
1003     * First get a list of stuff in the kernel.
1004     */
1005    deps = (struct linker_set*)
1006	linker_file_lookup_symbol(linker_kernel_file, MDT_SETNAME, 0);
1007    if (deps) {
1008	for (i = 0; i < deps->ls_length; i++) {
1009	    mp = deps->ls_items[i];
1010	    if (mp->md_type != MDT_VERSION)
1011		continue;
1012	    modname = mp->md_cval;
1013	    if (modlist_lookup(modname) != NULL) {
1014		printf("module %s already present!\n", modname);
1015		/* XXX what can we do? this is a build error. :-( */
1016		continue;
1017	    }
1018	    mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT);
1019	    if (mod == NULL)
1020		panic("no memory for module list");
1021	    bzero(mod, sizeof(*mod));
1022	    mod->container = linker_kernel_file;
1023	    mod->name = modname;
1024	    TAILQ_INSERT_TAIL(&found_modules, mod, link);
1025	}
1026    }
1027
1028    /*
1029     * this is a once-off kinky bubble sort
1030     * resolve relocation dependency requirements
1031     */
1032restart:
1033    for (lf = TAILQ_FIRST(&loaded_files); lf; lf = TAILQ_NEXT(lf, loaded)) {
1034	deps = (struct linker_set*)
1035	    linker_file_lookup_symbol(lf, MDT_SETNAME, 0);
1036	/*
1037	 * First, look to see if we would successfully link with this stuff.
1038	 */
1039	resolves = 1;	/* unless we know otherwise */
1040	if (deps) {
1041	    for (i = 0; i < deps->ls_length; i++) {
1042		mp = linker_reloc_ptr(lf, deps->ls_items[i]);
1043		if (mp->md_type != MDT_DEPEND)
1044		    continue;
1045		modname = linker_reloc_ptr(lf, mp->md_cval);
1046		for (j = 0; j < deps->ls_length; j++) {
1047		    nmp = linker_reloc_ptr(lf, deps->ls_items[j]);
1048		    if (nmp->md_type != MDT_VERSION)
1049			continue;
1050		    nmodname = linker_reloc_ptr(lf, nmp->md_cval);
1051		    if (strcmp(modname, nmodname) == 0)
1052			break;
1053		}
1054		if (j < deps->ls_length)	/* it's a self reference */
1055		    continue;
1056		if (modlist_lookup(modname) == NULL) {
1057		    /* ok, the module isn't here yet, we are not finished */
1058		    resolves = 0;
1059		}
1060	    }
1061	}
1062	/*
1063	 * OK, if we found our modules, we can link.  So, "provide" the
1064	 * modules inside and add it to the end of the link order list.
1065	 */
1066	if (resolves) {
1067	    if (deps) {
1068		for (i = 0; i < deps->ls_length; i++) {
1069		    mp = linker_reloc_ptr(lf, deps->ls_items[i]);
1070		    if (mp->md_type != MDT_VERSION)
1071			continue;
1072		    modname = linker_reloc_ptr(lf, mp->md_cval);
1073		    if (modlist_lookup(modname) != NULL) {
1074			printf("module %s already present!\n", modname);
1075			linker_file_unload(lf);
1076			TAILQ_REMOVE(&loaded_files, lf, loaded);
1077			goto restart;	/* we changed the tailq next ptr */
1078		    }
1079		    mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT);
1080		    if (mod == NULL)
1081			panic("no memory for module list");
1082		    bzero(mod, sizeof(*mod));
1083		    mod->container = lf;
1084		    mod->name = modname;
1085		    TAILQ_INSERT_TAIL(&found_modules, mod, link);
1086		}
1087	    }
1088	    TAILQ_REMOVE(&loaded_files, lf, loaded);
1089	    TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1090	    /*
1091	     * Since we provided modules, we need to restart the sort so
1092	     * that the previous files that depend on us have a chance.
1093	     * Also, we've busted the tailq next pointer with the REMOVE.
1094	     */
1095	    goto restart;
1096	}
1097    }
1098
1099    /*
1100     * At this point, we check to see what could not be resolved..
1101     */
1102    for (lf = TAILQ_FIRST(&loaded_files); lf; lf = TAILQ_NEXT(lf, loaded)) {
1103	printf("KLD file %s is missing dependencies\n", lf->filename);
1104	linker_file_unload(lf);
1105	TAILQ_REMOVE(&loaded_files, lf, loaded);
1106    }
1107
1108    /*
1109     * We made it. Finish off the linking in the order we determined.
1110     */
1111    for (lf = TAILQ_FIRST(&depended_files); lf; lf = TAILQ_NEXT(lf, loaded)) {
1112	if (linker_kernel_file) {
1113	    linker_kernel_file->refs++;
1114	    error = linker_file_add_dependancy(lf, linker_kernel_file);
1115	    if (error)
1116		panic("cannot add dependency");
1117	}
1118	lf->userrefs++;		/* so we can (try to) kldunload it */
1119	deps = (struct linker_set*)
1120	    linker_file_lookup_symbol(lf, MDT_SETNAME, 0);
1121	if (deps) {
1122	    for (i = 0; i < deps->ls_length; i++) {
1123		mp = linker_reloc_ptr(lf, deps->ls_items[i]);
1124		if (mp->md_type != MDT_DEPEND)
1125		    continue;
1126		modname = linker_reloc_ptr(lf, mp->md_cval);
1127		mod = modlist_lookup(modname);
1128		mod->container->refs++;
1129		error = linker_file_add_dependancy(lf, mod->container);
1130		if (error)
1131		    panic("cannot add dependency");
1132	    }
1133	}
1134
1135	/* Now do relocation etc using the symbol search paths established by the dependencies */
1136	error = LINKER_LINK_PRELOAD_FINISH(lf);
1137	if (error) {
1138	    printf("KLD file %s - could not finalize loading\n", lf->filename);
1139	    linker_file_unload(lf);
1140	    continue;
1141	}
1142
1143	mcount = linker_file_register_modules(lf);
1144	sysinits = (struct linker_set*)
1145	    linker_file_lookup_symbol(lf, "sysinit_set", 0);
1146	if (sysinits)
1147	    sysinit_add((struct sysinit **)sysinits->ls_items);
1148	linker_file_register_sysctls(lf);
1149	lf->flags |= LINKER_FILE_LINKED;
1150    }
1151    /* woohoo! we made it! */
1152}
1153
1154SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1155
1156/*
1157 * Search for a not-loaded module by name.
1158 *
1159 * Modules may be found in the following locations:
1160 *
1161 * - preloaded (result is just the module name)
1162 * - on disk (result is full path to module)
1163 *
1164 * If the module name is qualified in any way (contains path, etc.)
1165 * the we simply return a copy of it.
1166 *
1167 * The search path can be manipulated via sysctl.  Note that we use the ';'
1168 * character as a separator to be consistent with the bootloader.
1169 */
1170
1171static char linker_path[MAXPATHLEN] = "/boot/modules/;/modules/;/boot/kernel/";
1172
1173SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1174	      sizeof(linker_path), "module load search path");
1175
1176static char *linker_ext_list[] = {
1177	".ko",
1178	"",
1179	NULL
1180};
1181
1182static char *
1183linker_search_path(const char *name)
1184{
1185    struct nameidata	nd;
1186    struct proc		*p = curproc;	/* XXX */
1187    char		*cp, *ep, *result, **cpp;
1188    int			error, extlen, len, flags;
1189    enum vtype		type;
1190
1191    /* qualified at all? */
1192    if (index(name, '/'))
1193	return(linker_strdup(name));
1194
1195    extlen = 0;
1196    for (cpp = linker_ext_list; *cpp; cpp++) {
1197	len = strlen(*cpp);
1198	if (len > extlen)
1199	    extlen = len;
1200    }
1201    extlen++;	/* trailing '\0' */
1202
1203    /* traverse the linker path */
1204    cp = linker_path;
1205    len = strlen(name);
1206    for (;;) {
1207
1208	/* find the end of this component */
1209	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1210	    ;
1211	result = malloc((len + (ep - cp) + extlen), M_LINKER, M_WAITOK);
1212	if (result == NULL)	/* actually ENOMEM */
1213	    return(NULL);
1214	for (cpp = linker_ext_list; *cpp; cpp++) {
1215	    strncpy(result, cp, ep - cp);
1216	    strcpy(result + (ep - cp), name);
1217	    strcat(result, *cpp);
1218
1219	    /*
1220	     * Attempt to open the file, and return the path if we succeed
1221	     * and it's a regular file.
1222	     */
1223	    NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
1224	    flags = FREAD;
1225	    error = vn_open(&nd, &flags, 0);
1226	    if (error == 0) {
1227		NDFREE(&nd, NDF_ONLY_PNBUF);
1228		type = nd.ni_vp->v_type;
1229		VOP_UNLOCK(nd.ni_vp, 0, p);
1230		vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
1231		if (type == VREG)
1232		    return(result);
1233	    }
1234	}
1235	free(result, M_LINKER);
1236
1237	if (*ep == 0)
1238	    break;
1239	cp = ep + 1;
1240    }
1241    return(NULL);
1242}
1243
1244static const char *
1245linker_basename(const char* path)
1246{
1247    const char *filename;
1248
1249    filename = rindex(path, '/');
1250    if (filename == NULL)
1251	return path;
1252    if (filename[1])
1253	filename++;
1254    return filename;
1255}
1256
1257/*
1258 * Find a file which contains given module and load it,
1259 * if "parent" is not NULL, register a reference to it.
1260 */
1261static int
1262linker_load_module(const char *modname, struct linker_file *parent)
1263{
1264    linker_file_t lfdep;
1265    const char *filename;
1266    char *pathname;
1267    int error;
1268
1269    /*
1270     * There will be a system to look up or guess a file name from
1271     * a module name.
1272     * For now we just try to load a file with the same name.
1273     */
1274    pathname = linker_search_path(modname);
1275    if (pathname == NULL)
1276	return ENOENT;
1277
1278    /* Can't load more than one file with the same basename */
1279    filename = linker_basename(pathname);
1280    if (linker_find_file_by_name(filename)) {
1281	error = EEXIST;
1282	goto out;
1283    }
1284
1285    do {
1286	error = linker_load_file(pathname, &lfdep);
1287	if (error)
1288	    break;
1289	if (parent) {
1290	    error = linker_file_add_dependancy(parent, lfdep);
1291	    if (error)
1292		break;
1293	}
1294    } while(0);
1295out:
1296    if (pathname)
1297	free(pathname, M_LINKER);
1298    return error;
1299}
1300
1301/*
1302 * This routine is responsible for finding dependencies of userland
1303 * initiated kldload(2)'s of files.
1304 */
1305int
1306linker_load_dependancies(linker_file_t lf)
1307{
1308    linker_file_t lfdep;
1309    struct linker_set *deps;
1310    struct mod_metadata *mp, *nmp;
1311    modlist_t mod;
1312    char *modname, *nmodname;
1313    int i, j, error = 0;
1314
1315    /*
1316     * All files are dependant on /kernel.
1317     */
1318    if (linker_kernel_file) {
1319	linker_kernel_file->refs++;
1320	error = linker_file_add_dependancy(lf, linker_kernel_file);
1321	if (error)
1322	    return error;
1323    }
1324
1325    deps = (struct linker_set*)
1326	linker_file_lookup_symbol(lf, MDT_SETNAME, 0);
1327    if (deps != NULL) {
1328	for (i = 0; i < deps->ls_length; i++) {
1329	    mp = linker_reloc_ptr(lf, deps->ls_items[i]);
1330	    if (mp->md_type != MDT_VERSION)
1331		continue;
1332	    modname = linker_reloc_ptr(lf, mp->md_cval);
1333	    if (modlist_lookup(modname) != NULL) {
1334		printf("module %s already present!\n", modname);
1335		return EEXIST;
1336	    }
1337	}
1338    }
1339    if (deps != NULL) {
1340	for (i = 0; i < deps->ls_length; i++) {
1341	    mp = linker_reloc_ptr(lf, deps->ls_items[i]);
1342	    if (mp->md_type != MDT_DEPEND)
1343		continue;
1344	    modname = linker_reloc_ptr(lf, mp->md_cval);
1345	    nmodname = NULL;
1346	    for (j = 0; j < deps->ls_length; j++) {
1347		nmp = linker_reloc_ptr(lf, deps->ls_items[j]);
1348		if (nmp->md_type != MDT_VERSION)
1349		    continue;
1350		nmodname = linker_reloc_ptr(lf, nmp->md_cval);
1351		if (strcmp(modname, nmodname) == 0)
1352		    break;
1353	    }
1354	    if (j < deps->ls_length)	/* early exit, it's a self reference */
1355		continue;
1356	    mod = modlist_lookup(modname);
1357	    if (mod) {		/* woohoo, it's loaded already */
1358		lfdep = mod->container;
1359		lfdep->refs++;
1360		error = linker_file_add_dependancy(lf, lfdep);
1361		if (error)
1362		    break;
1363		continue;
1364	    }
1365	    error = linker_load_module(modname, lf);
1366	    if (error) {
1367		printf("KLD %s: depends on %s - not available\n",
1368		       lf->filename, modname);
1369		break;
1370	    }
1371	}
1372
1373    }
1374    if (error == 0 && deps) {
1375	for (i = 0; i < deps->ls_length; i++) {
1376	    mp = linker_reloc_ptr(lf, deps->ls_items[i]);
1377	    if (mp->md_type != MDT_VERSION)
1378		continue;
1379	    modname = linker_reloc_ptr(lf, mp->md_cval);
1380	    mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT);
1381	    if (mod == NULL)
1382		panic("no memory for module list");
1383	    bzero(mod, sizeof(*mod));
1384	    mod->container = lf;
1385	    mod->name = modname;
1386	    TAILQ_INSERT_TAIL(&found_modules, mod, link);
1387	}
1388    }
1389    return error;
1390}
1391