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