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