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