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