kernel: Add linker_file_foreach() (taken from FreeBSD).
[dragonfly.git] / sys / kern / kern_linker.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1997 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/kern/kern_linker.c,v 1.41.2.3 2001/11/21 17:50:35 luigi Exp $
f4e821f3 27 * $DragonFly: src/sys/kern/kern_linker.c,v 1.44 2008/09/01 19:39:44 dillon Exp $
984263bc
MD
28 */
29
30#include "opt_ddb.h"
31
32#include <sys/param.h>
33#include <sys/kernel.h>
34#include <sys/systm.h>
35#include <sys/malloc.h>
36#include <sys/sysproto.h>
37#include <sys/sysent.h>
38#include <sys/proc.h>
895c1f85 39#include <sys/priv.h>
984263bc
MD
40#include <sys/lock.h>
41#include <sys/module.h>
89e1aaa0 42#include <sys/queue.h>
984263bc
MD
43#include <sys/linker.h>
44#include <sys/fcntl.h>
45#include <sys/libkern.h>
fad57d0e 46#include <sys/nlookup.h>
984263bc
MD
47#include <sys/vnode.h>
48#include <sys/sysctl.h>
49
50#include <vm/vm_zone.h>
51
684a93c4
MD
52#include <sys/mplock2.h>
53
6ef6faeb
SS
54#ifdef _KERNEL_VIRTUAL
55#include <dlfcn.h>
56#endif
57
984263bc 58#ifdef KLD_DEBUG
1c0e3286 59int kld_debug = 1;
984263bc
MD
60#endif
61
6456e0ad
MD
62/* Metadata from the static kernel */
63SET_DECLARE(modmetadata_set, struct mod_metadata);
984263bc 64MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
6456e0ad 65
984263bc
MD
66linker_file_t linker_current_file;
67linker_file_t linker_kernel_file;
68
69static struct lock lock; /* lock for the file list */
70static linker_class_list_t classes;
71static linker_file_list_t linker_files;
72static int next_file_id = 1;
73
1c0e3286
SS
74/* XXX wrong name; we're looking at version provision tags here, not modules */
75typedef TAILQ_HEAD(, modlist) modlisthead_t;
76struct modlist {
77 TAILQ_ENTRY(modlist) link; /* chain together all modules */
78 linker_file_t container;
79 const char *name;
80 int version;
81};
82typedef struct modlist *modlist_t;
83static modlisthead_t found_modules;
84
85
86static int linker_load_module(const char *kldname, const char *modname,
87 struct linker_file *parent, struct mod_depend *verinfo,
88 struct linker_file **lfpp);
89
90static char *
91linker_strdup(const char *str)
92{
93 char *result;
94
95 result = kmalloc(strlen(str) + 1, M_LINKER, M_WAITOK);
96 strcpy(result, str);
97 return(result);
98}
99
984263bc
MD
100static void
101linker_init(void* arg)
102{
f2770c70 103 lockinit(&lock, "klink", 0, 0);
984263bc
MD
104 TAILQ_INIT(&classes);
105 TAILQ_INIT(&linker_files);
106}
107
ba39e2e0 108SYSINIT(linker, SI_BOOT2_KLD, SI_ORDER_FIRST, linker_init, 0);
984263bc
MD
109
110int
111linker_add_class(const char* desc, void* priv,
112 struct linker_class_ops* ops)
113{
114 linker_class_t lc;
115
e7b4468c 116 lc = kmalloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT | M_ZERO);
984263bc
MD
117 if (!lc)
118 return ENOMEM;
984263bc
MD
119
120 lc->desc = desc;
121 lc->priv = priv;
122 lc->ops = ops;
123 TAILQ_INSERT_HEAD(&classes, lc, link);
124
125 return 0;
126}
127
1c0e3286 128static void
984263bc
MD
129linker_file_sysinit(linker_file_t lf)
130{
dc62b251 131 struct sysinit** start, ** stop;
984263bc
MD
132 struct sysinit** sipp;
133 struct sysinit** xipp;
134 struct sysinit* save;
984263bc
MD
135
136 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
137 lf->filename));
138
dc62b251 139 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
1c0e3286 140 return;
984263bc 141
984263bc
MD
142 /*
143 * Perform a bubble sort of the system initialization objects by
144 * their subsystem (primary key) and order (secondary key).
145 *
146 * Since some things care about execution order, this is the
147 * operation which ensures continued function.
148 */
dc62b251
MD
149 for (sipp = start; sipp < stop; sipp++) {
150 for (xipp = sipp + 1; xipp < stop; xipp++) {
984263bc
MD
151 if ((*sipp)->subsystem < (*xipp)->subsystem ||
152 ((*sipp)->subsystem == (*xipp)->subsystem &&
153 (*sipp)->order <= (*xipp)->order))
154 continue; /* skip*/
155 save = *sipp;
156 *sipp = *xipp;
157 *xipp = save;
158 }
159 }
160
161
162 /*
163 * Traverse the (now) ordered list of system initialization tasks.
164 * Perform each task, and continue on to the next task.
165 */
dc62b251 166 for (sipp = start; sipp < stop; sipp++) {
ba39e2e0 167 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
984263bc
MD
168 continue; /* skip dummy task(s)*/
169
170 /* Call function */
171 (*((*sipp)->func))((*sipp)->udata);
172 }
984263bc
MD
173}
174
175static void
176linker_file_sysuninit(linker_file_t lf)
177{
dc62b251 178 struct sysinit** start, ** stop;
984263bc
MD
179 struct sysinit** sipp;
180 struct sysinit** xipp;
181 struct sysinit* save;
182
183 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
184 lf->filename));
185
dc62b251 186 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
984263bc
MD
187 return;
188
189 /*
190 * Perform a reverse bubble sort of the system initialization objects
191 * by their subsystem (primary key) and order (secondary key).
192 *
193 * Since some things care about execution order, this is the
194 * operation which ensures continued function.
195 */
dc62b251
MD
196 for (sipp = start; sipp < stop; sipp++) {
197 for (xipp = sipp + 1; xipp < stop; xipp++) {
984263bc
MD
198 if ((*sipp)->subsystem > (*xipp)->subsystem ||
199 ((*sipp)->subsystem == (*xipp)->subsystem &&
200 (*sipp)->order >= (*xipp)->order))
201 continue; /* skip*/
202 save = *sipp;
203 *sipp = *xipp;
204 *xipp = save;
205 }
206 }
207
208
209 /*
210 * Traverse the (now) ordered list of system initialization tasks.
211 * Perform each task, and continue on to the next task.
212 */
dc62b251 213 for (sipp = start; sipp < stop; sipp++) {
ba39e2e0 214 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
984263bc
MD
215 continue; /* skip dummy task(s)*/
216
217 /* Call function */
218 (*((*sipp)->func))((*sipp)->udata);
219 }
220}
221
222static void
223linker_file_register_sysctls(linker_file_t lf)
224{
dc62b251 225 struct sysctl_oid **start, **stop, **oidp;
984263bc
MD
226
227 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
228 lf->filename));
229
dc62b251 230 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
d1786c1b 231 return;
dc62b251
MD
232 for (oidp = start; oidp < stop; oidp++)
233 sysctl_register_oid(*oidp);
984263bc
MD
234}
235
236static void
237linker_file_unregister_sysctls(linker_file_t lf)
238{
dc62b251 239 struct sysctl_oid **start, **stop, **oidp;
984263bc
MD
240
241 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
242 lf->filename));
243
dc62b251 244 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
984263bc 245 return;
dc62b251
MD
246 for (oidp = start; oidp < stop; oidp++)
247 sysctl_unregister_oid(*oidp);
984263bc
MD
248}
249
1c0e3286
SS
250static int
251linker_file_register_modules(linker_file_t lf)
252{
253 struct mod_metadata **start, **stop, **mdp;
254 const moduledata_t *moddata;
255 int first_error, error;
256
257 KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
258 lf->filename));
259
260 if (linker_file_lookup_set(lf, "modmetadata_set", &start, &stop, NULL) != 0) {
261 /*
262 * This fallback should be unnecessary, but if we get booted
263 * from boot2 instead of loader and we are missing our
264 * metadata then we have to try the best we can.
265 */
266 if (lf == linker_kernel_file) {
267 start = SET_BEGIN(modmetadata_set);
268 stop = SET_LIMIT(modmetadata_set);
269 } else
270 return (0);
271 }
272 first_error = 0;
273 for (mdp = start; mdp < stop; mdp++) {
274 if ((*mdp)->md_type != MDT_MODULE)
275 continue;
276 moddata = (*mdp)->md_data;
277 KLD_DPF(FILE, ("Registering module %s in %s\n", moddata->name, lf->filename));
278 error = module_register(moddata, lf);
279 if (error) {
280 kprintf("Module %s failed to register: %d\n", moddata->name, error);
281 if (first_error == 0)
282 first_error = error;
283 }
284 }
285 return (first_error);
286}
287
288static void
289linker_init_kernel_modules(void)
290{
291
292 linker_file_register_modules(linker_kernel_file);
293}
294
295SYSINIT(linker_kernel, SI_BOOT2_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0);
296
984263bc 297int
1c0e3286 298linker_load_file(const char *filename, linker_file_t *result)
984263bc
MD
299{
300 linker_class_t lc;
301 linker_file_t lf;
302 int foundfile, error = 0;
984263bc
MD
303
304 /* Refuse to load modules if securelevel raised */
7e42c007 305 if (securelevel > 0 || kernel_mem_readonly)
984263bc
MD
306 return EPERM;
307
308 lf = linker_find_file_by_name(filename);
309 if (lf) {
310 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
311 *result = lf;
312 lf->refs++;
313 goto out;
314 }
315
984263bc
MD
316 lf = NULL;
317 foundfile = 0;
89e1aaa0 318 TAILQ_FOREACH(lc, &classes, link) {
984263bc
MD
319 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
320 filename, lc->desc));
321
1c0e3286 322 error = lc->ops->load_file(filename, &lf);
984263bc
MD
323 /*
324 * If we got something other than ENOENT, then it exists but we cannot
325 * load it for some other reason.
326 */
327 if (error != ENOENT)
328 foundfile = 1;
329 if (lf) {
1c0e3286
SS
330 error = linker_file_register_modules(lf);
331 if (error == EEXIST) {
332 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */);
333 return (error);
b8fb634a 334 }
b8fb634a 335 linker_file_register_sysctls(lf);
1c0e3286
SS
336 linker_file_sysinit(lf);
337 lf->flags |= LINKER_FILE_LINKED;
984263bc 338 *result = lf;
1c0e3286 339 return (0);
984263bc
MD
340 }
341 }
342 /*
343 * Less than ideal, but tells the user whether it failed to load or
344 * the module was not found.
345 */
828a7fe6 346 if (foundfile) {
1c0e3286
SS
347 /*
348 * If the file type has not been recognized by the last try
349 * printout a message before to fail.
350 */
351 if (error == ENOSYS)
352 kprintf("linker_load_file: Unsupported file type\n");
353
828a7fe6
HP
354 /*
355 * Format not recognized or otherwise unloadable.
356 * When loading a module that is statically built into
357 * the kernel EEXIST percolates back up as the return
22628b14
SW
358 * value. Preserve this so that apps can recognize this
359 * special case.
828a7fe6
HP
360 */
361 if (error != EEXIST)
362 error = ENOEXEC;
32832096 363 } else {
984263bc 364 error = ENOENT; /* Nothing found */
32832096 365 }
984263bc
MD
366
367out:
984263bc
MD
368 return error;
369}
370
32e913d7 371
984263bc
MD
372linker_file_t
373linker_find_file_by_name(const char* filename)
374{
375 linker_file_t lf = 0;
376 char *koname;
a3d55cbb
MD
377 int i;
378
379 for (i = strlen(filename); i > 0 && filename[i-1] != '/'; --i)
380 ;
381 filename += i;
984263bc 382
efda3bd0 383 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
f8c7a42d 384 ksprintf(koname, "%s.ko", filename);
984263bc 385
df4f70a6 386 lockmgr(&lock, LK_SHARED);
89e1aaa0 387 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
388 if (!strcmp(lf->filename, koname))
389 break;
390 if (!strcmp(lf->filename, filename))
391 break;
392 }
df4f70a6 393 lockmgr(&lock, LK_RELEASE);
984263bc 394
984263bc 395 if (koname)
efda3bd0 396 kfree(koname, M_LINKER);
984263bc
MD
397 return lf;
398}
399
400linker_file_t
401linker_find_file_by_id(int fileid)
402{
403 linker_file_t lf = 0;
404
df4f70a6 405 lockmgr(&lock, LK_SHARED);
89e1aaa0 406 TAILQ_FOREACH(lf, &linker_files, link)
984263bc
MD
407 if (lf->id == fileid)
408 break;
df4f70a6 409 lockmgr(&lock, LK_RELEASE);
984263bc
MD
410
411 return lf;
412}
413
66021a66
SW
414int
415linker_file_foreach(linker_predicate_t *predicate, void *context)
416{
417 linker_file_t lf;
418 int retval = 0;
419
420 lockmgr(&lock, LK_SHARED);
421 TAILQ_FOREACH(lf, &linker_files, link) {
422 retval = predicate(lf, context);
423 if (retval != 0)
424 break;
425 }
426 lockmgr(&lock, LK_RELEASE);
427 return (retval);
428}
429
984263bc
MD
430linker_file_t
431linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
432{
433 linker_file_t lf = 0;
984263bc
MD
434 const char *filename;
435
436 filename = rindex(pathname, '/');
437 if (filename && filename[1])
438 filename++;
439 else
440 filename = pathname;
441
442 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
df4f70a6 443 lockmgr(&lock, LK_EXCLUSIVE);
1c0e3286 444 lf = kmalloc(sizeof(struct linker_file), M_LINKER, M_WAITOK);
984263bc
MD
445 bzero(lf, sizeof(*lf));
446
447 lf->refs = 1;
448 lf->userrefs = 0;
449 lf->flags = 0;
1c0e3286 450 lf->filename = linker_strdup(filename);
984263bc
MD
451 lf->id = next_file_id++;
452 lf->ndeps = 0;
453 lf->deps = NULL;
454 STAILQ_INIT(&lf->common);
455 TAILQ_INIT(&lf->modules);
456
457 lf->priv = priv;
458 lf->ops = ops;
459 TAILQ_INSERT_TAIL(&linker_files, lf, link);
460
df4f70a6 461 lockmgr(&lock, LK_RELEASE);
984263bc
MD
462 return lf;
463}
464
465int
466linker_file_unload(linker_file_t file)
467{
1c0e3286
SS
468 module_t mod, next;;
469 modlist_t ml, nextml;
984263bc
MD
470 struct common_symbol* cp;
471 int error = 0;
472 int i;
473
474 /* Refuse to unload modules if securelevel raised */
7e42c007 475 if (securelevel > 0 || kernel_mem_readonly)
984263bc
MD
476 return EPERM;
477
478 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
1c0e3286 479
df4f70a6 480 lockmgr(&lock, LK_EXCLUSIVE);
984263bc 481
1c0e3286
SS
482 /* Easy case of just dropping a reference. */
483 if (file->refs > 1) {
484 file->refs--;
485 lockmgr(&lock, LK_RELEASE);
486 return (0);
487 }
984263bc 488
1c0e3286
SS
489 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
490
491 /*
492 * Inform any modules associated with this file.
493 */
494 mod = TAILQ_FIRST(&file->modules);
495 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
496 next = module_getfnext(mod);
e7a2d403
MD
497
498 /*
1c0e3286
SS
499 * Give the module a chance to veto the unload. Note that the
500 * act of unloading the module may cause other modules in the
501 * same file list to be unloaded recursively.
e7a2d403 502 */
1c0e3286
SS
503 if ((error = module_unload(mod)) != 0) {
504 KLD_DPF(FILE, ("linker_file_unload: module %p vetoes unload\n",
505 mod));
506 lockmgr(&lock, LK_RELEASE);
507 file->refs--;
508 goto out;
984263bc 509 }
1c0e3286 510 module_release(mod);
984263bc
MD
511 }
512
1c0e3286
SS
513 TAILQ_FOREACH_MUTABLE(ml, &found_modules, link, nextml) {
514 if (ml->container == file) {
515 TAILQ_REMOVE(&found_modules, ml, link);
516 kfree(ml, M_LINKER);
517 }
984263bc
MD
518 }
519
520 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
521 if (file->flags & LINKER_FILE_LINKED) {
1c0e3286
SS
522 file->flags &= ~LINKER_FILE_LINKED;
523 lockmgr(&lock, LK_RELEASE);
984263bc
MD
524 linker_file_sysuninit(file);
525 linker_file_unregister_sysctls(file);
1c0e3286 526 lockmgr(&lock, LK_EXCLUSIVE);
984263bc
MD
527 }
528
529 TAILQ_REMOVE(&linker_files, file, link);
984263bc 530
1c0e3286
SS
531 if (file->deps) {
532 lockmgr(&lock, LK_RELEASE);
533 for (i = 0; i < file->ndeps; i++)
534 linker_file_unload(file->deps[i]);
535 lockmgr(&lock, LK_EXCLUSIVE);
536 kfree(file->deps, M_LINKER);
537 file->deps = NULL;
538 }
984263bc 539
1c0e3286
SS
540 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
541 STAILQ_REMOVE_HEAD(&file->common, link);
efda3bd0 542 kfree(cp, M_LINKER);
984263bc
MD
543 }
544
545 file->ops->unload(file);
1c0e3286
SS
546
547 if (file->filename) {
548 kfree(file->filename, M_LINKER);
549 file->filename = NULL;
550 }
551
efda3bd0 552 kfree(file, M_LINKER);
984263bc 553
1c0e3286
SS
554 lockmgr(&lock, LK_RELEASE);
555
984263bc
MD
556out:
557 return error;
558}
559
addd2777 560void
984263bc
MD
561linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
562{
563 linker_file_t* newdeps;
564
77652cad 565 newdeps = kmalloc((file->ndeps + 1) * sizeof(linker_file_t*),
e7b4468c 566 M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
567
568 if (file->deps) {
569 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
efda3bd0 570 kfree(file->deps, M_LINKER);
984263bc
MD
571 }
572 file->deps = newdeps;
573 file->deps[file->ndeps] = dep;
574 file->ndeps++;
984263bc
MD
575}
576
dc62b251
MD
577/*
578 * Locate a linker set and its contents.
579 * This is a helper function to avoid linker_if.h exposure elsewhere.
580 * Note: firstp and lastp are really void ***
581 */
582int
583linker_file_lookup_set(linker_file_t file, const char *name,
584 void *firstp, void *lastp, int *countp)
585{
586 return file->ops->lookup_set(file, name, firstp, lastp, countp);
587}
588
d1786c1b
MD
589int
590linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
984263bc
MD
591{
592 c_linker_sym_t sym;
593 linker_symval_t symval;
594 linker_file_t lf;
984263bc
MD
595 size_t common_size = 0;
596 int i;
597
1c0e3286 598 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
984263bc
MD
599 file, name, deps));
600
601 if (file->ops->lookup_symbol(file, name, &sym) == 0) {
602 file->ops->symbol_values(file, sym, &symval);
d1786c1b
MD
603
604 /*
605 * XXX Assume a common symbol if its value is 0 and it has a non-zero
606 * size, otherwise it could be an absolute symbol with a value of 0.
607 */
608 if (symval.value == 0 && symval.size != 0) {
984263bc
MD
609 /*
610 * For commons, first look them up in the dependancies and
611 * only allocate space if not found there.
612 */
613 common_size = symval.size;
d1786c1b 614 } else {
1c0e3286 615 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%p\n", symval.value));
d1786c1b
MD
616 *raddr = symval.value;
617 return 0;
984263bc
MD
618 }
619 }
984263bc
MD
620 if (deps) {
621 for (i = 0; i < file->ndeps; i++) {
d1786c1b 622 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
1c0e3286 623 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%p\n", *raddr));
d1786c1b 624 return 0;
984263bc
MD
625 }
626 }
627
628 /* If we have not found it in the dependencies, search globally */
89e1aaa0 629 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
630 /* But skip the current file if it's on the list */
631 if (lf == file)
632 continue;
633 /* And skip the files we searched above */
634 for (i = 0; i < file->ndeps; i++)
635 if (lf == file->deps[i])
636 break;
637 if (i < file->ndeps)
638 continue;
d1786c1b 639 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
1c0e3286 640 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%p\n", *raddr));
d1786c1b 641 return 0;
984263bc
MD
642 }
643 }
644 }
645
646 if (common_size > 0) {
647 /*
648 * This is a common symbol which was not found in the
649 * dependancies. We maintain a simple common symbol table in
650 * the file object.
651 */
652 struct common_symbol* cp;
653
89e1aaa0 654 STAILQ_FOREACH(cp, &file->common, link)
984263bc 655 if (!strcmp(cp->name, name)) {
1c0e3286 656 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%p\n", cp->address));
d1786c1b
MD
657 *raddr = cp->address;
658 return 0;
984263bc
MD
659 }
660
661 /*
662 * Round the symbol size up to align.
663 */
664 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
77652cad 665 cp = kmalloc(sizeof(struct common_symbol)
984263bc
MD
666 + common_size
667 + strlen(name) + 1,
e7b4468c 668 M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
669
670 cp->address = (caddr_t) (cp + 1);
671 cp->name = cp->address + common_size;
672 strcpy(cp->name, name);
673 bzero(cp->address, common_size);
674 STAILQ_INSERT_TAIL(&file->common, cp, link);
675
1c0e3286 676 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%p\n", cp->address));
d1786c1b
MD
677 *raddr = cp->address;
678 return 0;
984263bc
MD
679 }
680
6ef6faeb
SS
681#ifdef _KERNEL_VIRTUAL
682 *raddr = dlsym(RTLD_NEXT, name);
683 if (*raddr != NULL) {
f130e9af 684 KLD_DPF(SYM, ("linker_file_lookup_symbol: found dlsym=%p\n", *raddr));
6ef6faeb
SS
685 return 0;
686 }
687#endif
688
984263bc 689 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
d1786c1b 690 return ENOENT;
984263bc
MD
691}
692
693#ifdef DDB
694/*
695 * DDB Helpers. DDB has to look across multiple files with their own
696 * symbol tables and string tables.
697 *
698 * Note that we do not obey list locking protocols here. We really don't
699 * need DDB to hang because somebody's got the lock held. We'll take the
700 * chance that the files list is inconsistant instead.
701 */
702
703int
704linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
705{
706 linker_file_t lf;
707
89e1aaa0 708 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
709 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
710 return 0;
711 }
712 return ENOENT;
713}
714
715int
716linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
717{
718 linker_file_t lf;
719 u_long off = (uintptr_t)value;
720 u_long diff, bestdiff;
721 c_linker_sym_t best;
722 c_linker_sym_t es;
723
724 best = 0;
725 bestdiff = off;
89e1aaa0 726 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
727 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
728 continue;
729 if (es != 0 && diff < bestdiff) {
730 best = es;
731 bestdiff = diff;
732 }
733 if (bestdiff == 0)
734 break;
735 }
736 if (best) {
737 *sym = best;
738 *diffp = bestdiff;
739 return 0;
740 } else {
741 *sym = 0;
742 *diffp = off;
743 return ENOENT;
744 }
745}
746
747int
748linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
749{
750 linker_file_t lf;
751
89e1aaa0 752 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
753 if (lf->ops->symbol_values(lf, sym, symval) == 0)
754 return 0;
755 }
756 return ENOENT;
757}
758
759#endif
760
761/*
762 * Syscalls.
3919ced0
MD
763 *
764 * MPALMOSTSAFE
984263bc 765 */
984263bc 766int
753fd850 767sys_kldload(struct kldload_args *uap)
984263bc 768{
dadab5e9 769 struct thread *td = curthread;
1c0e3286
SS
770 char *file;
771 char *kldname, *modname;
984263bc
MD
772 linker_file_t lf;
773 int error = 0;
774
c7114eea 775 uap->sysmsg_result = -1;
984263bc 776
7e42c007 777 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
984263bc
MD
778 return EPERM;
779
faa1a67e 780 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
984263bc
MD
781 return error;
782
1c0e3286
SS
783 file = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
784 if ((error = copyinstr(uap->file, file, MAXPATHLEN, NULL)) != 0)
984263bc
MD
785 goto out;
786
1c0e3286
SS
787 /*
788 * If file does not contain a qualified name or any dot in it
789 * (kldname.ko, or kldname.ver.ko) treat it as an interface
790 * name.
791 */
792 if (index(file, '/') || index(file, '.')) {
793 kldname = file;
794 modname = NULL;
795 } else {
796 kldname = NULL;
797 modname = file;
984263bc
MD
798 }
799
3919ced0
MD
800 get_mplock();
801 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
802 rel_mplock();
803 if (error)
984263bc
MD
804 goto out;
805
806 lf->userrefs++;
c7114eea 807 uap->sysmsg_result = lf->id;
984263bc
MD
808
809out:
1c0e3286
SS
810 if (file)
811 kfree(file, M_TEMP);
984263bc
MD
812 return error;
813}
814
3919ced0
MD
815/*
816 * MPALMOSTSAFE
817 */
984263bc 818int
753fd850 819sys_kldunload(struct kldunload_args *uap)
984263bc 820{
dadab5e9 821 struct thread *td = curthread;
984263bc
MD
822 linker_file_t lf;
823 int error = 0;
824
7e42c007 825 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
984263bc
MD
826 return EPERM;
827
faa1a67e 828 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
984263bc
MD
829 return error;
830
3919ced0 831 get_mplock();
ab2eb4eb 832 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
833 if (lf) {
834 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
835 if (lf->userrefs == 0) {
6ea70f76 836 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
984263bc
MD
837 error = EBUSY;
838 goto out;
839 }
840 lf->userrefs--;
841 error = linker_file_unload(lf);
842 if (error)
843 lf->userrefs++;
3919ced0 844 } else {
984263bc 845 error = ENOENT;
3919ced0 846 }
984263bc 847out:
3919ced0 848 rel_mplock();
984263bc
MD
849 return error;
850}
851
3919ced0
MD
852/*
853 * MPALMOSTSAFE
854 */
984263bc 855int
753fd850 856sys_kldfind(struct kldfind_args *uap)
984263bc 857{
41c20dac 858 char *filename = NULL, *modulename;
984263bc 859 linker_file_t lf;
3919ced0 860 int error;
984263bc 861
c7114eea 862 uap->sysmsg_result = -1;
984263bc 863
efda3bd0 864 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
ab2eb4eb 865 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
984263bc
MD
866 goto out;
867
868 modulename = rindex(filename, '/');
869 if (modulename == NULL)
870 modulename = filename;
871
3919ced0 872 get_mplock();
984263bc
MD
873 lf = linker_find_file_by_name(modulename);
874 if (lf)
c7114eea 875 uap->sysmsg_result = lf->id;
984263bc
MD
876 else
877 error = ENOENT;
3919ced0 878 rel_mplock();
984263bc
MD
879
880out:
881 if (filename)
efda3bd0 882 kfree(filename, M_TEMP);
984263bc
MD
883 return error;
884}
885
3919ced0
MD
886/*
887 * MPALMOSTSAFE
888 */
984263bc 889int
753fd850 890sys_kldnext(struct kldnext_args *uap)
984263bc
MD
891{
892 linker_file_t lf;
893 int error = 0;
894
3919ced0
MD
895 get_mplock();
896 if (uap->fileid == 0) {
1c0e3286 897 lf = TAILQ_FIRST(&linker_files);
3919ced0 898 } else {
1c0e3286
SS
899 lf = linker_find_file_by_id(uap->fileid);
900 if (lf == NULL) {
901 error = ENOENT;
902 goto out;
903 }
904 lf = TAILQ_NEXT(lf, link);
984263bc
MD
905 }
906
1c0e3286
SS
907 /* Skip partially loaded files. */
908 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) {
909 lf = TAILQ_NEXT(lf, link);
910 }
911
912 if (lf)
913 uap->sysmsg_result = lf->id;
914 else
915 uap->sysmsg_result = 0;
984263bc 916
1c0e3286 917out:
3919ced0 918 rel_mplock();
984263bc
MD
919 return error;
920}
921
3919ced0
MD
922/*
923 * MPALMOSTSAFE
924 */
984263bc 925int
753fd850 926sys_kldstat(struct kldstat_args *uap)
984263bc
MD
927{
928 linker_file_t lf;
929 int error = 0;
930 int version;
931 struct kld_file_stat* stat;
932 int namelen;
933
3919ced0 934 get_mplock();
ab2eb4eb 935 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
936 if (!lf) {
937 error = ENOENT;
938 goto out;
939 }
940
ab2eb4eb 941 stat = uap->stat;
984263bc
MD
942
943 /*
944 * Check the version of the user's structure.
945 */
946 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
947 goto out;
948 if (version != sizeof(struct kld_file_stat)) {
949 error = EINVAL;
950 goto out;
951 }
952
953 namelen = strlen(lf->filename) + 1;
954 if (namelen > MAXPATHLEN)
955 namelen = MAXPATHLEN;
956 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
957 goto out;
958 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
959 goto out;
960 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
961 goto out;
962 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
963 goto out;
964 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
965 goto out;
966
c7114eea 967 uap->sysmsg_result = 0;
984263bc
MD
968
969out:
3919ced0 970 rel_mplock();
984263bc
MD
971 return error;
972}
973
3919ced0
MD
974/*
975 * MPALMOSTSAFE
976 */
984263bc 977int
753fd850 978sys_kldfirstmod(struct kldfirstmod_args *uap)
984263bc
MD
979{
980 linker_file_t lf;
981 int error = 0;
982
3919ced0 983 get_mplock();
ab2eb4eb 984 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
985 if (lf) {
986 if (TAILQ_FIRST(&lf->modules))
c7114eea 987 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules));
984263bc 988 else
c7114eea 989 uap->sysmsg_result = 0;
3919ced0 990 } else {
984263bc 991 error = ENOENT;
3919ced0
MD
992 }
993 rel_mplock();
984263bc
MD
994
995 return error;
996}
997
3919ced0
MD
998/*
999 * MPALMOSTSAFE
1000 */
984263bc 1001int
753fd850 1002sys_kldsym(struct kldsym_args *uap)
984263bc
MD
1003{
1004 char *symstr = NULL;
1005 c_linker_sym_t sym;
1006 linker_symval_t symval;
1007 linker_file_t lf;
1008 struct kld_sym_lookup lookup;
1009 int error = 0;
1010
3919ced0 1011 get_mplock();
ab2eb4eb 1012 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
984263bc 1013 goto out;
ab2eb4eb 1014 if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP) {
984263bc
MD
1015 error = EINVAL;
1016 goto out;
1017 }
1018
efda3bd0 1019 symstr = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
984263bc
MD
1020 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1021 goto out;
1022
ab2eb4eb
DR
1023 if (uap->fileid != 0) {
1024 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
1025 if (lf == NULL) {
1026 error = ENOENT;
1027 goto out;
1028 }
1029 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
1030 lf->ops->symbol_values(lf, sym, &symval) == 0) {
1031 lookup.symvalue = (uintptr_t)symval.value;
1032 lookup.symsize = symval.size;
ab2eb4eb 1033 error = copyout(&lookup, uap->data, sizeof(lookup));
984263bc
MD
1034 } else
1035 error = ENOENT;
1036 } else {
89e1aaa0 1037 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
1038 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
1039 lf->ops->symbol_values(lf, sym, &symval) == 0) {
1040 lookup.symvalue = (uintptr_t)symval.value;
1041 lookup.symsize = symval.size;
ab2eb4eb 1042 error = copyout(&lookup, uap->data, sizeof(lookup));
984263bc
MD
1043 break;
1044 }
1045 }
1046 if (!lf)
1047 error = ENOENT;
1048 }
1049out:
3919ced0 1050 rel_mplock();
984263bc 1051 if (symstr)
efda3bd0 1052 kfree(symstr, M_TEMP);
984263bc
MD
1053 return error;
1054}
1055
32832096 1056/*
1c0e3286 1057 * Preloaded module support
32832096 1058 */
1c0e3286
SS
1059
1060static modlist_t
1061modlist_lookup(const char *name, int ver)
6456e0ad 1062{
1c0e3286 1063 modlist_t mod;
6456e0ad 1064
1c0e3286
SS
1065 TAILQ_FOREACH(mod, &found_modules, link) {
1066 if (strcmp(mod->name, name) == 0 && (ver == 0 || mod->version == ver))
1067 return (mod);
6456e0ad 1068 }
1c0e3286
SS
1069 return (NULL);
1070}
6456e0ad 1071
1c0e3286
SS
1072static modlist_t
1073modlist_lookup2(const char *name, struct mod_depend *verinfo)
1074{
1075 modlist_t mod, bestmod;
1076 int ver;
1077
1078 if (verinfo == NULL)
1079 return (modlist_lookup(name, 0));
1080 bestmod = NULL;
1081 TAILQ_FOREACH(mod, &found_modules, link) {
1082 if (strcmp(mod->name, name) != 0)
1083 continue;
1084 ver = mod->version;
1085 if (ver == verinfo->md_ver_preferred)
1086 return (mod);
1087 if (ver >= verinfo->md_ver_minimum &&
1088 ver <= verinfo->md_ver_maximum &&
1089 (bestmod == NULL || ver > bestmod->version))
1090 bestmod = mod;
1091 }
1092 return (bestmod);
1093}
1094
32e913d7
JT
1095int
1096linker_reference_module(const char *modname, struct mod_depend *verinfo,
1097 linker_file_t *result)
1098{
1099 modlist_t mod;
1100 int error;
1101
1102 lockmgr(&lock, LK_SHARED);
1103 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
1104 *result = mod->container;
1105 (*result)->refs++;
1106 lockmgr(&lock, LK_RELEASE);
1107 return (0);
1108 }
1109
32e913d7 1110 lockmgr(&lock, LK_RELEASE);
240c3f5a
JT
1111 get_mplock();
1112 error = linker_load_module(NULL, modname, NULL, verinfo, result);
1113 rel_mplock();
32e913d7
JT
1114 return (error);
1115}
1116
1117int
1118linker_release_module(const char *modname, struct mod_depend *verinfo,
1119 linker_file_t lf)
1120{
1121 modlist_t mod;
1122 int error;
1123
1124 lockmgr(&lock, LK_SHARED);
1125 if (lf == NULL) {
1126 KASSERT(modname != NULL,
1127 ("linker_release_module: no file or name"));
1128 mod = modlist_lookup2(modname, verinfo);
1129 if (mod == NULL) {
1130 lockmgr(&lock, LK_RELEASE);
1131 return (ESRCH);
1132 }
1133 lf = mod->container;
1134 } else
1135 KASSERT(modname == NULL && verinfo == NULL,
1136 ("linker_release_module: both file and name"));
32e913d7 1137 lockmgr(&lock, LK_RELEASE);
240c3f5a
JT
1138 get_mplock();
1139 error = linker_file_unload(lf);
1140 rel_mplock();
32e913d7
JT
1141 return (error);
1142}
1143
1c0e3286
SS
1144static modlist_t
1145modlist_newmodule(const char *modname, int version, linker_file_t container)
1146{
1147 modlist_t mod;
1148
1149 mod = kmalloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1150 if (mod == NULL)
1151 panic("no memory for module list");
1152 mod->container = container;
1153 mod->name = modname;
1154 mod->version = version;
1155 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1156 return (mod);
1157}
1158
1159static void
1160linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1161 struct mod_metadata **stop, int preload)
1162{
1163 struct mod_metadata *mp, **mdp;
1164 const char *modname;
1165 int ver;
1166
1167 for (mdp = start; mdp < stop; mdp++) {
1168 mp = *mdp;
1169 if (mp->md_type != MDT_VERSION)
1170 continue;
1171 modname = mp->md_cval;
1172 ver = ((struct mod_version *)mp->md_data)->mv_version;
1173 if (modlist_lookup(modname, ver) != NULL) {
1174 kprintf("module %s already present!\n", modname);
1175 /* XXX what can we do? this is a build error. :-( */
6456e0ad 1176 continue;
6456e0ad 1177 }
1c0e3286 1178 modlist_newmodule(modname, ver, lf);
6456e0ad 1179 }
6456e0ad
MD
1180}
1181
984263bc
MD
1182static void
1183linker_preload(void* arg)
1184{
1185 caddr_t modptr;
1c0e3286 1186 const char *modname, *nmodname;
984263bc 1187 char *modtype;
1c0e3286 1188 linker_file_t lf, nlf;
984263bc
MD
1189 linker_class_t lc;
1190 int error;
1c0e3286
SS
1191 linker_file_list_t loaded_files;
1192 linker_file_list_t depended_files;
1193 struct mod_metadata *mp, *nmp;
1194 struct mod_metadata **start, **stop, **mdp, **nmdp;
1195 struct mod_depend *verinfo;
1196 int nver;
1197 int resolves;
1198 modlist_t mod;
dc62b251 1199 struct sysinit **si_start, **si_stop;
984263bc 1200
1c0e3286
SS
1201 TAILQ_INIT(&loaded_files);
1202 TAILQ_INIT(&depended_files);
1203 TAILQ_INIT(&found_modules);
1204
984263bc
MD
1205 modptr = NULL;
1206 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1207 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1208 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1209 if (modname == NULL) {
6ea70f76 1210 kprintf("Preloaded module at %p does not have a name!\n", modptr);
984263bc
MD
1211 continue;
1212 }
1213 if (modtype == NULL) {
6ea70f76 1214 kprintf("Preloaded module at %p does not have a type!\n", modptr);
984263bc
MD
1215 continue;
1216 }
6456e0ad 1217
1c0e3286
SS
1218 if (bootverbose)
1219 kprintf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
984263bc 1220 lf = NULL;
89e1aaa0 1221 TAILQ_FOREACH(lc, &classes, link) {
1c0e3286
SS
1222 error = lc->ops->preload_file(modname, &lf);
1223 if (!error)
984263bc 1224 break;
1c0e3286 1225 lf = NULL;
984263bc 1226 }
1c0e3286
SS
1227 if (lf)
1228 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1229 }
984263bc 1230
1c0e3286
SS
1231 /*
1232 * First get a list of stuff in the kernel.
1233 */
1234 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1235 &stop, NULL) == 0)
1236 linker_addmodules(linker_kernel_file, start, stop, 1);
1237
1238 /*
1239 * This is a once-off kinky bubble sort to resolve relocation
1240 * dependency requirements.
1241 */
1242restart:
1243 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1244 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
1245 /*
1246 * First, look to see if we would successfully link with this
1247 * stuff.
1248 */
1249 resolves = 1; /* unless we know otherwise */
1250 if (!error) {
1251 for (mdp = start; mdp < stop; mdp++) {
1252 mp = *mdp;
1253 if (mp->md_type != MDT_DEPEND)
1254 continue;
1255 modname = mp->md_cval;
1256 verinfo = mp->md_data;
1257 for (nmdp = start; nmdp < stop; nmdp++) {
1258 nmp = *nmdp;
1259 if (nmp->md_type != MDT_VERSION)
1260 continue;
1261 nmodname = nmp->md_cval;
1262 if (strcmp(modname, nmodname) == 0)
1263 break;
1264 }
1265 if (nmdp < stop)/* it's a self reference */
1266 continue;
1267
1268 /*
1269 * ok, the module isn't here yet, we
1270 * are not finished
984263bc 1271 */
1c0e3286
SS
1272 if (modlist_lookup2(modname, verinfo) == NULL)
1273 resolves = 0;
1274 }
1275 }
1276 /*
1277 * OK, if we found our modules, we can link. So, "provide"
1278 * the modules inside and add it to the end of the link order
1279 * list.
1280 */
1281 if (resolves) {
1282 if (!error) {
1283 for (mdp = start; mdp < stop; mdp++) {
1284 mp = *mdp;
1285 if (mp->md_type != MDT_VERSION)
1286 continue;
1287 modname = mp->md_cval;
1288 nver = ((struct mod_version *)mp->md_data)->mv_version;
1289 if (modlist_lookup(modname, nver) != NULL) {
1290 kprintf("module %s already present!\n", modname);
1291 TAILQ_REMOVE(&loaded_files, lf, loaded);
1292 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
1293 /* we changed tailq next ptr */
1294 goto restart;
984263bc 1295 }
1c0e3286 1296 modlist_newmodule(modname, nver, lf);
984263bc 1297 }
984263bc 1298 }
1c0e3286
SS
1299 TAILQ_REMOVE(&loaded_files, lf, loaded);
1300 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1301 /*
1302 * Since we provided modules, we need to restart the
1303 * sort so that the previous files that depend on us
1304 * have a chance. Also, we've busted the tailq next
1305 * pointer with the REMOVE.
1306 */
1307 goto restart;
1308 }
1309 }
1310
1311 /*
1312 * At this point, we check to see what could not be resolved..
1313 */
1314 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1315 TAILQ_REMOVE(&loaded_files, lf, loaded);
1316 kprintf("KLD file %s is missing dependencies\n", lf->filename);
1317 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
1318 }
1319
1320 /*
1321 * We made it. Finish off the linking in the order we determined.
1322 */
1323 TAILQ_FOREACH_MUTABLE(lf, &depended_files, loaded, nlf) {
1324 if (linker_kernel_file) {
1325 linker_kernel_file->refs++;
1326 linker_file_add_dependancy(lf, linker_kernel_file);
1327 }
1328 lf->userrefs++;
1329
1330 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
1331 if (!error) {
1332 for (mdp = start; mdp < stop; mdp++) {
1333 mp = *mdp;
1334 if (mp->md_type != MDT_DEPEND)
1335 continue;
1336 modname = mp->md_cval;
1337 verinfo = mp->md_data;
1338 mod = modlist_lookup2(modname, verinfo);
1339 /* Don't count self-dependencies */
1340 if (lf == mod->container)
1341 continue;
1342 mod->container->refs++;
1343 linker_file_add_dependancy(lf, mod->container);
1344 }
1345 }
1346 /*
1347 * Now do relocation etc using the symbol search paths
1348 * established by the dependencies
1349 */
1350 error = lf->ops->preload_finish(lf);
1351 if (error) {
1352 TAILQ_REMOVE(&depended_files, lf, loaded);
1353 kprintf("KLD file %s - could not finalize loading\n",
1354 lf->filename);
1355 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */);
1356 continue;
984263bc 1357 }
1c0e3286
SS
1358 linker_file_register_modules(lf);
1359 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0)
1360 sysinit_add(si_start, si_stop);
1361 linker_file_register_sysctls(lf);
1362 lf->flags |= LINKER_FILE_LINKED;
984263bc 1363 }
1c0e3286 1364 /* woohoo! we made it! */
984263bc
MD
1365}
1366
ba39e2e0 1367SYSINIT(preload, SI_BOOT2_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
984263bc
MD
1368
1369/*
1370 * Search for a not-loaded module by name.
1371 *
1372 * Modules may be found in the following locations:
1373 *
1374 * - preloaded (result is just the module name)
1375 * - on disk (result is full path to module)
1376 *
1377 * If the module name is qualified in any way (contains path, etc.)
1378 * the we simply return a copy of it.
1379 *
1380 * The search path can be manipulated via sysctl. Note that we use the ';'
1381 * character as a separator to be consistent with the bootloader.
1382 */
1383
f4e821f3 1384static char linker_path[MAXPATHLEN] = "/boot;/boot/modules;/;/modules";
984263bc
MD
1385
1386SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1387 sizeof(linker_path), "module load search path");
446d0710 1388TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
984263bc 1389
984263bc
MD
1390char *
1391linker_search_path(const char *name)
1392{
fad57d0e 1393 struct nlookupdata nd;
984263bc 1394 char *cp, *ep, *result;
555d1043 1395 size_t name_len, prefix_len;
1c0e3286 1396 size_t result_len;
555d1043 1397 int sep;
984263bc
MD
1398 int error;
1399 enum vtype type;
1c0e3286
SS
1400 const char *exts[] = { "", ".ko", NULL };
1401 const char **ext;
984263bc
MD
1402
1403 /* qualified at all? */
1404 if (index(name, '/'))
1405 return(linker_strdup(name));
1406
1407 /* traverse the linker path */
1408 cp = linker_path;
555d1043 1409 name_len = strlen(name);
984263bc
MD
1410 for (;;) {
1411
1412 /* find the end of this component */
1413 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1414 ;
555d1043
YT
1415 prefix_len = ep - cp;
1416 /* if this component doesn't end with a slash, add one */
1417 if (ep == cp || *(ep - 1) != '/')
1418 sep = 1;
1419 else
1420 sep = 0;
1421
ad07b101 1422 /*
1c0e3286 1423 * +2+3 : possible separator, plus terminator + possible extension.
ad07b101 1424 */
1c0e3286 1425 result = kmalloc(prefix_len + name_len + 2+3, M_LINKER, M_WAITOK);
984263bc 1426
555d1043
YT
1427 strncpy(result, cp, prefix_len);
1428 if (sep)
1429 result[prefix_len++] = '/';
1430 strcpy(result + prefix_len, name);
984263bc 1431
1c0e3286
SS
1432 result_len = strlen(result);
1433 for (ext = exts; *ext != NULL; ext++) {
1434 strcpy(result + result_len, *ext);
1435
1436 /*
1437 * Attempt to open the file, and return the path if we succeed and it's
1438 * a regular file.
1439 */
1440 error = nlookup_init(&nd, result, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
1441 if (error == 0)
1442 error = vn_open(&nd, NULL, FREAD, 0);
1443 if (error == 0) {
1444 type = nd.nl_open_vp->v_type;
1445 if (type == VREG) {
1446 nlookup_done(&nd);
1447 return (result);
1448 }
fad57d0e 1449 }
1c0e3286 1450 nlookup_done(&nd);
984263bc 1451 }
1c0e3286 1452
efda3bd0 1453 kfree(result, M_LINKER);
984263bc
MD
1454
1455 if (*ep == 0)
1456 break;
1457 cp = ep + 1;
1458 }
1459 return(NULL);
1460}
1c0e3286
SS
1461
1462/*
1463 * Find a file which contains given module and load it, if "parent" is not
1464 * NULL, register a reference to it.
1465 */
1466static int
1467linker_load_module(const char *kldname, const char *modname,
1468 struct linker_file *parent, struct mod_depend *verinfo,
1469 struct linker_file **lfpp)
1470{
1471 linker_file_t lfdep;
1472 const char *filename;
1473 char *pathname;
1474 int error;
1475
1476 if (modname == NULL) {
1477 /*
1478 * We have to load KLD
1479 */
1480 KASSERT(verinfo == NULL, ("linker_load_module: verinfo is not NULL"));
1481 pathname = linker_search_path(kldname);
1482 } else {
1483 if (modlist_lookup2(modname, verinfo) != NULL)
1484 return (EEXIST);
1485 if (kldname != NULL)
240c3f5a 1486 {
1c0e3286 1487 pathname = linker_strdup(kldname);
240c3f5a 1488 }
1c0e3286
SS
1489 else if (rootvnode == NULL)
1490 pathname = NULL;
1491 else
240c3f5a 1492 {
1c0e3286 1493 pathname = linker_search_path(modname);
240c3f5a 1494 }
1c0e3286
SS
1495#if 0
1496 /*
1497 * Need to find a KLD with required module
1498 */
1499 pathname = linker_search_module(modname,
1500 strlen(modname), verinfo);
1501#endif
1502 }
1503 if (pathname == NULL)
1504 return (ENOENT);
1505
1506 /*
1507 * Can't load more than one file with the same basename XXX:
1508 * Actually it should be possible to have multiple KLDs with
1509 * the same basename but different path because they can
1510 * provide different versions of the same modules.
1511 */
1512 filename = rindex(pathname, '/');
1513 if (filename == NULL)
1514 filename = filename;
1515 else
1516 filename++;
1517 if (linker_find_file_by_name(filename))
1518 error = EEXIST;
1519 else
1520 do {
1521 error = linker_load_file(pathname, &lfdep);
1522 if (error)
1523 break;
1524 if (modname && verinfo && modlist_lookup2(modname, verinfo) == NULL) {
1525 linker_file_unload(lfdep /* , LINKER_UNLOAD_FORCE */ );
1526 error = ENOENT;
1527 break;
1528 }
1529 if (parent) {
1530 linker_file_add_dependancy(parent, lfdep);
1531 }
1532 if (lfpp)
1533 *lfpp = lfdep;
1534 } while (0);
1535 kfree(pathname, M_LINKER);
1536 return (error);
1537}
1538
1539/*
1540 * This routine is responsible for finding dependencies of userland initiated
1541 * kldload(2)'s of files.
1542 */
1543int
1544linker_load_dependencies(linker_file_t lf)
1545{
1546 linker_file_t lfdep;
1547 struct mod_metadata **start, **stop, **mdp, **nmdp;
1548 struct mod_metadata *mp, *nmp;
1549 struct mod_depend *verinfo;
1550 modlist_t mod;
1551 const char *modname, *nmodname;
1552 int ver, error = 0, count;
1553
1554 /*
1555 * All files are dependant on /kernel.
1556 */
1557 if (linker_kernel_file) {
1558 linker_kernel_file->refs++;
1559 linker_file_add_dependancy(lf, linker_kernel_file);
1560 }
1561 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, &count) != 0)
1562 return (0);
1563 for (mdp = start; mdp < stop; mdp++) {
1564 mp = *mdp;
1565 if (mp->md_type != MDT_VERSION)
1566 continue;
1567 modname = mp->md_cval;
1568 ver = ((struct mod_version *)mp->md_data)->mv_version;
1569 mod = modlist_lookup(modname, ver);
1570 if (mod != NULL) {
1571 kprintf("interface %s.%d already present in the KLD '%s'!\n",
1572 modname, ver, mod->container->filename);
1573 return (EEXIST);
1574 }
1575 }
1576
1577 for (mdp = start; mdp < stop; mdp++) {
1578 mp = *mdp;
1579 if (mp->md_type != MDT_DEPEND)
1580 continue;
1581 modname = mp->md_cval;
1582 verinfo = mp->md_data;
1583 nmodname = NULL;
1584 for (nmdp = start; nmdp < stop; nmdp++) {
1585 nmp = *nmdp;
1586 if (nmp->md_type != MDT_VERSION)
1587 continue;
1588 nmodname = nmp->md_cval;
1589 if (strcmp(modname, nmodname) == 0)
1590 break;
1591 }
1592 if (nmdp < stop) /* early exit, it's a self reference */
1593 continue;
1594 mod = modlist_lookup2(modname, verinfo);
1595 if (mod) { /* woohoo, it's loaded already */
1596 lfdep = mod->container;
1597 lfdep->refs++;
1598 linker_file_add_dependancy(lf, lfdep);
1599 continue;
1600 }
1601 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
1602 if (error) {
1603 kprintf("KLD %s: depends on %s - not available or version mismatch\n",
1604 lf->filename, modname);
1605 break;
1606 }
1607 }
1608
1609 if (error)
1610 return (error);
1611 linker_addmodules(lf, start, stop, 0);
1612 return (error);
1613}