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