2 * Copyright (c) 1997 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/kern/kern_linker.c,v 1.41.2.3 2001/11/21 17:50:35 luigi Exp $
27 * $DragonFly: src/sys/kern/kern_linker.c,v 1.13 2003/11/10 06:12:13 dillon Exp $
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>
40 #include <sys/module.h>
41 #include <sys/linker.h>
42 #include <sys/fcntl.h>
43 #include <sys/libkern.h>
44 #include <sys/namei.h>
45 #include <sys/vnode.h>
46 #include <sys/sysctl.h>
48 #include <vm/vm_zone.h>
54 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
55 linker_file_t linker_current_file;
56 linker_file_t linker_kernel_file;
58 static struct lock lock; /* lock for the file list */
59 static linker_class_list_t classes;
60 static linker_file_list_t linker_files;
61 static int next_file_id = 1;
64 linker_init(void* arg)
66 lockinit(&lock, 0, "klink", 0, 0);
68 TAILQ_INIT(&linker_files);
71 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
74 linker_add_class(const char* desc, void* priv,
75 struct linker_class_ops* ops)
79 lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT);
82 bzero(lc, sizeof(*lc));
87 TAILQ_INSERT_HEAD(&classes, lc, link);
93 linker_file_sysinit(linker_file_t lf)
95 struct sysinit** start, ** stop;
96 struct sysinit** sipp;
97 struct sysinit** xipp;
99 const moduledata_t *moddata;
102 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
105 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
106 return 0; /* XXX is this correct ? No sysinit ? */
109 for (sipp = start; sipp < stop; sipp++) {
110 if ((*sipp)->func == module_register_init) {
111 moddata = (*sipp)->udata;
112 error = module_register(moddata, lf);
114 printf("linker_file_sysinit \"%s\" failed to register! %d\n",
115 lf->filename, error);
122 * Perform a bubble sort of the system initialization objects by
123 * their subsystem (primary key) and order (secondary key).
125 * Since some things care about execution order, this is the
126 * operation which ensures continued function.
128 for (sipp = start; sipp < stop; sipp++) {
129 for (xipp = sipp + 1; xipp < stop; xipp++) {
130 if ((*sipp)->subsystem < (*xipp)->subsystem ||
131 ((*sipp)->subsystem == (*xipp)->subsystem &&
132 (*sipp)->order <= (*xipp)->order))
142 * Traverse the (now) ordered list of system initialization tasks.
143 * Perform each task, and continue on to the next task.
145 for (sipp = start; sipp < stop; sipp++) {
146 if ((*sipp)->subsystem == SI_SUB_DUMMY)
147 continue; /* skip dummy task(s)*/
150 (*((*sipp)->func))((*sipp)->udata);
152 return 0; /* no errors */
156 linker_file_sysuninit(linker_file_t lf)
158 struct sysinit** start, ** stop;
159 struct sysinit** sipp;
160 struct sysinit** xipp;
161 struct sysinit* save;
163 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
166 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
170 * Perform a reverse bubble sort of the system initialization objects
171 * by their subsystem (primary key) and order (secondary key).
173 * Since some things care about execution order, this is the
174 * operation which ensures continued function.
176 for (sipp = start; sipp < stop; sipp++) {
177 for (xipp = sipp + 1; xipp < stop; xipp++) {
178 if ((*sipp)->subsystem > (*xipp)->subsystem ||
179 ((*sipp)->subsystem == (*xipp)->subsystem &&
180 (*sipp)->order >= (*xipp)->order))
190 * Traverse the (now) ordered list of system initialization tasks.
191 * Perform each task, and continue on to the next task.
193 for (sipp = start; sipp < stop; sipp++) {
194 if ((*sipp)->subsystem == SI_SUB_DUMMY)
195 continue; /* skip dummy task(s)*/
198 (*((*sipp)->func))((*sipp)->udata);
203 linker_file_register_sysctls(linker_file_t lf)
205 struct sysctl_oid **start, **stop, **oidp;
207 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
210 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
212 for (oidp = start; oidp < stop; oidp++)
213 sysctl_register_oid(*oidp);
217 linker_file_unregister_sysctls(linker_file_t lf)
219 struct sysctl_oid **start, **stop, **oidp;
221 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
224 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
226 for (oidp = start; oidp < stop; oidp++)
227 sysctl_unregister_oid(*oidp);
231 linker_load_file(const char* filename, linker_file_t* result)
235 int foundfile, error = 0;
238 /* Refuse to load modules if securelevel raised */
242 lf = linker_find_file_by_name(filename);
244 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
250 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
251 if (koname == NULL) {
255 sprintf(koname, "%s.ko", filename);
258 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
259 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
260 filename, lc->desc));
262 error = lc->ops->load_file(koname, &lf); /* First with .ko */
263 if (lf == NULL && error == ENOENT)
264 error = lc->ops->load_file(filename, &lf); /* Then try without */
266 * If we got something other than ENOENT, then it exists but we cannot
267 * load it for some other reason.
272 linker_file_register_sysctls(lf);
273 error = linker_file_sysinit(lf);
280 * Less than ideal, but tells the user whether it failed to load or
281 * the module was not found.
285 * Format not recognized or otherwise unloadable.
286 * When loading a module that is statically built into
287 * the kernel EEXIST percolates back up as the return
288 * value. Preserve this so that apps like sysinstall
289 * can recognize this special case and not post bogus
295 error = ENOENT; /* Nothing found */
299 free(koname, M_LINKER);
304 linker_find_file_by_name(const char* filename)
306 linker_file_t lf = 0;
309 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
312 sprintf(koname, "%s.ko", filename);
314 lockmgr(&lock, LK_SHARED, 0, curthread);
315 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
316 if (!strcmp(lf->filename, koname))
318 if (!strcmp(lf->filename, filename))
321 lockmgr(&lock, LK_RELEASE, 0, curthread);
325 free(koname, M_LINKER);
330 linker_find_file_by_id(int fileid)
332 linker_file_t lf = 0;
334 lockmgr(&lock, LK_SHARED, 0, curthread);
335 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link))
336 if (lf->id == fileid)
338 lockmgr(&lock, LK_RELEASE, 0, curthread);
344 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
346 linker_file_t lf = 0;
348 const char *filename;
350 filename = rindex(pathname, '/');
351 if (filename && filename[1])
356 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
357 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
358 namelen = strlen(filename) + 1;
359 lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
362 bzero(lf, sizeof(*lf));
367 lf->filename = (char*) (lf + 1);
368 strcpy(lf->filename, filename);
369 lf->id = next_file_id++;
372 STAILQ_INIT(&lf->common);
373 TAILQ_INIT(&lf->modules);
377 TAILQ_INSERT_TAIL(&linker_files, lf, link);
380 lockmgr(&lock, LK_RELEASE, 0, curthread);
385 linker_file_unload(linker_file_t file)
388 struct common_symbol* cp;
392 /* Refuse to unload modules if securelevel raised */
396 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
397 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
398 if (file->refs == 1) {
399 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
401 * Inform any modules associated with this file.
403 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
404 next = module_getfnext(mod);
407 * Give the module a chance to veto the unload.
409 if ((error = module_unload(mod)) != 0) {
410 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
412 lockmgr(&lock, LK_RELEASE, 0, curthread);
421 if (file->refs > 0) {
422 lockmgr(&lock, LK_RELEASE, 0, curthread);
426 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
427 if (file->flags & LINKER_FILE_LINKED) {
428 linker_file_sysuninit(file);
429 linker_file_unregister_sysctls(file);
432 TAILQ_REMOVE(&linker_files, file, link);
433 lockmgr(&lock, LK_RELEASE, 0, curthread);
435 for (i = 0; i < file->ndeps; i++)
436 linker_file_unload(file->deps[i]);
437 free(file->deps, M_LINKER);
439 for (cp = STAILQ_FIRST(&file->common); cp;
440 cp = STAILQ_FIRST(&file->common)) {
441 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
445 file->ops->unload(file);
446 free(file, M_LINKER);
453 linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
455 linker_file_t* newdeps;
457 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
461 bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
464 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
465 free(file->deps, M_LINKER);
467 file->deps = newdeps;
468 file->deps[file->ndeps] = dep;
475 * Locate a linker set and its contents.
476 * This is a helper function to avoid linker_if.h exposure elsewhere.
477 * Note: firstp and lastp are really void ***
480 linker_file_lookup_set(linker_file_t file, const char *name,
481 void *firstp, void *lastp, int *countp)
483 return file->ops->lookup_set(file, name, firstp, lastp, countp);
487 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
490 linker_symval_t symval;
492 size_t common_size = 0;
495 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
498 if (file->ops->lookup_symbol(file, name, &sym) == 0) {
499 file->ops->symbol_values(file, sym, &symval);
502 * XXX Assume a common symbol if its value is 0 and it has a non-zero
503 * size, otherwise it could be an absolute symbol with a value of 0.
505 if (symval.value == 0 && symval.size != 0) {
507 * For commons, first look them up in the dependancies and
508 * only allocate space if not found there.
510 common_size = symval.size;
512 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
513 *raddr = symval.value;
518 for (i = 0; i < file->ndeps; i++) {
519 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
520 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", *raddr));
525 /* If we have not found it in the dependencies, search globally */
526 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
527 /* But skip the current file if it's on the list */
530 /* And skip the files we searched above */
531 for (i = 0; i < file->ndeps; i++)
532 if (lf == file->deps[i])
536 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
537 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", *raddr));
543 if (common_size > 0) {
545 * This is a common symbol which was not found in the
546 * dependancies. We maintain a simple common symbol table in
549 struct common_symbol* cp;
551 for (cp = STAILQ_FIRST(&file->common); cp;
552 cp = STAILQ_NEXT(cp, link))
553 if (!strcmp(cp->name, name)) {
554 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
555 *raddr = cp->address;
560 * Round the symbol size up to align.
562 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
563 cp = malloc(sizeof(struct common_symbol)
568 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
571 bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
573 cp->address = (caddr_t) (cp + 1);
574 cp->name = cp->address + common_size;
575 strcpy(cp->name, name);
576 bzero(cp->address, common_size);
577 STAILQ_INSERT_TAIL(&file->common, cp, link);
579 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
580 *raddr = cp->address;
584 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
590 * DDB Helpers. DDB has to look across multiple files with their own
591 * symbol tables and string tables.
593 * Note that we do not obey list locking protocols here. We really don't
594 * need DDB to hang because somebody's got the lock held. We'll take the
595 * chance that the files list is inconsistant instead.
599 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
603 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
604 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
611 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
614 u_long off = (uintptr_t)value;
615 u_long diff, bestdiff;
621 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
622 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
624 if (es != 0 && diff < bestdiff) {
643 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
647 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
648 if (lf->ops->symbol_values(lf, sym, symval) == 0)
661 kldload(struct kldload_args *uap)
663 struct thread *td = curthread;
664 char* filename = NULL, *modulename;
668 uap->sysmsg_result = -1;
670 if (securelevel > 0) /* redundant, but that's OK */
673 if ((error = suser(td)) != 0)
676 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
677 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
680 /* Can't load more than one module with the same name */
681 modulename = rindex(filename, '/');
682 if (modulename == NULL)
683 modulename = filename;
686 if (linker_find_file_by_name(modulename)) {
691 if ((error = linker_load_file(filename, &lf)) != 0)
695 uap->sysmsg_result = lf->id;
699 free(filename, M_TEMP);
704 kldunload(struct kldunload_args *uap)
706 struct thread *td = curthread;
710 if (securelevel > 0) /* redundant, but that's OK */
713 if ((error = suser(td)) != 0)
716 lf = linker_find_file_by_id(SCARG(uap, fileid));
718 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
719 if (lf->userrefs == 0) {
720 printf("linkerunload: attempt to unload file that was loaded by the kernel\n");
725 error = linker_file_unload(lf);
736 kldfind(struct kldfind_args *uap)
738 char *filename = NULL, *modulename;
742 uap->sysmsg_result = -1;
744 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
745 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
748 modulename = rindex(filename, '/');
749 if (modulename == NULL)
750 modulename = filename;
752 lf = linker_find_file_by_name(modulename);
754 uap->sysmsg_result = lf->id;
760 free(filename, M_TEMP);
765 kldnext(struct kldnext_args *uap)
770 if (SCARG(uap, fileid) == 0) {
771 if (TAILQ_FIRST(&linker_files))
772 uap->sysmsg_result = TAILQ_FIRST(&linker_files)->id;
774 uap->sysmsg_result = 0;
778 lf = linker_find_file_by_id(SCARG(uap, fileid));
780 if (TAILQ_NEXT(lf, link))
781 uap->sysmsg_result = TAILQ_NEXT(lf, link)->id;
783 uap->sysmsg_result = 0;
791 kldstat(struct kldstat_args *uap)
796 struct kld_file_stat* stat;
799 lf = linker_find_file_by_id(SCARG(uap, fileid));
805 stat = SCARG(uap, stat);
808 * Check the version of the user's structure.
810 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
812 if (version != sizeof(struct kld_file_stat)) {
817 namelen = strlen(lf->filename) + 1;
818 if (namelen > MAXPATHLEN)
819 namelen = MAXPATHLEN;
820 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
822 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
824 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
826 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
828 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
831 uap->sysmsg_result = 0;
838 kldfirstmod(struct kldfirstmod_args *uap)
843 lf = linker_find_file_by_id(SCARG(uap, fileid));
845 if (TAILQ_FIRST(&lf->modules))
846 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules));
848 uap->sysmsg_result = 0;
856 kldsym(struct kldsym_args *uap)
860 linker_symval_t symval;
862 struct kld_sym_lookup lookup;
865 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
867 if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
872 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
873 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
876 if (SCARG(uap, fileid) != 0) {
877 lf = linker_find_file_by_id(SCARG(uap, fileid));
882 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
883 lf->ops->symbol_values(lf, sym, &symval) == 0) {
884 lookup.symvalue = (uintptr_t)symval.value;
885 lookup.symsize = symval.size;
886 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
890 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
891 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
892 lf->ops->symbol_values(lf, sym, &symval) == 0) {
893 lookup.symvalue = (uintptr_t)symval.value;
894 lookup.symsize = symval.size;
895 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
904 free(symstr, M_TEMP);
909 * Preloaded module support
913 linker_preload(void* arg)
921 struct sysinit **sipp;
922 const moduledata_t *moddata;
923 struct sysinit **si_start, **si_stop;
926 while ((modptr = preload_search_next_name(modptr)) != NULL) {
927 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
928 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
929 if (modname == NULL) {
930 printf("Preloaded module at %p does not have a name!\n", modptr);
933 if (modtype == NULL) {
934 printf("Preloaded module at %p does not have a type!\n", modptr);
937 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
938 lf = linker_find_file_by_name(modname);
944 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
945 error = lc->ops->load_file(modname, &lf);
954 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0) {
956 * This is to set the sysinit moduledata so that the module
957 * can attach itself to the correct containing file.
958 * The sysinit could be run at *any* time.
960 for (sipp = si_start; sipp < si_stop; sipp++) {
961 if ((*sipp)->func == module_register_init) {
962 moddata = (*sipp)->udata;
963 error = module_register(moddata, lf);
965 printf("Preloaded %s \"%s\" failed to register: %d\n",
966 modtype, modname, error);
969 sysinit_add(si_start, si_stop);
971 linker_file_register_sysctls(lf);
976 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
979 * Search for a not-loaded module by name.
981 * Modules may be found in the following locations:
983 * - preloaded (result is just the module name)
984 * - on disk (result is full path to module)
986 * If the module name is qualified in any way (contains path, etc.)
987 * the we simply return a copy of it.
989 * The search path can be manipulated via sysctl. Note that we use the ';'
990 * character as a separator to be consistent with the bootloader.
993 static char linker_path[MAXPATHLEN] = "/;/boot/;/modules/";
995 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
996 sizeof(linker_path), "module load search path");
999 linker_strdup(const char *str)
1003 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
1004 strcpy(result, str);
1009 linker_search_path(const char *name)
1011 struct nameidata nd;
1012 struct thread *td = curthread;
1013 char *cp, *ep, *result;
1017 /* qualified at all? */
1018 if (index(name, '/'))
1019 return(linker_strdup(name));
1021 /* traverse the linker path */
1025 /* find the end of this component */
1026 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1028 result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
1029 if (result == NULL) /* actually ENOMEM */
1032 strncpy(result, cp, ep - cp);
1033 strcpy(result + (ep - cp), name);
1036 * Attempt to open the file, and return the path if we succeed and it's
1039 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, result, td);
1040 error = vn_open(&nd, FREAD, 0);
1042 NDFREE(&nd, NDF_ONLY_PNBUF);
1043 type = nd.ni_vp->v_type;
1044 VOP_UNLOCK(nd.ni_vp, 0, td);
1045 vn_close(nd.ni_vp, FREAD, td);
1049 free(result, M_LINKER);