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