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