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