Merge from vendor branch LIBEVENT:
[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 $
89e1aaa0 27 * $DragonFly: src/sys/kern/kern_linker.c,v 1.42 2008/01/10 10:31:48 matthias 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>
39#include <sys/lock.h>
40#include <sys/module.h>
89e1aaa0 41#include <sys/queue.h>
984263bc
MD
42#include <sys/linker.h>
43#include <sys/fcntl.h>
44#include <sys/libkern.h>
fad57d0e 45#include <sys/nlookup.h>
984263bc
MD
46#include <sys/vnode.h>
47#include <sys/sysctl.h>
48
49#include <vm/vm_zone.h>
50
6ef6faeb
SS
51#ifdef _KERNEL_VIRTUAL
52#include <dlfcn.h>
53#endif
54
984263bc
MD
55#ifdef KLD_DEBUG
56int kld_debug = 0;
57#endif
58
6456e0ad
MD
59/* Metadata from the static kernel */
60SET_DECLARE(modmetadata_set, struct mod_metadata);
984263bc 61MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
6456e0ad 62
984263bc
MD
63linker_file_t linker_current_file;
64linker_file_t linker_kernel_file;
65
66static struct lock lock; /* lock for the file list */
67static linker_class_list_t classes;
68static linker_file_list_t linker_files;
69static int next_file_id = 1;
70
71static void
72linker_init(void* arg)
73{
f2770c70 74 lockinit(&lock, "klink", 0, 0);
984263bc
MD
75 TAILQ_INIT(&classes);
76 TAILQ_INIT(&linker_files);
77}
78
ba39e2e0 79SYSINIT(linker, SI_BOOT2_KLD, SI_ORDER_FIRST, linker_init, 0);
984263bc
MD
80
81int
82linker_add_class(const char* desc, void* priv,
83 struct linker_class_ops* ops)
84{
85 linker_class_t lc;
86
e7b4468c 87 lc = kmalloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT | M_ZERO);
984263bc
MD
88 if (!lc)
89 return ENOMEM;
984263bc
MD
90
91 lc->desc = desc;
92 lc->priv = priv;
93 lc->ops = ops;
94 TAILQ_INSERT_HEAD(&classes, lc, link);
95
96 return 0;
97}
98
99static int
100linker_file_sysinit(linker_file_t lf)
101{
dc62b251 102 struct sysinit** start, ** stop;
984263bc
MD
103 struct sysinit** sipp;
104 struct sysinit** xipp;
105 struct sysinit* save;
106 const moduledata_t *moddata;
107 int error;
108
109 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
110 lf->filename));
111
dc62b251 112 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
984263bc
MD
113 return 0; /* XXX is this correct ? No sysinit ? */
114
115 /* HACK ALERT! */
dc62b251 116 for (sipp = start; sipp < stop; sipp++) {
984263bc
MD
117 if ((*sipp)->func == module_register_init) {
118 moddata = (*sipp)->udata;
119 error = module_register(moddata, lf);
120 if (error) {
6ea70f76 121 kprintf("linker_file_sysinit \"%s\" failed to register! %d\n",
984263bc
MD
122 lf->filename, error);
123 return error;
124 }
125 }
126 }
127
128 /*
129 * Perform a bubble sort of the system initialization objects by
130 * their subsystem (primary key) and order (secondary key).
131 *
132 * Since some things care about execution order, this is the
133 * operation which ensures continued function.
134 */
dc62b251
MD
135 for (sipp = start; sipp < stop; sipp++) {
136 for (xipp = sipp + 1; xipp < stop; xipp++) {
984263bc
MD
137 if ((*sipp)->subsystem < (*xipp)->subsystem ||
138 ((*sipp)->subsystem == (*xipp)->subsystem &&
139 (*sipp)->order <= (*xipp)->order))
140 continue; /* skip*/
141 save = *sipp;
142 *sipp = *xipp;
143 *xipp = save;
144 }
145 }
146
147
148 /*
149 * Traverse the (now) ordered list of system initialization tasks.
150 * Perform each task, and continue on to the next task.
151 */
dc62b251 152 for (sipp = start; sipp < stop; sipp++) {
ba39e2e0 153 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
984263bc
MD
154 continue; /* skip dummy task(s)*/
155
156 /* Call function */
157 (*((*sipp)->func))((*sipp)->udata);
158 }
159 return 0; /* no errors */
160}
161
162static void
163linker_file_sysuninit(linker_file_t lf)
164{
dc62b251 165 struct sysinit** start, ** stop;
984263bc
MD
166 struct sysinit** sipp;
167 struct sysinit** xipp;
168 struct sysinit* save;
169
170 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
171 lf->filename));
172
dc62b251 173 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
984263bc
MD
174 return;
175
176 /*
177 * Perform a reverse bubble sort of the system initialization objects
178 * by their subsystem (primary key) and order (secondary key).
179 *
180 * Since some things care about execution order, this is the
181 * operation which ensures continued function.
182 */
dc62b251
MD
183 for (sipp = start; sipp < stop; sipp++) {
184 for (xipp = sipp + 1; xipp < stop; xipp++) {
984263bc
MD
185 if ((*sipp)->subsystem > (*xipp)->subsystem ||
186 ((*sipp)->subsystem == (*xipp)->subsystem &&
187 (*sipp)->order >= (*xipp)->order))
188 continue; /* skip*/
189 save = *sipp;
190 *sipp = *xipp;
191 *xipp = save;
192 }
193 }
194
195
196 /*
197 * Traverse the (now) ordered list of system initialization tasks.
198 * Perform each task, and continue on to the next task.
199 */
dc62b251 200 for (sipp = start; sipp < stop; sipp++) {
ba39e2e0 201 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
984263bc
MD
202 continue; /* skip dummy task(s)*/
203
204 /* Call function */
205 (*((*sipp)->func))((*sipp)->udata);
206 }
207}
208
209static void
210linker_file_register_sysctls(linker_file_t lf)
211{
dc62b251 212 struct sysctl_oid **start, **stop, **oidp;
984263bc
MD
213
214 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
215 lf->filename));
216
dc62b251 217 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
d1786c1b 218 return;
dc62b251
MD
219 for (oidp = start; oidp < stop; oidp++)
220 sysctl_register_oid(*oidp);
984263bc
MD
221}
222
223static void
224linker_file_unregister_sysctls(linker_file_t lf)
225{
dc62b251 226 struct sysctl_oid **start, **stop, **oidp;
984263bc
MD
227
228 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
229 lf->filename));
230
dc62b251 231 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
984263bc 232 return;
dc62b251
MD
233 for (oidp = start; oidp < stop; oidp++)
234 sysctl_unregister_oid(*oidp);
984263bc
MD
235}
236
237int
238linker_load_file(const char* filename, linker_file_t* result)
239{
240 linker_class_t lc;
241 linker_file_t lf;
242 int foundfile, error = 0;
243 char *koname = NULL;
244
245 /* Refuse to load modules if securelevel raised */
7e42c007 246 if (securelevel > 0 || kernel_mem_readonly)
984263bc
MD
247 return EPERM;
248
249 lf = linker_find_file_by_name(filename);
250 if (lf) {
251 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
252 *result = lf;
253 lf->refs++;
254 goto out;
255 }
32832096
MD
256 if (find_mod_metadata(filename)) {
257 if (linker_kernel_file)
258 ++linker_kernel_file->refs;
259 *result = linker_kernel_file;
260 goto out;
261 }
984263bc 262
efda3bd0 263 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
f8c7a42d 264 ksprintf(koname, "%s.ko", filename);
984263bc
MD
265 lf = NULL;
266 foundfile = 0;
89e1aaa0 267 TAILQ_FOREACH(lc, &classes, link) {
984263bc
MD
268 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
269 filename, lc->desc));
270
271 error = lc->ops->load_file(koname, &lf); /* First with .ko */
272 if (lf == NULL && error == ENOENT)
273 error = lc->ops->load_file(filename, &lf); /* Then try without */
274 /*
275 * If we got something other than ENOENT, then it exists but we cannot
276 * load it for some other reason.
277 */
278 if (error != ENOENT)
279 foundfile = 1;
280 if (lf) {
281 linker_file_register_sysctls(lf);
282 error = linker_file_sysinit(lf);
283
284 *result = lf;
285 goto out;
286 }
287 }
288 /*
289 * Less than ideal, but tells the user whether it failed to load or
290 * the module was not found.
291 */
828a7fe6
HP
292 if (foundfile) {
293 /*
294 * Format not recognized or otherwise unloadable.
295 * When loading a module that is statically built into
296 * the kernel EEXIST percolates back up as the return
22628b14
SW
297 * value. Preserve this so that apps can recognize this
298 * special case.
828a7fe6
HP
299 */
300 if (error != EEXIST)
301 error = ENOEXEC;
32832096 302 } else {
984263bc 303 error = ENOENT; /* Nothing found */
32832096 304 }
984263bc
MD
305
306out:
307 if (koname)
efda3bd0 308 kfree(koname, M_LINKER);
984263bc
MD
309 return error;
310}
311
312linker_file_t
313linker_find_file_by_name(const char* filename)
314{
315 linker_file_t lf = 0;
316 char *koname;
a3d55cbb
MD
317 int i;
318
319 for (i = strlen(filename); i > 0 && filename[i-1] != '/'; --i)
320 ;
321 filename += i;
984263bc 322
efda3bd0 323 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
f8c7a42d 324 ksprintf(koname, "%s.ko", filename);
984263bc 325
df4f70a6 326 lockmgr(&lock, LK_SHARED);
89e1aaa0 327 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
328 if (!strcmp(lf->filename, koname))
329 break;
330 if (!strcmp(lf->filename, filename))
331 break;
332 }
df4f70a6 333 lockmgr(&lock, LK_RELEASE);
984263bc 334
984263bc 335 if (koname)
efda3bd0 336 kfree(koname, M_LINKER);
984263bc
MD
337 return lf;
338}
339
340linker_file_t
341linker_find_file_by_id(int fileid)
342{
343 linker_file_t lf = 0;
344
df4f70a6 345 lockmgr(&lock, LK_SHARED);
89e1aaa0 346 TAILQ_FOREACH(lf, &linker_files, link)
984263bc
MD
347 if (lf->id == fileid)
348 break;
df4f70a6 349 lockmgr(&lock, LK_RELEASE);
984263bc
MD
350
351 return lf;
352}
353
354linker_file_t
355linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
356{
357 linker_file_t lf = 0;
358 int namelen;
359 const char *filename;
360
361 filename = rindex(pathname, '/');
362 if (filename && filename[1])
363 filename++;
364 else
365 filename = pathname;
366
367 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
df4f70a6 368 lockmgr(&lock, LK_EXCLUSIVE);
984263bc 369 namelen = strlen(filename) + 1;
efda3bd0 370 lf = kmalloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
984263bc
MD
371 bzero(lf, sizeof(*lf));
372
373 lf->refs = 1;
374 lf->userrefs = 0;
375 lf->flags = 0;
376 lf->filename = (char*) (lf + 1);
377 strcpy(lf->filename, filename);
378 lf->id = next_file_id++;
379 lf->ndeps = 0;
380 lf->deps = NULL;
381 STAILQ_INIT(&lf->common);
382 TAILQ_INIT(&lf->modules);
383
384 lf->priv = priv;
385 lf->ops = ops;
386 TAILQ_INSERT_TAIL(&linker_files, lf, link);
387
df4f70a6 388 lockmgr(&lock, LK_RELEASE);
984263bc
MD
389 return lf;
390}
391
392int
393linker_file_unload(linker_file_t file)
394{
395 module_t mod, next;
396 struct common_symbol* cp;
397 int error = 0;
398 int i;
399
400 /* Refuse to unload modules if securelevel raised */
7e42c007 401 if (securelevel > 0 || kernel_mem_readonly)
984263bc
MD
402 return EPERM;
403
404 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
df4f70a6 405 lockmgr(&lock, LK_EXCLUSIVE);
984263bc
MD
406 if (file->refs == 1) {
407 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
408 /*
e7a2d403 409 * Temporarily bump file->refs to prevent recursive unloading
984263bc 410 */
e7a2d403 411 ++file->refs;
984263bc 412
e7a2d403
MD
413 /*
414 * Inform any modules associated with this file.
415 */
416 mod = TAILQ_FIRST(&file->modules);
417 while (mod) {
984263bc 418 /*
e7a2d403
MD
419 * Give the module a chance to veto the unload. Note that the
420 * act of unloading the module may cause other modules in the
421 * same file list to be unloaded recursively.
984263bc
MD
422 */
423 if ((error = module_unload(mod)) != 0) {
424 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
425 mod));
df4f70a6 426 lockmgr(&lock, LK_RELEASE);
b46eb239 427 file->refs--;
984263bc
MD
428 goto out;
429 }
430
e7a2d403
MD
431 /*
432 * Recursive relationships may prevent the release from
433 * immediately removing the module, or may remove other
434 * modules in the list.
435 */
436 next = module_getfnext(mod);
984263bc 437 module_release(mod);
e7a2d403
MD
438 mod = next;
439 }
440
441 /*
442 * Since we intend to destroy the file structure, we expect all
443 * modules to have been removed by now.
444 */
445 for (mod = TAILQ_FIRST(&file->modules);
446 mod;
447 mod = module_getfnext(mod)
448 ) {
6ea70f76 449 kprintf("linker_file_unload: module %p still has refs!\n", mod);
984263bc 450 }
e7a2d403 451 --file->refs;
984263bc
MD
452 }
453
454 file->refs--;
455 if (file->refs > 0) {
df4f70a6 456 lockmgr(&lock, LK_RELEASE);
984263bc
MD
457 goto out;
458 }
459
460 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
461 if (file->flags & LINKER_FILE_LINKED) {
462 linker_file_sysuninit(file);
463 linker_file_unregister_sysctls(file);
464 }
465
466 TAILQ_REMOVE(&linker_files, file, link);
df4f70a6 467 lockmgr(&lock, LK_RELEASE);
984263bc
MD
468
469 for (i = 0; i < file->ndeps; i++)
470 linker_file_unload(file->deps[i]);
efda3bd0 471 kfree(file->deps, M_LINKER);
984263bc
MD
472
473 for (cp = STAILQ_FIRST(&file->common); cp;
474 cp = STAILQ_FIRST(&file->common)) {
475 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
efda3bd0 476 kfree(cp, M_LINKER);
984263bc
MD
477 }
478
479 file->ops->unload(file);
efda3bd0 480 kfree(file, M_LINKER);
984263bc
MD
481
482out:
483 return error;
484}
485
486int
487linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
488{
489 linker_file_t* newdeps;
490
77652cad 491 newdeps = kmalloc((file->ndeps + 1) * sizeof(linker_file_t*),
e7b4468c 492 M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
493
494 if (file->deps) {
495 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
efda3bd0 496 kfree(file->deps, M_LINKER);
984263bc
MD
497 }
498 file->deps = newdeps;
499 file->deps[file->ndeps] = dep;
500 file->ndeps++;
501
502 return 0;
503}
504
dc62b251
MD
505/*
506 * Locate a linker set and its contents.
507 * This is a helper function to avoid linker_if.h exposure elsewhere.
508 * Note: firstp and lastp are really void ***
509 */
510int
511linker_file_lookup_set(linker_file_t file, const char *name,
512 void *firstp, void *lastp, int *countp)
513{
514 return file->ops->lookup_set(file, name, firstp, lastp, countp);
515}
516
d1786c1b
MD
517int
518linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
984263bc
MD
519{
520 c_linker_sym_t sym;
521 linker_symval_t symval;
522 linker_file_t lf;
984263bc
MD
523 size_t common_size = 0;
524 int i;
525
526 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
527 file, name, deps));
528
529 if (file->ops->lookup_symbol(file, name, &sym) == 0) {
530 file->ops->symbol_values(file, sym, &symval);
d1786c1b
MD
531
532 /*
533 * XXX Assume a common symbol if its value is 0 and it has a non-zero
534 * size, otherwise it could be an absolute symbol with a value of 0.
535 */
536 if (symval.value == 0 && symval.size != 0) {
984263bc
MD
537 /*
538 * For commons, first look them up in the dependancies and
539 * only allocate space if not found there.
540 */
541 common_size = symval.size;
d1786c1b 542 } else {
984263bc 543 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
d1786c1b
MD
544 *raddr = symval.value;
545 return 0;
984263bc
MD
546 }
547 }
984263bc
MD
548 if (deps) {
549 for (i = 0; i < file->ndeps; i++) {
d1786c1b
MD
550 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
551 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", *raddr));
552 return 0;
984263bc
MD
553 }
554 }
555
556 /* If we have not found it in the dependencies, search globally */
89e1aaa0 557 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
558 /* But skip the current file if it's on the list */
559 if (lf == file)
560 continue;
561 /* And skip the files we searched above */
562 for (i = 0; i < file->ndeps; i++)
563 if (lf == file->deps[i])
564 break;
565 if (i < file->ndeps)
566 continue;
d1786c1b
MD
567 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
568 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", *raddr));
569 return 0;
984263bc
MD
570 }
571 }
572 }
573
574 if (common_size > 0) {
575 /*
576 * This is a common symbol which was not found in the
577 * dependancies. We maintain a simple common symbol table in
578 * the file object.
579 */
580 struct common_symbol* cp;
581
89e1aaa0 582 STAILQ_FOREACH(cp, &file->common, link)
984263bc
MD
583 if (!strcmp(cp->name, name)) {
584 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
d1786c1b
MD
585 *raddr = cp->address;
586 return 0;
984263bc
MD
587 }
588
589 /*
590 * Round the symbol size up to align.
591 */
592 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
77652cad 593 cp = kmalloc(sizeof(struct common_symbol)
984263bc
MD
594 + common_size
595 + strlen(name) + 1,
e7b4468c 596 M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
597
598 cp->address = (caddr_t) (cp + 1);
599 cp->name = cp->address + common_size;
600 strcpy(cp->name, name);
601 bzero(cp->address, common_size);
602 STAILQ_INSERT_TAIL(&file->common, cp, link);
603
604 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
d1786c1b
MD
605 *raddr = cp->address;
606 return 0;
984263bc
MD
607 }
608
6ef6faeb
SS
609#ifdef _KERNEL_VIRTUAL
610 *raddr = dlsym(RTLD_NEXT, name);
611 if (*raddr != NULL) {
612 KLD_DPF(SYM, ("linker_file_lookup_symbol: found dlsym=%x\n", *raddr));
613 return 0;
614 }
615#endif
616
984263bc 617 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
d1786c1b 618 return ENOENT;
984263bc
MD
619}
620
621#ifdef DDB
622/*
623 * DDB Helpers. DDB has to look across multiple files with their own
624 * symbol tables and string tables.
625 *
626 * Note that we do not obey list locking protocols here. We really don't
627 * need DDB to hang because somebody's got the lock held. We'll take the
628 * chance that the files list is inconsistant instead.
629 */
630
631int
632linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
633{
634 linker_file_t lf;
635
89e1aaa0 636 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
637 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
638 return 0;
639 }
640 return ENOENT;
641}
642
643int
644linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
645{
646 linker_file_t lf;
647 u_long off = (uintptr_t)value;
648 u_long diff, bestdiff;
649 c_linker_sym_t best;
650 c_linker_sym_t es;
651
652 best = 0;
653 bestdiff = off;
89e1aaa0 654 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
655 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
656 continue;
657 if (es != 0 && diff < bestdiff) {
658 best = es;
659 bestdiff = diff;
660 }
661 if (bestdiff == 0)
662 break;
663 }
664 if (best) {
665 *sym = best;
666 *diffp = bestdiff;
667 return 0;
668 } else {
669 *sym = 0;
670 *diffp = off;
671 return ENOENT;
672 }
673}
674
675int
676linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
677{
678 linker_file_t lf;
679
89e1aaa0 680 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
681 if (lf->ops->symbol_values(lf, sym, symval) == 0)
682 return 0;
683 }
684 return ENOENT;
685}
686
687#endif
688
689/*
690 * Syscalls.
691 */
692
693int
753fd850 694sys_kldload(struct kldload_args *uap)
984263bc 695{
dadab5e9 696 struct thread *td = curthread;
984263bc
MD
697 char* filename = NULL, *modulename;
698 linker_file_t lf;
699 int error = 0;
700
c7114eea 701 uap->sysmsg_result = -1;
984263bc 702
7e42c007 703 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
984263bc
MD
704 return EPERM;
705
dadab5e9 706 if ((error = suser(td)) != 0)
984263bc
MD
707 return error;
708
efda3bd0 709 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
ab2eb4eb 710 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
984263bc
MD
711 goto out;
712
713 /* Can't load more than one module with the same name */
714 modulename = rindex(filename, '/');
715 if (modulename == NULL)
716 modulename = filename;
717 else
718 modulename++;
719 if (linker_find_file_by_name(modulename)) {
720 error = EEXIST;
721 goto out;
722 }
723
724 if ((error = linker_load_file(filename, &lf)) != 0)
725 goto out;
726
727 lf->userrefs++;
c7114eea 728 uap->sysmsg_result = lf->id;
984263bc
MD
729
730out:
731 if (filename)
efda3bd0 732 kfree(filename, M_TEMP);
984263bc
MD
733 return error;
734}
735
736int
753fd850 737sys_kldunload(struct kldunload_args *uap)
984263bc 738{
dadab5e9 739 struct thread *td = curthread;
984263bc
MD
740 linker_file_t lf;
741 int error = 0;
742
7e42c007 743 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
984263bc
MD
744 return EPERM;
745
dadab5e9 746 if ((error = suser(td)) != 0)
984263bc
MD
747 return error;
748
ab2eb4eb 749 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
750 if (lf) {
751 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
752 if (lf->userrefs == 0) {
6ea70f76 753 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
984263bc
MD
754 error = EBUSY;
755 goto out;
756 }
757 lf->userrefs--;
758 error = linker_file_unload(lf);
759 if (error)
760 lf->userrefs++;
761 } else
762 error = ENOENT;
763
764out:
765 return error;
766}
767
768int
753fd850 769sys_kldfind(struct kldfind_args *uap)
984263bc 770{
41c20dac 771 char *filename = NULL, *modulename;
984263bc
MD
772 linker_file_t lf;
773 int error = 0;
774
c7114eea 775 uap->sysmsg_result = -1;
984263bc 776
efda3bd0 777 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
ab2eb4eb 778 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
984263bc
MD
779 goto out;
780
781 modulename = rindex(filename, '/');
782 if (modulename == NULL)
783 modulename = filename;
784
785 lf = linker_find_file_by_name(modulename);
786 if (lf)
c7114eea 787 uap->sysmsg_result = lf->id;
984263bc
MD
788 else
789 error = ENOENT;
790
791out:
792 if (filename)
efda3bd0 793 kfree(filename, M_TEMP);
984263bc
MD
794 return error;
795}
796
797int
753fd850 798sys_kldnext(struct kldnext_args *uap)
984263bc
MD
799{
800 linker_file_t lf;
801 int error = 0;
802
ab2eb4eb 803 if (uap->fileid == 0) {
984263bc 804 if (TAILQ_FIRST(&linker_files))
c7114eea 805 uap->sysmsg_result = TAILQ_FIRST(&linker_files)->id;
984263bc 806 else
c7114eea 807 uap->sysmsg_result = 0;
984263bc
MD
808 return 0;
809 }
810
ab2eb4eb 811 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
812 if (lf) {
813 if (TAILQ_NEXT(lf, link))
c7114eea 814 uap->sysmsg_result = TAILQ_NEXT(lf, link)->id;
984263bc 815 else
c7114eea 816 uap->sysmsg_result = 0;
984263bc
MD
817 } else
818 error = ENOENT;
819
820 return error;
821}
822
823int
753fd850 824sys_kldstat(struct kldstat_args *uap)
984263bc
MD
825{
826 linker_file_t lf;
827 int error = 0;
828 int version;
829 struct kld_file_stat* stat;
830 int namelen;
831
ab2eb4eb 832 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
833 if (!lf) {
834 error = ENOENT;
835 goto out;
836 }
837
ab2eb4eb 838 stat = uap->stat;
984263bc
MD
839
840 /*
841 * Check the version of the user's structure.
842 */
843 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
844 goto out;
845 if (version != sizeof(struct kld_file_stat)) {
846 error = EINVAL;
847 goto out;
848 }
849
850 namelen = strlen(lf->filename) + 1;
851 if (namelen > MAXPATHLEN)
852 namelen = MAXPATHLEN;
853 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
854 goto out;
855 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
856 goto out;
857 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
858 goto out;
859 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
860 goto out;
861 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
862 goto out;
863
c7114eea 864 uap->sysmsg_result = 0;
984263bc
MD
865
866out:
867 return error;
868}
869
870int
753fd850 871sys_kldfirstmod(struct kldfirstmod_args *uap)
984263bc
MD
872{
873 linker_file_t lf;
874 int error = 0;
875
ab2eb4eb 876 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
877 if (lf) {
878 if (TAILQ_FIRST(&lf->modules))
c7114eea 879 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules));
984263bc 880 else
c7114eea 881 uap->sysmsg_result = 0;
984263bc
MD
882 } else
883 error = ENOENT;
884
885 return error;
886}
887
888int
753fd850 889sys_kldsym(struct kldsym_args *uap)
984263bc
MD
890{
891 char *symstr = NULL;
892 c_linker_sym_t sym;
893 linker_symval_t symval;
894 linker_file_t lf;
895 struct kld_sym_lookup lookup;
896 int error = 0;
897
ab2eb4eb 898 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
984263bc 899 goto out;
ab2eb4eb 900 if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP) {
984263bc
MD
901 error = EINVAL;
902 goto out;
903 }
904
efda3bd0 905 symstr = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
984263bc
MD
906 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
907 goto out;
908
ab2eb4eb
DR
909 if (uap->fileid != 0) {
910 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
911 if (lf == NULL) {
912 error = ENOENT;
913 goto out;
914 }
915 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
916 lf->ops->symbol_values(lf, sym, &symval) == 0) {
917 lookup.symvalue = (uintptr_t)symval.value;
918 lookup.symsize = symval.size;
ab2eb4eb 919 error = copyout(&lookup, uap->data, sizeof(lookup));
984263bc
MD
920 } else
921 error = ENOENT;
922 } else {
89e1aaa0 923 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
924 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
925 lf->ops->symbol_values(lf, sym, &symval) == 0) {
926 lookup.symvalue = (uintptr_t)symval.value;
927 lookup.symsize = symval.size;
ab2eb4eb 928 error = copyout(&lookup, uap->data, sizeof(lookup));
984263bc
MD
929 break;
930 }
931 }
932 if (!lf)
933 error = ENOENT;
934 }
935out:
936 if (symstr)
efda3bd0 937 kfree(symstr, M_TEMP);
984263bc
MD
938 return error;
939}
940
32832096
MD
941/*
942 * Look for module metadata in the static kernel
943 */
944struct mod_metadata *
6456e0ad
MD
945find_mod_metadata(const char *modname)
946{
947 int len;
948 struct mod_metadata **mdp;
949 struct mod_metadata *mdt;
950
951 /*
32832096
MD
952 * Strip path prefixes and any dot extension. MDT_MODULE names
953 * are just the module name without a path or ".ko".
6456e0ad
MD
954 */
955 for (len = strlen(modname) - 1; len >= 0; --len) {
956 if (modname[len] == '/')
957 break;
958 }
959 modname += len + 1;
960 for (len = 0; modname[len] && modname[len] != '.'; ++len)
961 ;
962
963 /*
964 * Look for the module declaration
965 */
966 SET_FOREACH(mdp, modmetadata_set) {
967 mdt = *mdp;
968 if (mdt->md_type != MDT_MODULE)
969 continue;
970 if (strlen(mdt->md_cval) == len &&
3374c6b7 971 strncmp(mdt->md_cval, modname, len) == 0) {
6456e0ad
MD
972 return(mdt);
973 }
974 }
975 return(NULL);
976}
977
984263bc
MD
978/*
979 * Preloaded module support
980 */
984263bc
MD
981static void
982linker_preload(void* arg)
983{
984 caddr_t modptr;
985 char *modname;
986 char *modtype;
987 linker_file_t lf;
988 linker_class_t lc;
989 int error;
984263bc
MD
990 struct sysinit **sipp;
991 const moduledata_t *moddata;
dc62b251 992 struct sysinit **si_start, **si_stop;
984263bc
MD
993
994 modptr = NULL;
995 while ((modptr = preload_search_next_name(modptr)) != NULL) {
996 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
997 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
998 if (modname == NULL) {
6ea70f76 999 kprintf("Preloaded module at %p does not have a name!\n", modptr);
984263bc
MD
1000 continue;
1001 }
1002 if (modtype == NULL) {
6ea70f76 1003 kprintf("Preloaded module at %p does not have a type!\n", modptr);
984263bc
MD
1004 continue;
1005 }
6456e0ad
MD
1006
1007 /*
1008 * This is a hack at the moment, but what's in FreeBSD-5 is even
1009 * worse so I'd rather the hack.
1010 */
6ea70f76 1011 kprintf("Preloaded %s \"%s\" at %p", modtype, modname, modptr);
6456e0ad 1012 if (find_mod_metadata(modname)) {
6ea70f76 1013 kprintf(" (ignored, already in static kernel)\n");
6456e0ad
MD
1014 continue;
1015 }
6ea70f76 1016 kprintf(".\n");
6456e0ad 1017
984263bc
MD
1018 lf = linker_find_file_by_name(modname);
1019 if (lf) {
9df06c99 1020 lf->refs++;
984263bc
MD
1021 lf->userrefs++;
1022 continue;
1023 }
1024 lf = NULL;
89e1aaa0 1025 TAILQ_FOREACH(lc, &classes, link) {
984263bc
MD
1026 error = lc->ops->load_file(modname, &lf);
1027 if (error) {
1028 lf = NULL;
1029 break;
1030 }
1031 }
1032 if (lf) {
1033 lf->userrefs++;
1034
dc62b251 1035 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0) {
984263bc
MD
1036 /* HACK ALERT!
1037 * This is to set the sysinit moduledata so that the module
1038 * can attach itself to the correct containing file.
1039 * The sysinit could be run at *any* time.
1040 */
dc62b251 1041 for (sipp = si_start; sipp < si_stop; sipp++) {
984263bc
MD
1042 if ((*sipp)->func == module_register_init) {
1043 moddata = (*sipp)->udata;
1044 error = module_register(moddata, lf);
1045 if (error)
6ea70f76 1046 kprintf("Preloaded %s \"%s\" failed to register: %d\n",
984263bc
MD
1047 modtype, modname, error);
1048 }
1049 }
dc62b251 1050 sysinit_add(si_start, si_stop);
984263bc
MD
1051 }
1052 linker_file_register_sysctls(lf);
1053 }
1054 }
1055}
1056
ba39e2e0 1057SYSINIT(preload, SI_BOOT2_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
984263bc
MD
1058
1059/*
1060 * Search for a not-loaded module by name.
1061 *
1062 * Modules may be found in the following locations:
1063 *
1064 * - preloaded (result is just the module name)
1065 * - on disk (result is full path to module)
1066 *
1067 * If the module name is qualified in any way (contains path, etc.)
1068 * the we simply return a copy of it.
1069 *
1070 * The search path can be manipulated via sysctl. Note that we use the ';'
1071 * character as a separator to be consistent with the bootloader.
1072 */
1073
555d1043 1074static char linker_path[MAXPATHLEN] = "/;/boot;/modules";
984263bc
MD
1075
1076SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1077 sizeof(linker_path), "module load search path");
446d0710 1078TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
984263bc
MD
1079
1080static char *
1081linker_strdup(const char *str)
1082{
1083 char *result;
1084
978400d3
SW
1085 result = kmalloc(strlen(str) + 1, M_LINKER, M_WAITOK);
1086 strcpy(result, str);
984263bc
MD
1087 return(result);
1088}
1089
1090char *
1091linker_search_path(const char *name)
1092{
fad57d0e 1093 struct nlookupdata nd;
984263bc 1094 char *cp, *ep, *result;
555d1043
YT
1095 size_t name_len, prefix_len;
1096 int sep;
984263bc
MD
1097 int error;
1098 enum vtype type;
1099
1100 /* qualified at all? */
1101 if (index(name, '/'))
1102 return(linker_strdup(name));
1103
1104 /* traverse the linker path */
1105 cp = linker_path;
555d1043 1106 name_len = strlen(name);
984263bc
MD
1107 for (;;) {
1108
1109 /* find the end of this component */
1110 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1111 ;
555d1043
YT
1112 prefix_len = ep - cp;
1113 /* if this component doesn't end with a slash, add one */
1114 if (ep == cp || *(ep - 1) != '/')
1115 sep = 1;
1116 else
1117 sep = 0;
1118
ad07b101
MD
1119 /*
1120 * +2 : possible separator, plus terminator.
1121 */
1122 result = kmalloc(prefix_len + name_len + 2, M_LINKER, M_WAITOK);
984263bc 1123
555d1043
YT
1124 strncpy(result, cp, prefix_len);
1125 if (sep)
1126 result[prefix_len++] = '/';
1127 strcpy(result + prefix_len, name);
984263bc
MD
1128
1129 /*
1130 * Attempt to open the file, and return the path if we succeed and it's
1131 * a regular file.
1132 */
fad57d0e
MD
1133 error = nlookup_init(&nd, result, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
1134 if (error == 0)
1135 error = vn_open(&nd, NULL, FREAD, 0);
984263bc 1136 if (error == 0) {
fad57d0e
MD
1137 type = nd.nl_open_vp->v_type;
1138 if (type == VREG) {
1139 nlookup_done(&nd);
1140 return (result);
1141 }
984263bc 1142 }
fad57d0e 1143 nlookup_done(&nd);
efda3bd0 1144 kfree(result, M_LINKER);
984263bc
MD
1145
1146 if (*ep == 0)
1147 break;
1148 cp = ep + 1;
1149 }
1150 return(NULL);
1151}