kern_linker.c revision 83358
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 83358 2001-09-12 00:50:23Z 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/mutex.h>
40#include <sys/module.h>
41#include <sys/linker.h>
42#include <sys/fcntl.h>
43#include <sys/libkern.h>
44#include <sys/namei.h>
45#include <sys/vnode.h>
46#include <sys/sysctl.h>
47
48
49#include "linker_if.h"
50
51#ifdef KLD_DEBUG
52int kld_debug = 0;
53#endif
54
55/*static char *linker_search_path(const char *name, struct mod_depend *verinfo);*/
56static const char *linker_basename(const char* path);
57static int linker_load_module(const char *kldname, const char *modname,
58	struct linker_file *parent, struct mod_depend *verinfo,
59	struct linker_file **lfpp);
60
61/* Metadata from the static kernel */
62SET_DECLARE(modmetadata_set, struct mod_metadata);
63
64MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
65
66linker_file_t linker_kernel_file;
67
68static struct lock lock;	/* lock for the file list */
69static linker_class_list_t classes;
70static linker_file_list_t linker_files;
71static int next_file_id = 1;
72
73/* XXX wrong name; we're looking at version provision tags here, not modules */
74typedef TAILQ_HEAD(, modlist) modlisthead_t;
75struct modlist {
76    TAILQ_ENTRY(modlist) link;		/* chain together all modules */
77    linker_file_t	container;
78    const char		*name;
79    int			version;
80};
81typedef struct modlist	*modlist_t;
82static modlisthead_t	found_modules;
83
84static char *
85linker_strdup(const char *str)
86{
87    char	*result;
88
89    if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
90	strcpy(result, str);
91    return(result);
92}
93
94static void
95linker_init(void* arg)
96{
97    lockinit(&lock, PVM, "klink", 0, 0);
98    TAILQ_INIT(&classes);
99    TAILQ_INIT(&linker_files);
100}
101
102SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
103
104int
105linker_add_class(linker_class_t lc)
106{
107    kobj_class_compile((kobj_class_t) lc);
108    TAILQ_INSERT_TAIL(&classes, lc, link);
109    return 0;
110}
111
112static void
113linker_file_sysinit(linker_file_t lf)
114{
115    struct sysinit** start, ** stop;
116    struct sysinit** sipp;
117    struct sysinit** xipp;
118    struct sysinit* save;
119
120    KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
121		   lf->filename));
122
123    if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
124	return;
125    /*
126     * Perform a bubble sort of the system initialization objects by
127     * their subsystem (primary key) and order (secondary key).
128     *
129     * Since some things care about execution order, this is the
130     * operation which ensures continued function.
131     */
132    for (sipp = start; sipp < stop; sipp++) {
133	for (xipp = sipp + 1; xipp < stop; xipp++) {
134	    if ((*sipp)->subsystem < (*xipp)->subsystem ||
135		 ((*sipp)->subsystem == (*xipp)->subsystem &&
136		  (*sipp)->order <= (*xipp)->order))
137		continue;	/* skip*/
138	    save = *sipp;
139	    *sipp = *xipp;
140	    *xipp = save;
141	}
142    }
143
144
145    /*
146     * Traverse the (now) ordered list of system initialization tasks.
147     * Perform each task, and continue on to the next task.
148     */
149    for (sipp = start; sipp < stop; sipp++) {
150	if ((*sipp)->subsystem == SI_SUB_DUMMY)
151	    continue;	/* skip dummy task(s)*/
152
153	/* Call function */
154	(*((*sipp)->func))((*sipp)->udata);
155    }
156}
157
158static void
159linker_file_sysuninit(linker_file_t lf)
160{
161    struct sysinit** start, ** stop;
162    struct sysinit** sipp;
163    struct sysinit** xipp;
164    struct sysinit* save;
165
166    KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
167		   lf->filename));
168
169    if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
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 = start; sipp < stop; sipp++) {
180	for (xipp = sipp + 1; xipp < stop; xipp++) {
181	    if ((*sipp)->subsystem > (*xipp)->subsystem ||
182		 ((*sipp)->subsystem == (*xipp)->subsystem &&
183		  (*sipp)->order >= (*xipp)->order))
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 = start; sipp < stop; 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 sysctl_oid **start, **stop, **oidp;
208
209    KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
210		   lf->filename));
211
212    if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
213	return;
214
215    for (oidp = start; oidp < stop; oidp++)
216	sysctl_register_oid(*oidp);
217}
218
219static void
220linker_file_unregister_sysctls(linker_file_t lf)
221{
222    struct sysctl_oid **start, **stop, **oidp;
223
224    KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
225		   lf->filename));
226
227    if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
228	return;
229
230    for (oidp = start; oidp < stop; oidp++)
231	sysctl_unregister_oid(*oidp);
232}
233
234static int
235linker_file_register_modules(linker_file_t lf)
236{
237    int error;
238    struct mod_metadata **start, **stop;
239    struct mod_metadata **mdp;
240    const moduledata_t *moddata;
241
242    KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
243		   lf->filename));
244
245    if (linker_file_lookup_set(lf, "modmetadata_set", &start, &stop, 0) != 0) {
246	/*
247	 * This fallback should be unnecessary, but if we get booted from
248	 * boot2 instead of loader and we are missing our metadata then
249	 * we have to try the best we can.
250	 */
251	if (lf == linker_kernel_file) {
252	    start = SET_BEGIN(modmetadata_set);
253	    stop = SET_LIMIT(modmetadata_set);
254	} else {
255	    return 0;
256	}
257    }
258    for (mdp = start; mdp < stop; mdp++) {
259	if ((*mdp)->md_type != MDT_MODULE)
260	    continue;
261	moddata = (*mdp)->md_data;
262	KLD_DPF(FILE, ("Registering module %s in %s\n",
263             moddata->name, lf->filename));
264	if (module_lookupbyname(moddata->name) != NULL) {
265	    printf("Warning: module %s already exists\n", moddata->name);
266	    continue;	/* or return a error ? */
267	}
268	error = module_register(moddata, lf);
269	if (error)
270	    printf("Module %s failed to register: %d\n", moddata->name, error);
271    }
272    return 0;
273}
274
275static void
276linker_init_kernel_modules(void)
277{
278    linker_file_register_modules(linker_kernel_file);
279}
280
281SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0);
282
283int
284linker_load_file(const char* filename, linker_file_t* result)
285{
286    linker_class_t lc;
287    linker_file_t lf;
288    int foundfile, error = 0;
289
290    /* Refuse to load modules if securelevel raised */
291    if (securelevel > 0)
292	return EPERM;
293
294    lf = linker_find_file_by_name(filename);
295    if (lf) {
296	KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
297	*result = lf;
298	lf->refs++;
299	goto out;
300    }
301
302    lf = NULL;
303    foundfile = 0;
304    TAILQ_FOREACH(lc, &classes, link) {
305	KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
306		       filename));
307	error = LINKER_LOAD_FILE(lc, filename, &lf);
308	/*
309	 * If we got something other than ENOENT, then it exists but we cannot
310	 * load it for some other reason.
311	 */
312	if (error != ENOENT)
313	    foundfile = 1;
314	if (lf) {
315	    linker_file_register_modules(lf);
316	    linker_file_register_sysctls(lf);
317	    linker_file_sysinit(lf);
318	    lf->flags |= LINKER_FILE_LINKED;
319
320	    *result = lf;
321	    error = 0;
322	    goto out;
323	}
324    }
325    /*
326     * Less than ideal, but tells the user whether it failed to load or
327     * the module was not found.
328     */
329    if (foundfile)
330	error = ENOEXEC;	/* Format not recognised (or unloadable) */
331    else
332	error = ENOENT;		/* Nothing found */
333
334out:
335    return error;
336}
337
338/* XXX: function parameters are incomplete */
339int
340linker_reference_module(const char *modname, linker_file_t *result)
341{
342
343    return linker_load_module(NULL, modname, NULL, NULL, result);
344}
345
346linker_file_t
347linker_find_file_by_name(const char* filename)
348{
349    linker_file_t lf = 0;
350    char *koname;
351
352    koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
353    if (koname == NULL)
354	goto out;
355    sprintf(koname, "%s.ko", filename);
356
357    lockmgr(&lock, LK_SHARED, 0, curproc);
358    TAILQ_FOREACH(lf, &linker_files, link) {
359	if (!strcmp(lf->filename, koname))
360	    break;
361	if (!strcmp(lf->filename, filename))
362	    break;
363    }
364    lockmgr(&lock, LK_RELEASE, 0, curproc);
365
366out:
367    if (koname)
368	free(koname, M_LINKER);
369    return lf;
370}
371
372linker_file_t
373linker_find_file_by_id(int fileid)
374{
375    linker_file_t lf = 0;
376
377    lockmgr(&lock, LK_SHARED, 0, curproc);
378    TAILQ_FOREACH(lf, &linker_files, link)
379	if (lf->id == fileid)
380	    break;
381    lockmgr(&lock, LK_RELEASE, 0, curproc);
382
383    return lf;
384}
385
386linker_file_t
387linker_make_file(const char* pathname, linker_class_t lc)
388{
389    linker_file_t lf = 0;
390    const char *filename;
391
392    filename = linker_basename(pathname);
393
394    KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
395    lockmgr(&lock, LK_EXCLUSIVE, 0, curproc);
396    lf = (linker_file_t) kobj_create((kobj_class_t) lc, M_LINKER, M_WAITOK);
397    if (!lf)
398	goto out;
399
400    lf->refs = 1;
401    lf->userrefs = 0;
402    lf->flags = 0;
403    lf->filename = linker_strdup(filename);
404    lf->id = next_file_id++;
405    lf->ndeps = 0;
406    lf->deps = NULL;
407    STAILQ_INIT(&lf->common);
408    TAILQ_INIT(&lf->modules);
409
410    TAILQ_INSERT_TAIL(&linker_files, lf, link);
411
412out:
413    lockmgr(&lock, LK_RELEASE, 0, curproc);
414    return lf;
415}
416
417int
418linker_file_unload(linker_file_t file)
419{
420    module_t mod, next;
421    modlist_t ml, nextml;
422    struct common_symbol* cp;
423    int error = 0;
424    int i;
425
426    /* Refuse to unload modules if securelevel raised */
427    if (securelevel > 0)
428	return EPERM;
429
430    KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
431    lockmgr(&lock, LK_EXCLUSIVE, 0, curproc);
432    if (file->refs == 1) {
433	KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
434	/*
435	 * Inform any modules associated with this file.
436	 */
437	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
438	    next = module_getfnext(mod);
439
440	    /*
441	     * Give the module a chance to veto the unload.
442	     */
443	    if ((error = module_unload(mod)) != 0) {
444		KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
445			       mod));
446		lockmgr(&lock, LK_RELEASE, 0, curproc);
447		goto out;
448	    }
449
450	    module_release(mod);
451	}
452    }
453
454    file->refs--;
455    if (file->refs > 0) {
456	lockmgr(&lock, LK_RELEASE, 0, curproc);
457	goto out;
458    }
459
460    for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) {
461	nextml = TAILQ_NEXT(ml, link);
462	if (ml->container == file) {
463	    TAILQ_REMOVE(&found_modules, ml, link);
464	}
465    }
466
467    /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
468    if (file->flags & LINKER_FILE_LINKED) {
469	linker_file_sysuninit(file);
470	linker_file_unregister_sysctls(file);
471    }
472
473    TAILQ_REMOVE(&linker_files, file, link);
474    lockmgr(&lock, LK_RELEASE, 0, curproc);
475
476    if (file->deps) {
477	for (i = 0; i < file->ndeps; i++)
478	    linker_file_unload(file->deps[i]);
479	free(file->deps, M_LINKER);
480	file->deps = NULL;
481    }
482
483    for (cp = STAILQ_FIRST(&file->common); cp;
484	 cp = STAILQ_FIRST(&file->common)) {
485	STAILQ_REMOVE(&file->common, cp, common_symbol, link);
486	free(cp, M_LINKER);
487    }
488
489    LINKER_UNLOAD(file);
490    if (file->filename) {
491	free(file->filename, M_LINKER);
492	file->filename = NULL;
493    }
494    kobj_delete((kobj_t) file, M_LINKER);
495
496out:
497    return error;
498}
499
500int
501linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
502{
503    linker_file_t* newdeps;
504
505    newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
506		     M_LINKER, M_WAITOK | M_ZERO);
507    if (newdeps == NULL)
508	return ENOMEM;
509
510    if (file->deps) {
511	bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
512	free(file->deps, M_LINKER);
513    }
514    file->deps = newdeps;
515    file->deps[file->ndeps] = dep;
516    file->ndeps++;
517
518    return 0;
519}
520
521/*
522 * Locate a linker set and its contents.
523 * This is a helper function to avoid linker_if.h exposure elsewhere.
524 * Note: firstp and lastp are really void ***
525 */
526int
527linker_file_lookup_set(linker_file_t file, const char *name,
528		       void *firstp, void *lastp, int *countp)
529{
530
531    return LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
532}
533
534caddr_t
535linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
536{
537    c_linker_sym_t sym;
538    linker_symval_t symval;
539    caddr_t address;
540    size_t common_size = 0;
541    int i;
542
543    KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
544		  file, name, deps));
545
546    if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
547	LINKER_SYMBOL_VALUES(file, sym, &symval);
548	if (symval.value == 0)
549	    /*
550	     * For commons, first look them up in the dependancies and
551	     * only allocate space if not found there.
552	     */
553	    common_size = symval.size;
554	else {
555	    KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
556	    return symval.value;
557	}
558    }
559
560    if (deps) {
561	for (i = 0; i < file->ndeps; i++) {
562	    address = linker_file_lookup_symbol(file->deps[i], name, 0);
563	    if (address) {
564		KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
565		return address;
566	    }
567	}
568    }
569
570    if (common_size > 0) {
571	/*
572	 * This is a common symbol which was not found in the
573	 * dependancies.  We maintain a simple common symbol table in
574	 * the file object.
575	 */
576	struct common_symbol* cp;
577
578	STAILQ_FOREACH(cp, &file->common, 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    TAILQ_FOREACH(lf, &linker_files, 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    TAILQ_FOREACH(lf, &linker_files, 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    TAILQ_FOREACH(lf, &linker_files, 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/*
683 * MPSAFE
684 */
685int
686kldload(struct proc* p, struct kldload_args* uap)
687{
688    char *kldname, *modname;
689    char *pathname = NULL;
690    linker_file_t lf;
691    int error = 0;
692
693    p->p_retval[0] = -1;
694
695    if (securelevel > 0)	/* redundant, but that's OK */
696	return EPERM;
697
698    mtx_lock(&Giant);
699
700    if ((error = suser(p)) != 0)
701	goto out;
702
703    pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
704    if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0)
705	goto out;
706
707    /*
708     * If path do not contain qualified name or any dot in it (kldname.ko, or
709     * kldname.ver.ko) treat it as interface name.
710     */
711    if (index(pathname, '/') || index(pathname, '.')) {
712	kldname = pathname;
713	modname = NULL;
714    } else {
715	kldname = NULL;
716	modname = pathname;
717    }
718    error = linker_load_module(kldname, modname, NULL, NULL, &lf);
719    if (error)
720	goto out;
721
722    lf->userrefs++;
723    p->p_retval[0] = lf->id;
724
725out:
726    if (pathname)
727	free(pathname, M_TEMP);
728    mtx_unlock(&Giant);
729    return (error);
730}
731
732/*
733 * MPSAFE
734 */
735int
736kldunload(struct proc* p, struct kldunload_args* uap)
737{
738    linker_file_t lf;
739    int error = 0;
740
741    if (securelevel > 0)	/* redundant, but that's OK */
742	return EPERM;
743
744    mtx_lock(&Giant);
745
746    if ((error = suser(p)) != 0)
747	goto out;
748
749    lf = linker_find_file_by_id(SCARG(uap, fileid));
750    if (lf) {
751	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
752	if (lf->userrefs == 0) {
753	    printf("kldunload: attempt to unload file that was loaded by the kernel\n");
754	    error = EBUSY;
755	    goto out;
756	}
757	lf->userrefs--;
758	error = linker_file_unload(lf);
759	if (error)
760	    lf->userrefs++;
761    } else {
762	error = ENOENT;
763    }
764out:
765    mtx_unlock(&Giant);
766    return (error);
767}
768
769/*
770 * MPSAFE
771 */
772int
773kldfind(struct proc* p, struct kldfind_args* uap)
774{
775    char* pathname;
776    const char *filename;
777    linker_file_t lf;
778    int error = 0;
779
780    mtx_lock(&Giant);
781
782    p->p_retval[0] = -1;
783
784    pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
785    if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0)
786	goto out;
787
788    filename = linker_basename(pathname);
789
790    lf = linker_find_file_by_name(filename);
791    if (lf)
792	p->p_retval[0] = lf->id;
793    else
794	error = ENOENT;
795
796out:
797    if (pathname)
798	free(pathname, M_TEMP);
799    mtx_unlock(&Giant);
800    return (error);
801}
802
803/*
804 * MPSAFE
805 */
806int
807kldnext(struct proc* p, struct kldnext_args* uap)
808{
809    linker_file_t lf;
810    int error = 0;
811
812    mtx_lock(&Giant);
813
814    if (SCARG(uap, fileid) == 0) {
815	if (TAILQ_FIRST(&linker_files))
816	    p->p_retval[0] = TAILQ_FIRST(&linker_files)->id;
817	else
818	    p->p_retval[0] = 0;
819	goto out;
820    }
821
822    lf = linker_find_file_by_id(SCARG(uap, fileid));
823    if (lf) {
824	if (TAILQ_NEXT(lf, link))
825	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
826	else
827	    p->p_retval[0] = 0;
828    } else {
829	error = ENOENT;
830    }
831out:
832    mtx_unlock(&Giant);
833    return (error);
834}
835
836/*
837 * MPSAFE
838 */
839int
840kldstat(struct proc* p, struct kldstat_args* uap)
841{
842    linker_file_t lf;
843    int error = 0;
844    int version;
845    struct kld_file_stat* stat;
846    int namelen;
847
848    mtx_lock(&Giant);
849
850    lf = linker_find_file_by_id(SCARG(uap, fileid));
851    if (!lf) {
852	error = ENOENT;
853	goto out;
854    }
855
856    stat = SCARG(uap, stat);
857
858    /*
859     * Check the version of the user's structure.
860     */
861    if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
862	goto out;
863    if (version != sizeof(struct kld_file_stat)) {
864	error = EINVAL;
865	goto out;
866    }
867
868    namelen = strlen(lf->filename) + 1;
869    if (namelen > MAXPATHLEN)
870	namelen = MAXPATHLEN;
871    if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
872	goto out;
873    if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
874	goto out;
875    if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
876	goto out;
877    if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
878	goto out;
879    if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
880	goto out;
881
882    p->p_retval[0] = 0;
883
884out:
885    mtx_unlock(&Giant);
886    return (error);
887}
888
889/*
890 * MPSAFE
891 */
892int
893kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
894{
895    linker_file_t lf;
896    int error = 0;
897
898    mtx_lock(&Giant);
899    lf = linker_find_file_by_id(SCARG(uap, fileid));
900    if (lf) {
901	if (TAILQ_FIRST(&lf->modules))
902	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
903	else
904	    p->p_retval[0] = 0;
905    } else {
906	error = ENOENT;
907    }
908    mtx_unlock(&Giant);
909    return (error);
910}
911
912/*
913 * MPSAFE
914 */
915int
916kldsym(struct proc *p, struct kldsym_args *uap)
917{
918    char *symstr = NULL;
919    c_linker_sym_t sym;
920    linker_symval_t symval;
921    linker_file_t lf;
922    struct kld_sym_lookup lookup;
923    int error = 0;
924
925    mtx_lock(&Giant);
926
927    if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
928	goto out;
929    if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
930	error = EINVAL;
931	goto out;
932    }
933
934    symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
935    if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
936	goto out;
937
938    if (SCARG(uap, fileid) != 0) {
939	lf = linker_find_file_by_id(SCARG(uap, fileid));
940	if (lf == NULL) {
941	    error = ENOENT;
942	    goto out;
943	}
944	if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
945	    LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
946	    lookup.symvalue = (uintptr_t)symval.value;
947	    lookup.symsize = symval.size;
948	    error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
949	} else
950	    error = ENOENT;
951    } else {
952	TAILQ_FOREACH(lf, &linker_files, link) {
953	    if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
954		LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
955		lookup.symvalue = (uintptr_t)symval.value;
956		lookup.symsize = symval.size;
957		error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
958		break;
959	    }
960	}
961	if (!lf)
962	    error = ENOENT;
963    }
964out:
965    if (symstr)
966	free(symstr, M_TEMP);
967    mtx_unlock(&Giant);
968    return (error);
969}
970
971/*
972 * Preloaded module support
973 */
974
975static modlist_t
976modlist_lookup(const char *name, int ver)
977{
978    modlist_t mod;
979
980    TAILQ_FOREACH(mod, &found_modules, link) {
981	if (strcmp(mod->name, name) == 0 && (ver == 0 || mod->version == ver))
982	    return mod;
983    }
984    return NULL;
985}
986
987static modlist_t
988modlist_lookup2(const char *name, struct mod_depend *verinfo)
989{
990    modlist_t mod, bestmod;
991    int ver;
992
993    if (verinfo == NULL)
994	return modlist_lookup(name, 0);
995    bestmod = NULL;
996    for (mod = TAILQ_FIRST(&found_modules); mod; mod = TAILQ_NEXT(mod, link)) {
997	if (strcmp(mod->name, name) != 0)
998	    continue;
999	ver = mod->version;
1000	if (ver == verinfo->md_ver_preferred)
1001	    return mod;
1002	if (ver >= verinfo->md_ver_minimum &&
1003	    ver <= verinfo->md_ver_maximum &&
1004	    ver > bestmod->version)
1005	    bestmod = mod;
1006    }
1007    return bestmod;
1008}
1009
1010static modlist_t
1011modlist_newmodule(const char *modname, int version, linker_file_t container)
1012{
1013    modlist_t mod;
1014
1015    mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT);
1016    if (mod == NULL)
1017	panic("no memory for module list");
1018    bzero(mod, sizeof(*mod));
1019    mod->container = container;
1020    mod->name = modname;
1021    mod->version = version;
1022    TAILQ_INSERT_TAIL(&found_modules, mod, link);
1023    return mod;
1024}
1025
1026/*
1027 * This routine is cheap and nasty but will work for data pointers.
1028 */
1029static void *
1030linker_reloc_ptr(linker_file_t lf, const void *offset)
1031{
1032	return lf->address + (uintptr_t)offset;
1033}
1034
1035/*
1036 * Dereference MDT_VERSION metadata into module name and version
1037 */
1038static void
1039linker_mdt_version(linker_file_t lf, struct mod_metadata *mp,
1040	const char **modname, int *version)
1041{
1042    struct mod_version *mvp;
1043
1044    if (modname)
1045	*modname = linker_reloc_ptr(lf, mp->md_cval);
1046    if (version) {
1047	mvp = linker_reloc_ptr(lf, mp->md_data);
1048	*version = mvp->mv_version;
1049    }
1050}
1051
1052/*
1053 * Dereference MDT_DEPEND metadata into module name and mod_depend structure
1054 */
1055static void
1056linker_mdt_depend(linker_file_t lf, struct mod_metadata *mp,
1057	const char **modname, struct mod_depend **verinfo)
1058{
1059
1060    if (modname)
1061	*modname = linker_reloc_ptr(lf, mp->md_cval);
1062    if (verinfo)
1063	*verinfo = linker_reloc_ptr(lf, mp->md_data);
1064}
1065
1066static void
1067linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1068	struct mod_metadata **stop, int preload)
1069{
1070    struct mod_metadata	*mp, **mdp;
1071    const char *modname;
1072    int ver;
1073
1074    for (mdp = start; mdp < stop; mdp++) {
1075	if (preload)
1076	    mp = *mdp;
1077	else
1078	    mp = linker_reloc_ptr(lf, *mdp);
1079	if (mp->md_type != MDT_VERSION)
1080	    continue;
1081	if (preload) {
1082	    modname = mp->md_cval;
1083	    ver = ((struct mod_version*)mp->md_data)->mv_version;
1084	} else
1085	    linker_mdt_version(lf, mp, &modname, &ver);
1086	if (modlist_lookup(modname, ver) != NULL) {
1087	    printf("module %s already present!\n", modname);
1088	    /* XXX what can we do? this is a build error. :-( */
1089	    continue;
1090	}
1091	modlist_newmodule(modname, ver, lf);
1092    }
1093}
1094
1095static void
1096linker_preload(void* arg)
1097{
1098    caddr_t		modptr;
1099    const char		*modname, *nmodname;
1100    char		*modtype;
1101    linker_file_t	lf;
1102    linker_class_t	lc;
1103    int			error;
1104    linker_file_list_t	loaded_files;
1105    linker_file_list_t	depended_files;
1106    struct mod_metadata	*mp, *nmp;
1107    struct mod_metadata **start, **stop, **mdp, **nmdp;
1108    struct mod_depend	*verinfo;
1109    int			nver;
1110    int			resolves;
1111    modlist_t		mod;
1112    struct sysinit	**si_start, **si_stop;
1113
1114    TAILQ_INIT(&loaded_files);
1115    TAILQ_INIT(&depended_files);
1116    TAILQ_INIT(&found_modules);
1117    error = 0;
1118
1119    modptr = NULL;
1120    while ((modptr = preload_search_next_name(modptr)) != NULL) {
1121	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1122	modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1123	if (modname == NULL) {
1124	    printf("Preloaded module at %p does not have a name!\n", modptr);
1125	    continue;
1126	}
1127	if (modtype == NULL) {
1128	    printf("Preloaded module at %p does not have a type!\n", modptr);
1129	    continue;
1130	}
1131	printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
1132	lf = NULL;
1133	TAILQ_FOREACH(lc, &classes, link) {
1134	    error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1135	    if (error) {
1136		lf = NULL;
1137		break;
1138	    }
1139	}
1140	if (lf)
1141	    TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1142    }
1143
1144    /*
1145     * First get a list of stuff in the kernel.
1146     */
1147    if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, &stop,
1148			       NULL) == 0)
1149	linker_addmodules(linker_kernel_file, start, stop, 1);
1150
1151    /*
1152     * this is a once-off kinky bubble sort
1153     * resolve relocation dependency requirements
1154     */
1155restart:
1156    TAILQ_FOREACH(lf, &loaded_files, loaded) {
1157	error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
1158	/*
1159	 * First, look to see if we would successfully link with this stuff.
1160	 */
1161	resolves = 1;	/* unless we know otherwise */
1162	if (!error) {
1163	    for (mdp = start; mdp < stop; mdp++) {
1164		mp = linker_reloc_ptr(lf, *mdp);
1165		if (mp->md_type != MDT_DEPEND)
1166		    continue;
1167		linker_mdt_depend(lf, mp, &modname, &verinfo);
1168		for (nmdp = start; nmdp < stop; nmdp++) {
1169		    nmp = linker_reloc_ptr(lf, *nmdp);
1170		    if (nmp->md_type != MDT_VERSION)
1171			continue;
1172		    linker_mdt_version(lf, nmp, &nmodname, NULL);
1173		    nmodname = linker_reloc_ptr(lf, nmp->md_cval);
1174		    if (strcmp(modname, nmodname) == 0)
1175			break;
1176		}
1177		if (nmdp < stop)		/* it's a self reference */
1178		    continue;
1179		if (modlist_lookup2(modname, verinfo) == NULL) {
1180		    /* ok, the module isn't here yet, we are not finished */
1181		    resolves = 0;
1182		}
1183	    }
1184	}
1185	/*
1186	 * OK, if we found our modules, we can link.  So, "provide" the
1187	 * modules inside and add it to the end of the link order list.
1188	 */
1189	if (resolves) {
1190	    if (!error) {
1191		for (mdp = start; mdp < stop; mdp++) {
1192		    mp = linker_reloc_ptr(lf, *mdp);
1193		    if (mp->md_type != MDT_VERSION)
1194			continue;
1195		    linker_mdt_version(lf, mp, &modname, &nver);
1196		    if (modlist_lookup(modname, nver) != NULL) {
1197			printf("module %s already present!\n", modname);
1198			linker_file_unload(lf);
1199			TAILQ_REMOVE(&loaded_files, lf, loaded);
1200			goto restart;	/* we changed the tailq next ptr */
1201		    }
1202		    modlist_newmodule(modname, nver, lf);
1203		}
1204	    }
1205	    TAILQ_REMOVE(&loaded_files, lf, loaded);
1206	    TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1207	    /*
1208	     * Since we provided modules, we need to restart the sort so
1209	     * that the previous files that depend on us have a chance.
1210	     * Also, we've busted the tailq next pointer with the REMOVE.
1211	     */
1212	    goto restart;
1213	}
1214    }
1215
1216    /*
1217     * At this point, we check to see what could not be resolved..
1218     */
1219    TAILQ_FOREACH(lf, &loaded_files, loaded) {
1220	printf("KLD file %s is missing dependencies\n", lf->filename);
1221	linker_file_unload(lf);
1222	TAILQ_REMOVE(&loaded_files, lf, loaded);
1223    }
1224
1225    /*
1226     * We made it. Finish off the linking in the order we determined.
1227     */
1228    TAILQ_FOREACH(lf, &depended_files, loaded) {
1229	if (linker_kernel_file) {
1230	    linker_kernel_file->refs++;
1231	    error = linker_file_add_dependancy(lf, linker_kernel_file);
1232	    if (error)
1233		panic("cannot add dependency");
1234	}
1235	lf->userrefs++;		/* so we can (try to) kldunload it */
1236	error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
1237	if (!error) {
1238	    for (mdp = start; mdp < stop; mdp++) {
1239		mp = linker_reloc_ptr(lf, *mdp);
1240		if (mp->md_type != MDT_DEPEND)
1241		    continue;
1242		linker_mdt_depend(lf, mp, &modname, &verinfo);
1243		mod = modlist_lookup2(modname, verinfo);
1244		mod->container->refs++;
1245		error = linker_file_add_dependancy(lf, mod->container);
1246		if (error)
1247		    panic("cannot add dependency");
1248	    }
1249	}
1250
1251	/*
1252	 * Now do relocation etc using the symbol search paths established by
1253	 * the dependencies
1254	 */
1255	error = LINKER_LINK_PRELOAD_FINISH(lf);
1256	if (error) {
1257	    printf("KLD file %s - could not finalize loading\n", lf->filename);
1258	    linker_file_unload(lf);
1259	    continue;
1260	}
1261
1262	linker_file_register_modules(lf);
1263	if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0)
1264	    sysinit_add(si_start, si_stop);
1265	linker_file_register_sysctls(lf);
1266	lf->flags |= LINKER_FILE_LINKED;
1267    }
1268    /* woohoo! we made it! */
1269}
1270
1271SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1272
1273/*
1274 * Search for a not-loaded module by name.
1275 *
1276 * Modules may be found in the following locations:
1277 *
1278 * - preloaded (result is just the module name)
1279 * - on disk (result is full path to module)
1280 *
1281 * If the module name is qualified in any way (contains path, etc.)
1282 * the we simply return a copy of it.
1283 *
1284 * The search path can be manipulated via sysctl.  Note that we use the ';'
1285 * character as a separator to be consistent with the bootloader.
1286 */
1287
1288static char linker_hintfile[] = "linker.hints";
1289static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules;/modules";
1290
1291SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1292	      sizeof(linker_path), "module load search path");
1293
1294TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1295
1296static char *linker_ext_list[] = {
1297	"",
1298	".ko",
1299	NULL
1300};
1301
1302/*
1303 * Check if file actually exists either with or without extension listed
1304 * in the linker_ext_list.
1305 * (probably should be generic for the rest of the kernel)
1306 */
1307static char *
1308linker_lookup_file(const char *path, int pathlen,
1309	const char *name, int namelen, struct vattr *vap)
1310{
1311    struct nameidata	nd;
1312    struct proc		*p = curproc;	/* XXX */
1313    char		*result, **cpp, *sep;
1314    int			error, len, extlen, reclen, flags;
1315    enum vtype		type;
1316
1317    extlen = 0;
1318    for (cpp = linker_ext_list; *cpp; cpp++) {
1319	len = strlen(*cpp);
1320	if (len > extlen)
1321	    extlen = len;
1322    }
1323    extlen++;	/* trailing '\0' */
1324    sep = (path[pathlen - 1] != '/') ? "/" : "";
1325
1326    reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1327    result = malloc(reclen, M_LINKER, M_WAITOK);
1328    for (cpp = linker_ext_list; *cpp; cpp++) {
1329	snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1330	    namelen, name, *cpp);
1331	/*
1332	 * Attempt to open the file, and return the path if we succeed
1333	 * and it's a regular file.
1334	 */
1335	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
1336	flags = FREAD;
1337	error = vn_open(&nd, &flags, 0);
1338	if (error == 0) {
1339	    NDFREE(&nd, NDF_ONLY_PNBUF);
1340	    type = nd.ni_vp->v_type;
1341	    if (vap)
1342		VOP_GETATTR(nd.ni_vp, vap, p->p_ucred, p);
1343	    VOP_UNLOCK(nd.ni_vp, 0, p);
1344	    vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
1345	    if (type == VREG)
1346	        return(result);
1347	}
1348    }
1349    free(result, M_LINKER);
1350    return(NULL);
1351}
1352
1353#define	INT_ALIGN(base, ptr)	ptr = \
1354	(base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1355
1356/*
1357 * Lookup KLD which contains requested module in the "linker.hints" file.
1358 * If version specification is available, then try to find the best KLD.
1359 * Otherwise just find the latest one.
1360 */
1361static char *
1362linker_hints_lookup(const char *path, int pathlen,
1363	const char *modname, int modnamelen,
1364	struct mod_depend *verinfo)
1365{
1366    struct proc *p = curproc;
1367    struct ucred *cred = p ? p->p_ucred : NULL;
1368    struct nameidata nd;
1369    struct vattr vattr, mattr;
1370    u_char *hints = NULL;
1371    u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1372    int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1373
1374    result = NULL;
1375    bestver = found = 0;
1376
1377    sep = (path[pathlen - 1] != '/') ? "/" : "";
1378    reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1379        strlen(sep) + 1;
1380    pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1381    snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, linker_hintfile);
1382
1383    NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, p);
1384    flags = FREAD;
1385    error = vn_open(&nd, &flags, 0);
1386    if (error)
1387	goto bad;
1388    NDFREE(&nd, NDF_ONLY_PNBUF);
1389    VOP_UNLOCK(nd.ni_vp, 0, p);
1390    if (nd.ni_vp->v_type != VREG)
1391	goto bad;
1392    best = cp = NULL;
1393    error = VOP_GETATTR(nd.ni_vp, &vattr, cred, p);
1394    if (error)
1395	goto bad;
1396    /*
1397     * XXX: we need to limit this number to some reasonable value
1398     */
1399    if (vattr.va_size > 100 * 1024) {
1400	printf("hints file too large %ld\n", (long)vattr.va_size);
1401	goto bad;
1402    }
1403    hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1404    if (hints == NULL)
1405	goto bad;
1406    error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1407		    UIO_SYSSPACE, IO_NODELOCKED, cred, &reclen, p);
1408    if (error)
1409	goto bad;
1410    vn_close(nd.ni_vp, FREAD, cred, p);
1411    nd.ni_vp = NULL;
1412    if (reclen != 0) {
1413	printf("can't read %d\n", reclen);
1414	goto bad;
1415    }
1416    intp = (int*)hints;
1417    ival = *intp++;
1418    if (ival != LINKER_HINTS_VERSION) {
1419	printf("hints file version mismatch %d\n", ival);
1420	goto bad;
1421    }
1422    bufend = hints + vattr.va_size;
1423    recptr = (u_char*)intp;
1424    clen = blen = 0;
1425    while (recptr < bufend && !found) {
1426	intp = (int*)recptr;
1427	reclen = *intp++;
1428	ival = *intp++;
1429	cp = (char*)intp;
1430	switch (ival) {
1431	case MDT_VERSION:
1432	    clen = *cp++;
1433	    if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1434		break;
1435	    cp += clen;
1436	    INT_ALIGN(hints, cp);
1437	    ival = *(int*)cp;
1438	    cp += sizeof(int);
1439	    clen = *cp++;
1440	    if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
1441		found = 1;
1442		break;
1443	    }
1444	    if (ival >= verinfo->md_ver_minimum &&
1445		ival <= verinfo->md_ver_maximum &&
1446		ival > bestver) {
1447		bestver = ival;
1448		best = cp;
1449		blen = clen;
1450	    }
1451	    break;
1452	default:
1453	    break;
1454	}
1455	recptr += reclen + sizeof(int);
1456    }
1457    /*
1458     * Finally check if KLD is in the place
1459     */
1460    if (found)
1461	result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1462    else if (best)
1463	result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1464    if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) {
1465	/*
1466	 * KLD is newer than hints file. What we should do now ?
1467	 */
1468	 printf("warning: KLD '%s' is newer than the linker.hints file\n",
1469	     result);
1470    }
1471bad:
1472    if (hints)
1473	free(hints, M_TEMP);
1474    if (nd.ni_vp != NULL)
1475	vn_close(nd.ni_vp, FREAD, cred, p);
1476    /*
1477     * If nothing found or hints is absent - fallback to the old way
1478     * by using "kldname[.ko]" as module name.
1479     */
1480    if (!found && !bestver && result == NULL)
1481	result = linker_lookup_file(path, pathlen, modname, modnamelen, NULL);
1482    return result;
1483}
1484
1485/*
1486 * Lookup KLD which contains requested module in the all directories.
1487 */
1488static char *
1489linker_search_module(const char *modname, int modnamelen,
1490	struct mod_depend *verinfo)
1491{
1492    char *cp, *ep, *result;
1493
1494    /*
1495     * traverse the linker path
1496     */
1497    for (cp = linker_path; *cp; cp = ep + 1) {
1498
1499	/* find the end of this component */
1500	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1501	    ;
1502	result = linker_hints_lookup(cp, ep - cp, modname, modnamelen, verinfo);
1503	if (result != NULL)
1504	    return(result);
1505	if (*ep == 0)
1506	    break;
1507    }
1508    return (NULL);
1509}
1510
1511/*
1512 * Search for module in all directories listed in the linker_path.
1513 */
1514static char *
1515linker_search_kld(const char *name)
1516{
1517    char		*cp, *ep, *result, **cpp;
1518    int			extlen, len;
1519
1520    /* qualified at all? */
1521    if (index(name, '/'))
1522	return(linker_strdup(name));
1523
1524    extlen = 0;
1525    for (cpp = linker_ext_list; *cpp; cpp++) {
1526	len = strlen(*cpp);
1527	if (len > extlen)
1528	    extlen = len;
1529    }
1530    extlen++;	/* trailing '\0' */
1531
1532    /* traverse the linker path */
1533    len = strlen(name);
1534    for (ep = linker_path; *ep; ep++) {
1535	cp = ep;
1536	/* find the end of this component */
1537	for (; *ep != 0 && *ep != ';'; ep++)
1538	    ;
1539	result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1540	if (result != NULL)
1541	    return(result);
1542    }
1543    return(NULL);
1544}
1545
1546static const char *
1547linker_basename(const char* path)
1548{
1549    const char *filename;
1550
1551    filename = rindex(path, '/');
1552    if (filename == NULL)
1553	return path;
1554    if (filename[1])
1555	filename++;
1556    return filename;
1557}
1558
1559/*
1560 * Find a file which contains given module and load it,
1561 * if "parent" is not NULL, register a reference to it.
1562 */
1563static int
1564linker_load_module(const char *kldname, const char *modname,
1565	struct linker_file *parent, struct mod_depend *verinfo,
1566	struct linker_file **lfpp)
1567{
1568    linker_file_t lfdep;
1569    const char *filename;
1570    char *pathname;
1571    int error;
1572
1573    if (modname == NULL) {
1574	/*
1575	 * We have to load KLD
1576	 */
1577	KASSERT(verinfo == NULL, ("linker_load_module: verinfo is not NULL"));
1578	pathname = linker_search_kld(kldname);
1579    } else {
1580	if (modlist_lookup2(modname, verinfo) != NULL)
1581	    return (EEXIST);
1582	if (kldname == NULL) {
1583	    /*
1584	     * Need to find a KLD with required module
1585	     */
1586	    pathname = linker_search_module(modname, strlen(modname), verinfo);
1587	} else
1588	    pathname = linker_strdup(kldname);
1589    }
1590    if (pathname == NULL)
1591	return (ENOENT);
1592
1593    /*
1594     * Can't load more than one file with the same basename
1595     * XXX: Actually it should be possible to have multiple KLDs
1596     * with the same basename but different path because they can provide
1597     * different versions of the same modules.
1598     */
1599    filename = linker_basename(pathname);
1600    if (linker_find_file_by_name(filename)) {
1601	error = EEXIST;
1602	goto out;
1603    }
1604
1605    do {
1606	error = linker_load_file(pathname, &lfdep);
1607	if (error)
1608	    break;
1609	if (modname && verinfo && modlist_lookup2(modname, verinfo) == NULL) {
1610	    linker_file_unload(lfdep);
1611	    error = ENOENT;
1612	    break;
1613	}
1614	if (parent) {
1615	    error = linker_file_add_dependancy(parent, lfdep);
1616	    if (error)
1617		break;
1618	}
1619	if (lfpp)
1620	    *lfpp = lfdep;
1621    } while(0);
1622out:
1623    if (pathname)
1624	free(pathname, M_LINKER);
1625    return error;
1626}
1627
1628/*
1629 * This routine is responsible for finding dependencies of userland
1630 * initiated kldload(2)'s of files.
1631 */
1632int
1633linker_load_dependancies(linker_file_t lf)
1634{
1635    linker_file_t lfdep;
1636    struct mod_metadata **start, **stop, **mdp, **nmdp;
1637    struct mod_metadata *mp, *nmp;
1638    struct mod_depend *verinfo;
1639    modlist_t mod;
1640    const char *modname, *nmodname;
1641    int ver, error = 0, count;
1642
1643    /*
1644     * All files are dependant on /kernel.
1645     */
1646    if (linker_kernel_file) {
1647	linker_kernel_file->refs++;
1648	error = linker_file_add_dependancy(lf, linker_kernel_file);
1649	if (error)
1650	    return error;
1651    }
1652
1653    if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, &count) != 0)
1654	return 0;
1655    for (mdp = start; mdp < stop; mdp++) {
1656	mp = linker_reloc_ptr(lf, *mdp);
1657	if (mp->md_type != MDT_VERSION)
1658	    continue;
1659	linker_mdt_version(lf, mp, &modname, &ver);
1660	mod = modlist_lookup(modname, ver);
1661	if (mod != NULL) {
1662	    printf("interface %s.%d already present in the KLD '%s'!\n",
1663		modname, ver, mod->container->filename);
1664	    return EEXIST;
1665	}
1666    }
1667
1668    for (mdp = start; mdp < stop; mdp++) {
1669	mp = linker_reloc_ptr(lf, *mdp);
1670	if (mp->md_type != MDT_DEPEND)
1671	    continue;
1672	linker_mdt_depend(lf, mp, &modname, &verinfo);
1673	nmodname = NULL;
1674	for (nmdp = start; nmdp < stop; nmdp++) {
1675	    nmp = linker_reloc_ptr(lf, *nmdp);
1676	    if (nmp->md_type != MDT_VERSION)
1677		continue;
1678	    nmodname = linker_reloc_ptr(lf, nmp->md_cval);
1679	    if (strcmp(modname, nmodname) == 0)
1680		break;
1681	}
1682	if (nmdp < stop)	/* early exit, it's a self reference */
1683	    continue;
1684	mod = modlist_lookup2(modname, verinfo);
1685	if (mod) {		/* woohoo, it's loaded already */
1686	    lfdep = mod->container;
1687	    lfdep->refs++;
1688	    error = linker_file_add_dependancy(lf, lfdep);
1689	    if (error)
1690		break;
1691	    continue;
1692	}
1693	error = linker_load_module(NULL, modname, lf, verinfo, NULL);
1694	if (error) {
1695	    printf("KLD %s: depends on %s - not available\n",
1696		lf->filename, modname);
1697	    break;
1698	}
1699    }
1700
1701    if (error)
1702	return error;
1703    linker_addmodules(lf, start, stop, 0);
1704    return error;
1705}
1706