xref: /dragonfly/sys/kern/kern_linker.c (revision 9c600e7d)
1 /*-
2  * Copyright (c) 1997 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/kern/kern_linker.c,v 1.41.2.3 2001/11/21 17:50:35 luigi Exp $
27  * $DragonFly: src/sys/kern/kern_linker.c,v 1.8 2003/07/19 21:14:38 dillon Exp $
28  */
29 
30 #include "opt_ddb.h"
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/sysproto.h>
37 #include <sys/sysent.h>
38 #include <sys/proc.h>
39 #include <sys/lock.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 #include <vm/vm_zone.h>
49 
50 #ifdef KLD_DEBUG
51 int kld_debug = 0;
52 #endif
53 
54 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
55 linker_file_t linker_current_file;
56 linker_file_t linker_kernel_file;
57 
58 static struct lock lock;	/* lock for the file list */
59 static linker_class_list_t classes;
60 static linker_file_list_t linker_files;
61 static int next_file_id = 1;
62 
63 static void
64 linker_init(void* arg)
65 {
66     lockinit(&lock, 0, "klink", 0, 0);
67     TAILQ_INIT(&classes);
68     TAILQ_INIT(&linker_files);
69 }
70 
71 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
72 
73 int
74 linker_add_class(const char* desc, void* priv,
75 		 struct linker_class_ops* ops)
76 {
77     linker_class_t lc;
78 
79     lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT);
80     if (!lc)
81 	return ENOMEM;
82     bzero(lc, sizeof(*lc));
83 
84     lc->desc = desc;
85     lc->priv = priv;
86     lc->ops = ops;
87     TAILQ_INSERT_HEAD(&classes, lc, link);
88 
89     return 0;
90 }
91 
92 static int
93 linker_file_sysinit(linker_file_t lf)
94 {
95     struct linker_set* sysinits;
96     struct sysinit** sipp;
97     struct sysinit** xipp;
98     struct sysinit* save;
99     const moduledata_t *moddata;
100     int error;
101 
102     KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
103 		   lf->filename));
104 
105     if (linker_file_lookup_symbol(lf, "sysinit_set", 0, (caddr_t *)&sysinits)) {
106 	KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs not found\n"));
107 	return 0; /* XXX is this correct ? No sysinit ? */
108     }
109     KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits));
110     if (sysinits == NULL)
111 	return 0; /* XXX is this correct ? No sysinit ? */
112 
113     /* HACK ALERT! */
114     for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
115 	if ((*sipp)->func == module_register_init) {
116 	    moddata = (*sipp)->udata;
117 	    error = module_register(moddata, lf);
118 	    if (error) {
119 		printf("linker_file_sysinit \"%s\" failed to register! %d\n",
120 		    lf->filename, error);
121 		return error;
122 	    }
123 	}
124     }
125 
126     /*
127      * Perform a bubble sort of the system initialization objects by
128      * their subsystem (primary key) and order (secondary key).
129      *
130      * Since some things care about execution order, this is the
131      * operation which ensures continued function.
132      */
133     for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
134 	for (xipp = sipp + 1; *xipp; xipp++) {
135 	    if ((*sipp)->subsystem < (*xipp)->subsystem ||
136 		 ((*sipp)->subsystem == (*xipp)->subsystem &&
137 		  (*sipp)->order <= (*xipp)->order))
138 		continue;	/* skip*/
139 	    save = *sipp;
140 	    *sipp = *xipp;
141 	    *xipp = save;
142 	}
143     }
144 
145 
146     /*
147      * Traverse the (now) ordered list of system initialization tasks.
148      * Perform each task, and continue on to the next task.
149      */
150     for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
151 	if ((*sipp)->subsystem == SI_SUB_DUMMY)
152 	    continue;	/* skip dummy task(s)*/
153 
154 	/* Call function */
155 	(*((*sipp)->func))((*sipp)->udata);
156     }
157     return 0; /* no errors */
158 }
159 
160 static void
161 linker_file_sysuninit(linker_file_t lf)
162 {
163     struct linker_set* sysuninits;
164     struct sysinit** sipp;
165     struct sysinit** xipp;
166     struct sysinit* save;
167 
168     KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
169 		   lf->filename));
170 
171     if (linker_file_lookup_symbol(lf, "sysuninit_set", 0, (caddr_t *)&sysuninits)) {
172 	KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs not found\n"));
173 	return;
174     }
175     KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits));
176     if (sysuninits == NULL)
177 	return;
178 
179     /*
180      * Perform a reverse bubble sort of the system initialization objects
181      * by their subsystem (primary key) and order (secondary key).
182      *
183      * Since some things care about execution order, this is the
184      * operation which ensures continued function.
185      */
186     for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
187 	for (xipp = sipp + 1; *xipp; xipp++) {
188 	    if ((*sipp)->subsystem > (*xipp)->subsystem ||
189 		 ((*sipp)->subsystem == (*xipp)->subsystem &&
190 		  (*sipp)->order >= (*xipp)->order))
191 		continue;	/* skip*/
192 	    save = *sipp;
193 	    *sipp = *xipp;
194 	    *xipp = save;
195 	}
196     }
197 
198 
199     /*
200      * Traverse the (now) ordered list of system initialization tasks.
201      * Perform each task, and continue on to the next task.
202      */
203     for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
204 	if ((*sipp)->subsystem == SI_SUB_DUMMY)
205 	    continue;	/* skip dummy task(s)*/
206 
207 	/* Call function */
208 	(*((*sipp)->func))((*sipp)->udata);
209     }
210 }
211 
212 static void
213 linker_file_register_sysctls(linker_file_t lf)
214 {
215     struct linker_set* sysctls;
216 
217     KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
218 		   lf->filename));
219 
220     if (linker_file_lookup_symbol(lf, "sysctl_set", 0, (caddr_t *)&sysctls)) {
221 	KLD_DPF(FILE, ("linker_file_register_sysctls: SYSCTLs not found\n"));
222 	return;
223     }
224     KLD_DPF(FILE, ("linker_file_register_sysctls: SYSCTLs %p\n", sysctls));
225     if (sysctls == NULL)
226 	return;
227 
228     sysctl_register_set(sysctls);
229 }
230 
231 static void
232 linker_file_unregister_sysctls(linker_file_t lf)
233 {
234     struct linker_set* sysctls;
235 
236     KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
237 		   lf->filename));
238 
239     if (linker_file_lookup_symbol(lf, "sysctl_set", 0, (caddr_t *)&sysctls)) {
240 	KLD_DPF(FILE, ("linker_file_unregister_sysctls: SYSCTLs not found\n"));
241 	return;
242     }
243     KLD_DPF(FILE, ("linker_file_unregister_sysctls: SYSCTLs %p\n", sysctls));
244     if (sysctls == NULL)
245 	return;
246 
247     sysctl_unregister_set(sysctls);
248 }
249 
250 int
251 linker_load_file(const char* filename, linker_file_t* result)
252 {
253     linker_class_t lc;
254     linker_file_t lf;
255     int foundfile, error = 0;
256     char *koname = NULL;
257 
258     /* Refuse to load modules if securelevel raised */
259     if (securelevel > 0)
260 	return EPERM;
261 
262     lf = linker_find_file_by_name(filename);
263     if (lf) {
264 	KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
265 	*result = lf;
266 	lf->refs++;
267 	goto out;
268     }
269 
270     koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
271     if (koname == NULL) {
272 	error = ENOMEM;
273 	goto out;
274     }
275     sprintf(koname, "%s.ko", filename);
276     lf = NULL;
277     foundfile = 0;
278     for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
279 	KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
280 		       filename, lc->desc));
281 
282 	error = lc->ops->load_file(koname, &lf);	/* First with .ko */
283 	if (lf == NULL && error == ENOENT)
284 	    error = lc->ops->load_file(filename, &lf);	/* Then try without */
285 	/*
286 	 * If we got something other than ENOENT, then it exists but we cannot
287 	 * load it for some other reason.
288 	 */
289 	if (error != ENOENT)
290 	    foundfile = 1;
291 	if (lf) {
292 	    linker_file_register_sysctls(lf);
293 	    error = linker_file_sysinit(lf);
294 
295 	    *result = lf;
296 	    goto out;
297 	}
298     }
299     /*
300      * Less than ideal, but tells the user whether it failed to load or
301      * the module was not found.
302      */
303     if (foundfile)
304 	error = ENOEXEC;	/* Format not recognised (or unloadable) */
305     else
306 	error = ENOENT;		/* Nothing found */
307 
308 out:
309     if (koname)
310 	free(koname, M_LINKER);
311     return error;
312 }
313 
314 linker_file_t
315 linker_find_file_by_name(const char* filename)
316 {
317     linker_file_t lf = 0;
318     char *koname;
319 
320     koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
321     if (koname == NULL)
322 	goto out;
323     sprintf(koname, "%s.ko", filename);
324 
325     lockmgr(&lock, LK_SHARED, 0, curthread);
326     for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
327 	if (!strcmp(lf->filename, koname))
328 	    break;
329 	if (!strcmp(lf->filename, filename))
330 	    break;
331     }
332     lockmgr(&lock, LK_RELEASE, 0, curthread);
333 
334 out:
335     if (koname)
336 	free(koname, M_LINKER);
337     return lf;
338 }
339 
340 linker_file_t
341 linker_find_file_by_id(int fileid)
342 {
343     linker_file_t lf = 0;
344 
345     lockmgr(&lock, LK_SHARED, 0, curthread);
346     for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link))
347 	if (lf->id == fileid)
348 	    break;
349     lockmgr(&lock, LK_RELEASE, 0, curthread);
350 
351     return lf;
352 }
353 
354 linker_file_t
355 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
356 {
357     linker_file_t lf = 0;
358     int namelen;
359     const char *filename;
360 
361     filename = rindex(pathname, '/');
362     if (filename && filename[1])
363 	filename++;
364     else
365 	filename = pathname;
366 
367     KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
368     lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
369     namelen = strlen(filename) + 1;
370     lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
371     if (!lf)
372 	goto out;
373     bzero(lf, sizeof(*lf));
374 
375     lf->refs = 1;
376     lf->userrefs = 0;
377     lf->flags = 0;
378     lf->filename = (char*) (lf + 1);
379     strcpy(lf->filename, filename);
380     lf->id = next_file_id++;
381     lf->ndeps = 0;
382     lf->deps = NULL;
383     STAILQ_INIT(&lf->common);
384     TAILQ_INIT(&lf->modules);
385 
386     lf->priv = priv;
387     lf->ops = ops;
388     TAILQ_INSERT_TAIL(&linker_files, lf, link);
389 
390 out:
391     lockmgr(&lock, LK_RELEASE, 0, curthread);
392     return lf;
393 }
394 
395 int
396 linker_file_unload(linker_file_t file)
397 {
398     module_t mod, next;
399     struct common_symbol* cp;
400     int error = 0;
401     int i;
402 
403     /* Refuse to unload modules if securelevel raised */
404     if (securelevel > 0)
405 	return EPERM;
406 
407     KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
408     lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
409     if (file->refs == 1) {
410 	KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
411 	/*
412 	 * Inform any modules associated with this file.
413 	 */
414 	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
415 	    next = module_getfnext(mod);
416 
417 	    /*
418 	     * Give the module a chance to veto the unload.
419 	     */
420 	    if ((error = module_unload(mod)) != 0) {
421 		KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
422 			       mod));
423 		lockmgr(&lock, LK_RELEASE, 0, curthread);
424 		goto out;
425 	    }
426 
427 	    module_release(mod);
428 	}
429     }
430 
431     file->refs--;
432     if (file->refs > 0) {
433 	lockmgr(&lock, LK_RELEASE, 0, curthread);
434 	goto out;
435     }
436 
437     /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
438     if (file->flags & LINKER_FILE_LINKED) {
439 	linker_file_sysuninit(file);
440 	linker_file_unregister_sysctls(file);
441     }
442 
443     TAILQ_REMOVE(&linker_files, file, link);
444     lockmgr(&lock, LK_RELEASE, 0, curthread);
445 
446     for (i = 0; i < file->ndeps; i++)
447 	linker_file_unload(file->deps[i]);
448     free(file->deps, M_LINKER);
449 
450     for (cp = STAILQ_FIRST(&file->common); cp;
451 	 cp = STAILQ_FIRST(&file->common)) {
452 	STAILQ_REMOVE(&file->common, cp, common_symbol, link);
453 	free(cp, M_LINKER);
454     }
455 
456     file->ops->unload(file);
457     free(file, M_LINKER);
458 
459 out:
460     return error;
461 }
462 
463 int
464 linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
465 {
466     linker_file_t* newdeps;
467 
468     newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
469 		     M_LINKER, M_WAITOK);
470     if (newdeps == NULL)
471 	return ENOMEM;
472     bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
473 
474     if (file->deps) {
475 	bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
476 	free(file->deps, M_LINKER);
477     }
478     file->deps = newdeps;
479     file->deps[file->ndeps] = dep;
480     file->ndeps++;
481 
482     return 0;
483 }
484 
485 int
486 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
487 {
488     c_linker_sym_t sym;
489     linker_symval_t symval;
490     linker_file_t lf;
491     size_t common_size = 0;
492     int i;
493 
494     KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
495 		  file, name, deps));
496 
497     if (file->ops->lookup_symbol(file, name, &sym) == 0) {
498 	file->ops->symbol_values(file, sym, &symval);
499 
500 	/*
501 	 * XXX Assume a common symbol if its value is 0 and it has a non-zero
502 	 * size, otherwise it could be an absolute symbol with a value of 0.
503 	 */
504 	if (symval.value == 0 && symval.size != 0) {
505 	    /*
506 	     * For commons, first look them up in the dependancies and
507 	     * only allocate space if not found there.
508 	     */
509 	    common_size = symval.size;
510 	} else {
511 	    KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
512 	    *raddr = symval.value;
513 	    return 0;
514 	}
515     }
516     if (deps) {
517 	for (i = 0; i < file->ndeps; i++) {
518 	    if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
519 		KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", *raddr));
520 		return 0;
521 	    }
522 	}
523 
524 	/* If we have not found it in the dependencies, search globally */
525 	for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
526 	    /* But skip the current file if it's on the list */
527 	    if (lf == file)
528 		continue;
529 	    /* And skip the files we searched above */
530 	    for (i = 0; i < file->ndeps; i++)
531 		if (lf == file->deps[i])
532 		    break;
533 	    if (i < file->ndeps)
534 		continue;
535 	    if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
536 		KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", *raddr));
537 		return 0;
538 	    }
539 	}
540     }
541 
542     if (common_size > 0) {
543 	/*
544 	 * This is a common symbol which was not found in the
545 	 * dependancies.  We maintain a simple common symbol table in
546 	 * the file object.
547 	 */
548 	struct common_symbol* cp;
549 
550 	for (cp = STAILQ_FIRST(&file->common); cp;
551 	     cp = STAILQ_NEXT(cp, link))
552 	    if (!strcmp(cp->name, name)) {
553 		KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
554 		*raddr = cp->address;
555 		return 0;
556 	    }
557 
558 	/*
559 	 * Round the symbol size up to align.
560 	 */
561 	common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
562 	cp = malloc(sizeof(struct common_symbol)
563 		    + common_size
564 		    + strlen(name) + 1,
565 		    M_LINKER, M_WAITOK);
566 	if (!cp) {
567 	    KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
568 	    return ENOMEM;
569 	}
570 	bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
571 
572 	cp->address = (caddr_t) (cp + 1);
573 	cp->name = cp->address + common_size;
574 	strcpy(cp->name, name);
575 	bzero(cp->address, common_size);
576 	STAILQ_INSERT_TAIL(&file->common, cp, link);
577 
578 	KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
579 	*raddr = cp->address;
580 	return 0;
581     }
582 
583     KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
584     return ENOENT;
585 }
586 
587 #ifdef DDB
588 /*
589  * DDB Helpers.  DDB has to look across multiple files with their own
590  * symbol tables and string tables.
591  *
592  * Note that we do not obey list locking protocols here.  We really don't
593  * need DDB to hang because somebody's got the lock held.  We'll take the
594  * chance that the files list is inconsistant instead.
595  */
596 
597 int
598 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
599 {
600     linker_file_t lf;
601 
602     for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
603 	if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
604 	    return 0;
605     }
606     return ENOENT;
607 }
608 
609 int
610 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
611 {
612     linker_file_t lf;
613     u_long off = (uintptr_t)value;
614     u_long diff, bestdiff;
615     c_linker_sym_t best;
616     c_linker_sym_t es;
617 
618     best = 0;
619     bestdiff = off;
620     for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
621 	if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
622 	    continue;
623 	if (es != 0 && diff < bestdiff) {
624 	    best = es;
625 	    bestdiff = diff;
626 	}
627 	if (bestdiff == 0)
628 	    break;
629     }
630     if (best) {
631 	*sym = best;
632 	*diffp = bestdiff;
633 	return 0;
634     } else {
635 	*sym = 0;
636 	*diffp = off;
637 	return ENOENT;
638     }
639 }
640 
641 int
642 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
643 {
644     linker_file_t lf;
645 
646     for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
647 	if (lf->ops->symbol_values(lf, sym, symval) == 0)
648 	    return 0;
649     }
650     return ENOENT;
651 }
652 
653 #endif
654 
655 /*
656  * Syscalls.
657  */
658 
659 int
660 kldload(struct kldload_args *uap)
661 {
662     struct thread *td = curthread;
663     struct proc *p = td->td_proc;
664     char* filename = NULL, *modulename;
665     linker_file_t lf;
666     int error = 0;
667 
668     p->p_retval[0] = -1;
669 
670     if (securelevel > 0)	/* redundant, but that's OK */
671 	return EPERM;
672 
673     if ((error = suser(td)) != 0)
674 	return error;
675 
676     filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
677     if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
678 	goto out;
679 
680     /* Can't load more than one module with the same name */
681     modulename = rindex(filename, '/');
682     if (modulename == NULL)
683 	modulename = filename;
684     else
685 	modulename++;
686     if (linker_find_file_by_name(modulename)) {
687 	error = EEXIST;
688 	goto out;
689     }
690 
691     if ((error = linker_load_file(filename, &lf)) != 0)
692 	goto out;
693 
694     lf->userrefs++;
695     p->p_retval[0] = lf->id;
696 
697 out:
698     if (filename)
699 	free(filename, M_TEMP);
700     return error;
701 }
702 
703 int
704 kldunload(struct kldunload_args *uap)
705 {
706     struct thread *td = curthread;
707     linker_file_t lf;
708     int error = 0;
709 
710     if (securelevel > 0)	/* redundant, but that's OK */
711 	return EPERM;
712 
713     if ((error = suser(td)) != 0)
714 	return error;
715 
716     lf = linker_find_file_by_id(SCARG(uap, fileid));
717     if (lf) {
718 	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
719 	if (lf->userrefs == 0) {
720 	    printf("linkerunload: attempt to unload file that was loaded by the kernel\n");
721 	    error = EBUSY;
722 	    goto out;
723 	}
724 	lf->userrefs--;
725 	error = linker_file_unload(lf);
726 	if (error)
727 	    lf->userrefs++;
728     } else
729 	error = ENOENT;
730 
731 out:
732     return error;
733 }
734 
735 int
736 kldfind(struct kldfind_args *uap)
737 {
738     struct proc *p = curproc;
739     char *filename = NULL, *modulename;
740     linker_file_t lf;
741     int error = 0;
742 
743     p->p_retval[0] = -1;
744 
745     filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
746     if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
747 	goto out;
748 
749     modulename = rindex(filename, '/');
750     if (modulename == NULL)
751 	modulename = filename;
752 
753     lf = linker_find_file_by_name(modulename);
754     if (lf)
755 	p->p_retval[0] = lf->id;
756     else
757 	error = ENOENT;
758 
759 out:
760     if (filename)
761 	free(filename, M_TEMP);
762     return error;
763 }
764 
765 int
766 kldnext(struct kldnext_args *uap)
767 {
768     struct proc *p = curproc;
769     linker_file_t lf;
770     int error = 0;
771 
772     if (SCARG(uap, fileid) == 0) {
773 	if (TAILQ_FIRST(&linker_files))
774 	    p->p_retval[0] = TAILQ_FIRST(&linker_files)->id;
775 	else
776 	    p->p_retval[0] = 0;
777 	return 0;
778     }
779 
780     lf = linker_find_file_by_id(SCARG(uap, fileid));
781     if (lf) {
782 	if (TAILQ_NEXT(lf, link))
783 	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
784 	else
785 	    p->p_retval[0] = 0;
786     } else
787 	error = ENOENT;
788 
789     return error;
790 }
791 
792 int
793 kldstat(struct kldstat_args *uap)
794 {
795     struct proc *p = curproc;
796     linker_file_t lf;
797     int error = 0;
798     int version;
799     struct kld_file_stat* stat;
800     int namelen;
801 
802     lf = linker_find_file_by_id(SCARG(uap, fileid));
803     if (!lf) {
804 	error = ENOENT;
805 	goto out;
806     }
807 
808     stat = SCARG(uap, stat);
809 
810     /*
811      * Check the version of the user's structure.
812      */
813     if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
814 	goto out;
815     if (version != sizeof(struct kld_file_stat)) {
816 	error = EINVAL;
817 	goto out;
818     }
819 
820     namelen = strlen(lf->filename) + 1;
821     if (namelen > MAXPATHLEN)
822 	namelen = MAXPATHLEN;
823     if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
824 	goto out;
825     if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
826 	goto out;
827     if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
828 	goto out;
829     if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
830 	goto out;
831     if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
832 	goto out;
833 
834     p->p_retval[0] = 0;
835 
836 out:
837     return error;
838 }
839 
840 int
841 kldfirstmod(struct kldfirstmod_args *uap)
842 {
843     struct proc *p = curproc;
844     linker_file_t lf;
845     int error = 0;
846 
847     lf = linker_find_file_by_id(SCARG(uap, fileid));
848     if (lf) {
849 	if (TAILQ_FIRST(&lf->modules))
850 	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
851 	else
852 	    p->p_retval[0] = 0;
853     } else
854 	error = ENOENT;
855 
856     return error;
857 }
858 
859 int
860 kldsym(struct kldsym_args *uap)
861 {
862     char *symstr = NULL;
863     c_linker_sym_t sym;
864     linker_symval_t symval;
865     linker_file_t lf;
866     struct kld_sym_lookup lookup;
867     int error = 0;
868 
869     if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
870 	goto out;
871     if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
872 	error = EINVAL;
873 	goto out;
874     }
875 
876     symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
877     if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
878 	goto out;
879 
880     if (SCARG(uap, fileid) != 0) {
881 	lf = linker_find_file_by_id(SCARG(uap, fileid));
882 	if (lf == NULL) {
883 	    error = ENOENT;
884 	    goto out;
885 	}
886 	if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
887 	    lf->ops->symbol_values(lf, sym, &symval) == 0) {
888 	    lookup.symvalue = (uintptr_t)symval.value;
889 	    lookup.symsize = symval.size;
890 	    error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
891 	} else
892 	    error = ENOENT;
893     } else {
894 	for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
895 	    if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
896 		lf->ops->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 		break;
901 	    }
902 	}
903 	if (!lf)
904 	    error = ENOENT;
905     }
906 out:
907     if (symstr)
908 	free(symstr, M_TEMP);
909     return error;
910 }
911 
912 /*
913  * Preloaded module support
914  */
915 
916 static void
917 linker_preload(void* arg)
918 {
919     caddr_t		modptr;
920     char		*modname;
921     char		*modtype;
922     linker_file_t	lf;
923     linker_class_t	lc;
924     int			error;
925     struct linker_set	*sysinits;
926     struct sysinit	**sipp;
927     const moduledata_t	*moddata;
928 
929     modptr = NULL;
930     while ((modptr = preload_search_next_name(modptr)) != NULL) {
931 	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
932 	modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
933 	if (modname == NULL) {
934 	    printf("Preloaded module at %p does not have a name!\n", modptr);
935 	    continue;
936 	}
937 	if (modtype == NULL) {
938 	    printf("Preloaded module at %p does not have a type!\n", modptr);
939 	    continue;
940 	}
941 	printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
942 	lf = linker_find_file_by_name(modname);
943 	if (lf) {
944 	    lf->userrefs++;
945 	    continue;
946 	}
947 	lf = NULL;
948 	for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
949 	    error = lc->ops->load_file(modname, &lf);
950 	    if (error) {
951 		lf = NULL;
952 		break;
953 	    }
954 	}
955 	if (lf) {
956 	    lf->userrefs++;
957 
958 	    if (linker_file_lookup_symbol(lf, "sysinit_set", 0, (caddr_t *)&sysinits) == 0 && sysinits) {
959 		/* HACK ALERT!
960 		 * This is to set the sysinit moduledata so that the module
961 		 * can attach itself to the correct containing file.
962 		 * The sysinit could be run at *any* time.
963 		 */
964 		for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
965 		    if ((*sipp)->func == module_register_init) {
966 			moddata = (*sipp)->udata;
967 			error = module_register(moddata, lf);
968 			if (error)
969 			    printf("Preloaded %s \"%s\" failed to register: %d\n",
970 				modtype, modname, error);
971 		    }
972 		}
973 		sysinit_add((struct sysinit **)sysinits->ls_items);
974 	    }
975 	    linker_file_register_sysctls(lf);
976 	}
977     }
978 }
979 
980 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
981 
982 /*
983  * Search for a not-loaded module by name.
984  *
985  * Modules may be found in the following locations:
986  *
987  * - preloaded (result is just the module name)
988  * - on disk (result is full path to module)
989  *
990  * If the module name is qualified in any way (contains path, etc.)
991  * the we simply return a copy of it.
992  *
993  * The search path can be manipulated via sysctl.  Note that we use the ';'
994  * character as a separator to be consistent with the bootloader.
995  */
996 
997 static char linker_path[MAXPATHLEN] = "/;/boot/;/modules/";
998 
999 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1000 	      sizeof(linker_path), "module load search path");
1001 
1002 static char *
1003 linker_strdup(const char *str)
1004 {
1005     char	*result;
1006 
1007     if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
1008 	strcpy(result, str);
1009     return(result);
1010 }
1011 
1012 char *
1013 linker_search_path(const char *name)
1014 {
1015     struct nameidata	nd;
1016     struct thread	*td = curthread;
1017     char		*cp, *ep, *result;
1018     int			error;
1019     enum vtype		type;
1020 
1021     /* qualified at all? */
1022     if (index(name, '/'))
1023 	return(linker_strdup(name));
1024 
1025     /* traverse the linker path */
1026     cp = linker_path;
1027     for (;;) {
1028 
1029 	/* find the end of this component */
1030 	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1031 	    ;
1032 	result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
1033 	if (result == NULL)	/* actually ENOMEM */
1034 	    return(NULL);
1035 
1036 	strncpy(result, cp, ep - cp);
1037 	strcpy(result + (ep - cp), name);
1038 
1039 	/*
1040 	 * Attempt to open the file, and return the path if we succeed and it's
1041 	 * a regular file.
1042 	 */
1043 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1044 	error = vn_open(&nd, FREAD, 0);
1045 	if (error == 0) {
1046 	    NDFREE(&nd, NDF_ONLY_PNBUF);
1047 	    type = nd.ni_vp->v_type;
1048 	    VOP_UNLOCK(nd.ni_vp, 0, td);
1049 	    vn_close(nd.ni_vp, FREAD, td);
1050 	    if (type == VREG)
1051 		return(result);
1052 	}
1053 	free(result, M_LINKER);
1054 
1055 	if (*ep == 0)
1056 	    break;
1057 	cp = ep + 1;
1058     }
1059     return(NULL);
1060 }
1061