kern_linker.c revision 40159
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 *	$Id: kern_linker.c,v 1.7 1998/08/12 08:44:21 dfr Exp $
27 */
28
29#include "opt_ddb.h"
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/sysproto.h>
36#include <sys/sysent.h>
37#include <sys/proc.h>
38#include <sys/lock.h>
39#include <machine/cpu.h>
40#include <machine/bootinfo.h>
41#include <sys/module.h>
42#include <sys/linker.h>
43#include <sys/unistd.h>
44#include <sys/fcntl.h>
45#include <sys/libkern.h>
46#include <sys/namei.h>
47#include <sys/vnode.h>
48#include <sys/sysctl.h>
49
50MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
51linker_file_t linker_current_file;
52
53static struct lock lock;	/* lock for the file list */
54static linker_class_list_t classes;
55static linker_file_list_t files;
56static int next_file_id = 1;
57
58static void
59linker_init(void* arg)
60{
61    lockinit(&lock, PVM, "klink", 0, 0);
62    TAILQ_INIT(&classes);
63    TAILQ_INIT(&files);
64}
65
66SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
67
68int
69linker_add_class(const char* desc, void* priv,
70		 struct linker_class_ops* ops)
71{
72    linker_class_t lc;
73
74    lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT);
75    if (!lc)
76	return ENOMEM;
77
78    lc->desc = desc;
79    lc->priv = priv;
80    lc->ops = ops;
81    TAILQ_INSERT_HEAD(&classes, lc, link);
82
83    return 0;
84}
85
86static void
87linker_file_sysinit(linker_file_t lf)
88{
89    struct linker_set* sysinits;
90    struct sysinit** sipp;
91    struct sysinit** xipp;
92    struct sysinit* save;
93    moduledata_t *moddata;
94
95    KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
96		   lf->filename));
97
98    sysinits = (struct linker_set*)
99	linker_file_lookup_symbol(lf, "sysinit_set", 0);
100
101    KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits));
102    if (!sysinits)
103	return;
104
105    /* HACK ALERT! */
106    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
107	if ((*sipp)->func == module_register_init) {
108	    moddata = (*sipp)->udata;
109	    moddata->_file = lf;
110	}
111    }
112
113    /*
114     * Perform a bubble sort of the system initialization objects by
115     * their subsystem (primary key) and order (secondary key).
116     *
117     * Since some things care about execution order, this is the
118     * operation which ensures continued function.
119     */
120    for( sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
121	for( xipp = sipp + 1; *xipp; xipp++) {
122	    if( (*sipp)->subsystem < (*xipp)->subsystem ||
123		( (*sipp)->subsystem == (*xipp)->subsystem &&
124		  (*sipp)->order < (*xipp)->order))
125		continue;	/* skip*/
126	    save = *sipp;
127	    *sipp = *xipp;
128	    *xipp = save;
129	}
130    }
131
132
133    /*
134     * Traverse the (now) ordered list of system initialization tasks.
135     * Perform each task, and continue on to the next task.
136     *
137     * The last item on the list is expected to be the scheduler,
138     * which will not return.
139     */
140    for( sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
141	if( (*sipp)->subsystem == SI_SUB_DUMMY)
142	    continue;	/* skip dummy task(s)*/
143
144	switch( (*sipp)->type) {
145	case SI_TYPE_DEFAULT:
146	    /* no special processing*/
147	    (*((*sipp)->func))( (*sipp)->udata);
148	    break;
149
150	case SI_TYPE_KTHREAD:
151#if !defined(SMP)
152	    /* kernel thread*/
153	    if (fork1(&proc0, RFFDG|RFPROC|RFMEM))
154		panic("fork kernel thread");
155	    cpu_set_fork_handler(pfind(proc0.p_retval[0]),
156		(*sipp)->func, (*sipp)->udata);
157	    break;
158#endif
159
160	case SI_TYPE_KPROCESS:
161	    /* kernel thread*/
162	    if (fork1(&proc0, RFFDG|RFPROC))
163		panic("fork kernel process");
164	    cpu_set_fork_handler(pfind(proc0.p_retval[0]),
165		(*sipp)->func, (*sipp)->udata);
166	    break;
167
168	default:
169	    panic( "linker_file_sysinit: unrecognized init type");
170	}
171    }
172}
173
174int
175linker_load_file(const char* filename, linker_file_t* result)
176{
177    linker_class_t lc;
178    linker_file_t lf;
179    int error = 0;
180
181    lf = linker_find_file_by_name(filename);
182    if (lf) {
183	KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
184	*result = lf;
185	lf->refs++;
186	goto out;
187    }
188
189    lf = NULL;
190    for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
191	KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
192		       filename, lc->desc));
193	if (error = lc->ops->load_file(filename, &lf))
194	    goto out;
195	if (lf) {
196	    linker_file_sysinit(lf);
197
198	    *result = lf;
199	    goto out;
200	}
201    }
202    error = ENOEXEC;		/* format not recognised */
203
204out:
205    return error;
206}
207
208linker_file_t
209linker_find_file_by_name(const char* filename)
210{
211    linker_file_t lf = 0;
212
213    lockmgr(&lock, LK_SHARED, 0, curproc);
214    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link))
215	if (!strcmp(lf->filename, filename))
216	    break;
217    lockmgr(&lock, LK_RELEASE, 0, curproc);
218
219    return lf;
220}
221
222linker_file_t
223linker_find_file_by_id(int fileid)
224{
225    linker_file_t lf = 0;
226
227    lockmgr(&lock, LK_SHARED, 0, curproc);
228    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link))
229	if (lf->id == fileid)
230	    break;
231    lockmgr(&lock, LK_RELEASE, 0, curproc);
232
233    return lf;
234}
235
236linker_file_t
237linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
238{
239    linker_file_t lf = 0;
240    int namelen;
241    const char *filename;
242
243    filename = rindex(pathname, '/');
244    if (filename && filename[1])
245	filename++;
246    else
247	filename = pathname;
248
249    KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
250    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
251    namelen = strlen(filename) + 1;
252    lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
253    if (!lf)
254	goto out;
255
256    lf->refs = 1;
257    lf->userrefs = 0;
258    lf->filename = (char*) (lf + 1);
259    strcpy(lf->filename, filename);
260    lf->id = next_file_id++;
261    lf->ndeps = 0;
262    lf->deps = NULL;
263    STAILQ_INIT(&lf->common);
264    TAILQ_INIT(&lf->modules);
265
266    lf->priv = priv;
267    lf->ops = ops;
268    TAILQ_INSERT_TAIL(&files, lf, link);
269
270out:
271    lockmgr(&lock, LK_RELEASE, 0, curproc);
272    return lf;
273}
274
275int
276linker_file_unload(linker_file_t file)
277{
278    module_t mod, next;
279    struct common_symbol* cp;
280    int error = 0;
281    int i;
282
283    KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
284    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
285    if (file->refs == 1) {
286	KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
287	/*
288	 * Inform any modules associated with this file.
289	 */
290	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
291	    next = module_getfnext(mod);
292
293	    /*
294	     * Give the module a chance to veto the unload.
295	     */
296	    if (error = module_unload(mod)) {
297		KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
298			       mod));
299		lockmgr(&lock, LK_RELEASE, 0, curproc);
300		goto out;
301	    }
302
303	    module_release(mod);
304	}
305    }
306
307    file->refs--;
308    if (file->refs > 0) {
309	lockmgr(&lock, LK_RELEASE, 0, curproc);
310	goto out;
311    }
312
313    TAILQ_REMOVE(&files, file, link);
314    lockmgr(&lock, LK_RELEASE, 0, curproc);
315
316    for (i = 0; i < file->ndeps; i++)
317	linker_file_unload(file->deps[i]);
318    free(file->deps, M_LINKER);
319
320    for (cp = STAILQ_FIRST(&file->common); cp;
321	 cp = STAILQ_FIRST(&file->common)) {
322	STAILQ_REMOVE(&file->common, cp, common_symbol, link);
323	free(cp, M_LINKER);
324    }
325
326    file->ops->unload(file);
327    free(file, M_LINKER);
328
329out:
330    return error;
331}
332
333int
334linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
335{
336    linker_file_t* newdeps;
337
338    newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
339		     M_LINKER, M_WAITOK);
340    if (newdeps == NULL)
341	return ENOMEM;
342
343    if (file->deps) {
344	bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
345	free(file->deps, M_LINKER);
346    }
347    file->deps = newdeps;
348    file->deps[file->ndeps] = dep;
349    file->ndeps++;
350
351    return 0;
352}
353
354caddr_t
355linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
356{
357    linker_sym_t sym;
358    linker_symval_t symval;
359    caddr_t address;
360    size_t common_size = 0;
361    int i;
362
363    KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
364		  file, name, deps));
365
366    if (file->ops->lookup_symbol(file, name, &sym) == 0) {
367	file->ops->symbol_values(file, sym, &symval);
368	if (symval.value == 0)
369	    /*
370	     * For commons, first look them up in the dependancies and
371	     * only allocate space if not found there.
372	     */
373	    common_size = symval.size;
374	else {
375	    KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
376	    return symval.value;
377	}
378    }
379
380    if (deps)
381	for (i = 0; i < file->ndeps; i++) {
382	    address = linker_file_lookup_symbol(file->deps[i], name, 0);
383	    if (address) {
384		KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
385		return address;
386	    }
387	}
388
389    if (common_size > 0) {
390	/*
391	 * This is a common symbol which was not found in the
392	 * dependancies.  We maintain a simple common symbol table in
393	 * the file object.
394	 */
395	struct common_symbol* cp;
396
397	for (cp = STAILQ_FIRST(&file->common); cp;
398	     cp = STAILQ_NEXT(cp, link))
399	    if (!strcmp(cp->name, name)) {
400		KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
401		return cp->address;
402	    }
403
404	/*
405	 * Round the symbol size up to align.
406	 */
407	common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
408	cp = malloc(sizeof(struct common_symbol)
409		    + common_size
410		    + strlen(name) + 1,
411		    M_LINKER, M_WAITOK);
412	if (!cp) {
413	    KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
414	    return 0;
415	}
416
417	cp->address = (caddr_t) (cp + 1);
418	cp->name = cp->address + common_size;
419	strcpy(cp->name, name);
420	bzero(cp->address, common_size);
421	STAILQ_INSERT_TAIL(&file->common, cp, link);
422
423	KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
424	return cp->address;
425    }
426
427    KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
428    return 0;
429}
430
431#ifdef DDB
432/*
433 * DDB Helpers.  DDB has to look across multiple files with their own
434 * symbol tables and string tables.
435 *
436 * Note that we do not obey list locking protocols here.  We really don't
437 * need DDB to hang because somebody's got the lock held.  We'll take the
438 * chance that the files list is inconsistant instead.
439 */
440
441int
442linker_ddb_lookup(char *symstr, linker_sym_t *sym)
443{
444    linker_file_t lf;
445
446    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
447	if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
448	    return 0;
449    }
450    return ENOENT;
451}
452
453int
454linker_ddb_search_symbol(caddr_t value, linker_sym_t *sym, long *diffp)
455{
456    linker_file_t lf;
457    u_long off = (u_long)value;
458    u_long diff, bestdiff;
459    linker_sym_t best;
460    linker_sym_t es;
461
462    best = 0;
463    bestdiff = off;
464    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
465	if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
466	    continue;
467	if (es != 0 && diff < bestdiff) {
468	    best = es;
469	    bestdiff = diff;
470	}
471	if (bestdiff == 0)
472	    break;
473    }
474    if (best) {
475	*sym = best;
476	*diffp = bestdiff;
477	return 0;
478    } else {
479	*sym = 0;
480	*diffp = off;
481	return ENOENT;
482    }
483}
484
485int
486linker_ddb_symbol_values(linker_sym_t sym, linker_symval_t *symval)
487{
488    linker_file_t lf;
489
490    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
491	if (lf->ops->symbol_values(lf, sym, symval) == 0)
492	    return 0;
493    }
494    return ENOENT;
495}
496
497#endif
498
499/*
500 * Syscalls.
501 */
502
503int
504kldload(struct proc* p, struct kldload_args* uap)
505{
506    char* filename = NULL;
507    linker_file_t lf;
508    int error = 0;
509
510    p->p_retval[0] = -1;
511
512    if (securelevel > 0)
513	return EPERM;
514
515    if (error = suser(p->p_ucred, &p->p_acflag))
516	return error;
517
518    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
519    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
520	goto out;
521
522    if (error = linker_load_file(uap->file, &lf))
523	goto out;
524
525    lf->userrefs++;
526    p->p_retval[0] = lf->id;
527
528out:
529    if (filename)
530	free(filename, M_TEMP);
531    return error;
532}
533
534int
535kldunload(struct proc* p, struct kldunload_args* uap)
536{
537    linker_file_t lf;
538    int error = 0;
539
540    if (securelevel > 0)
541	return EPERM;
542
543    if (error = suser(p->p_ucred, &p->p_acflag))
544	return error;
545
546    lf = linker_find_file_by_id(SCARG(uap, fileid));
547    if (lf) {
548	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
549	if (lf->userrefs == 0) {
550	    printf("linkerunload: attempt to unload file which was not loaded by user\n");
551	    error = EBUSY;
552	    goto out;
553	}
554	lf->userrefs--;
555	error = linker_file_unload(lf);
556    } else
557	error = ENOENT;
558
559out:
560    return error;
561}
562
563int
564kldfind(struct proc* p, struct kldfind_args* uap)
565{
566    char* filename = NULL;
567    linker_file_t lf;
568    int error = 0;
569
570    p->p_retval[0] = -1;
571
572    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
573    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
574	goto out;
575
576    lf = linker_find_file_by_name(filename);
577    if (lf)
578	p->p_retval[0] = lf->id;
579    else
580	error = ENOENT;
581
582out:
583    if (filename)
584	free(filename, M_TEMP);
585    return error;
586}
587
588int
589kldnext(struct proc* p, struct kldnext_args* uap)
590{
591    linker_file_t lf;
592    int error = 0;
593
594    if (SCARG(uap, fileid) == 0) {
595	if (TAILQ_FIRST(&files))
596	    p->p_retval[0] = TAILQ_FIRST(&files)->id;
597	else
598	    p->p_retval[0] = 0;
599	return 0;
600    }
601
602    lf = linker_find_file_by_id(SCARG(uap, fileid));
603    if (lf) {
604	if (TAILQ_NEXT(lf, link))
605	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
606	else
607	    p->p_retval[0] = 0;
608    } else
609	error = ENOENT;
610
611    return error;
612}
613
614int
615kldstat(struct proc* p, struct kldstat_args* uap)
616{
617    linker_file_t lf;
618    int error = 0;
619    int version;
620    struct kld_file_stat* stat;
621    int namelen;
622
623    lf = linker_find_file_by_id(SCARG(uap, fileid));
624    if (!lf) {
625	error = ENOENT;
626	goto out;
627    }
628
629    stat = SCARG(uap, stat);
630
631    /*
632     * Check the version of the user's structure.
633     */
634    if (error = copyin(&stat->version, &version, sizeof(version)))
635	goto out;
636    if (version != sizeof(struct kld_file_stat)) {
637	error = EINVAL;
638	goto out;
639    }
640
641    namelen = strlen(lf->filename) + 1;
642    if (namelen > MAXPATHLEN)
643	namelen = MAXPATHLEN;
644    if (error = copyout(lf->filename, &stat->name[0], namelen))
645	goto out;
646    if (error = copyout(&lf->refs, &stat->refs, sizeof(int)))
647	goto out;
648    if (error = copyout(&lf->id, &stat->id, sizeof(int)))
649	goto out;
650    if (error = copyout(&lf->address, &stat->address, sizeof(caddr_t)))
651	goto out;
652    if (error = copyout(&lf->size, &stat->size, sizeof(size_t)))
653	goto out;
654
655    p->p_retval[0] = 0;
656
657out:
658    return error;
659}
660
661int
662kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
663{
664    linker_file_t lf;
665    int error = 0;
666
667    lf = linker_find_file_by_id(SCARG(uap, fileid));
668    if (lf) {
669	if (TAILQ_FIRST(&lf->modules))
670	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
671	else
672	    p->p_retval[0] = 0;
673    } else
674	error = ENOENT;
675
676    return error;
677}
678
679/*
680 * Preloaded module support
681 */
682
683static void
684linker_preload(void* arg)
685{
686    caddr_t		modptr;
687    char		*modname;
688    linker_file_t	lf;
689    linker_class_t	lc;
690    int			error;
691    struct linker_set	*sysinits;
692    struct sysinit	**sipp;
693    moduledata_t	*moddata;
694
695
696    modptr = NULL;
697    while ((modptr = preload_search_next_name(modptr)) != NULL) {
698	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
699	if (modname == NULL) {
700	    printf("Preloaded module at 0x%p does not have a name!\n", modptr);
701	    continue;
702	}
703	printf("Preloaded module %s at 0x%p.\n", modname, modptr);
704	lf = linker_find_file_by_name(modname);
705	if (lf) {
706	    lf->userrefs++;
707	    continue;
708	}
709	lf = NULL;
710	for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
711	    error = lc->ops->load_file(modname, &lf);
712	    if (error) {
713		lf = NULL;
714		break;
715	    }
716	}
717	if (lf) {
718	    lf->userrefs++;
719
720	    sysinits = (struct linker_set*)
721		linker_file_lookup_symbol(lf, "sysinit_set", 0);
722	    if (sysinits) {
723		/* HACK ALERT!
724		 * This is to set the sysinit moduledata so that the module
725		 * can attach itself to the correct containing file.
726		 * The sysinit could be run at *any* time.
727		 */
728		for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
729		    if ((*sipp)->func == module_register_init) {
730			moddata = (*sipp)->udata;
731			moddata->_file = lf;
732		    }
733		}
734		sysinit_add((struct sysinit **)sysinits->ls_items);
735	    }
736
737	    break;
738	}
739    }
740}
741
742SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
743
744/*
745 * Search for a not-loaded module by name.
746 *
747 * Modules may be found in the following locations:
748 *
749 * - preloaded (result is just the module name)
750 * - on disk (result is full path to module)
751 *
752 * If the module name is qualified in any way (contains path, etc.)
753 * the we simply return a copy of it.
754 *
755 * The search path can be manipulated via sysctl.  Note that we use the ';'
756 * character as a separator to be consistent with the bootloader.
757 */
758
759static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/";
760
761SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
762	      sizeof(linker_path), "module load search path");
763
764static char *
765linker_strdup(const char *str)
766{
767    char	*result;
768
769    if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
770	strcpy(result, str);
771    return(result);
772}
773
774char *
775linker_search_path(const char *name)
776{
777    struct nameidata	nd;
778    struct proc		*p = curproc;	/* XXX */
779    char		*cp, *ep, *result;
780    int			error;
781    enum vtype		type;
782
783    /* qualified at all? */
784    if (index(name, '/'))
785	return(linker_strdup(name));
786
787    /* traverse the linker path */
788    cp = linker_path;
789    for (;;) {
790
791	/* find the end of this component */
792	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
793	    ;
794	result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
795	if (result == NULL)	/* actually ENOMEM */
796	    return(NULL);
797
798	strncpy(result, cp, ep - cp);
799	strcpy(result + (ep - cp), name);
800
801	/*
802	 * Attempt to open the file, and return the path if we succeed and it's
803	 * a regular file.
804	 */
805	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
806	error = vn_open(&nd, FREAD, 0);
807	if (error == 0) {
808	    type = nd.ni_vp->v_type;
809	    VOP_UNLOCK(nd.ni_vp, 0, p);
810	    vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
811	    if (type == VREG)
812		return(result);
813	}
814	free(result, M_LINKER);
815
816	if (*ep == 0)
817	    break;
818	cp = ep + 1;
819    }
820    return(NULL);
821}
822