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