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