kern_linker.c revision 74639
160814Sps/*- 260786Sps * Copyright (c) 1997-2000 Doug Rabson 3221715Sdelphij * All rights reserved. 460786Sps * 560786Sps * Redistribution and use in source and binary forms, with or without 660786Sps * modification, are permitted provided that the following conditions 760786Sps * are met: 860786Sps * 1. Redistributions of source code must retain the above copyright 960786Sps * notice, this list of conditions and the following disclaimer. 1060786Sps * 2. Redistributions in binary form must reproduce the above copyright 1160786Sps * notice, this list of conditions and the following disclaimer in the 1260786Sps * documentation and/or other materials provided with the distribution. 1360786Sps * 1460786Sps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1560786Sps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1660786Sps * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1760786Sps * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1860786Sps * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1960786Sps * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2060786Sps * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2160786Sps * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2260786Sps * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2360786Sps * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2460786Sps * SUCH DAMAGE. 2560786Sps * 2660786Sps * $FreeBSD: head/sys/kern/kern_linker.c 74639 2001-03-22 07:14:42Z bp $ 2760786Sps */ 2860786Sps 2960786Sps#include "opt_ddb.h" 3060786Sps 3160786Sps#include <sys/param.h> 3260786Sps#include <sys/kernel.h> 33170259Sdelphij#include <sys/systm.h> 3460786Sps#include <sys/malloc.h> 3560786Sps#include <sys/sysproto.h> 3660786Sps#include <sys/sysent.h> 3760786Sps#include <sys/proc.h> 3860786Sps#include <sys/lock.h> 3960786Sps#include <sys/module.h> 4060786Sps#include <sys/linker.h> 4160786Sps#include <sys/fcntl.h> 4260786Sps#include <sys/libkern.h> 4360786Sps#include <sys/namei.h> 4460786Sps#include <sys/vnode.h> 4589022Sps#include <sys/sysctl.h> 4660786Sps 4789022Sps 4860786Sps#include "linker_if.h" 4989022Sps 5060786Sps#ifdef KLD_DEBUG 5189022Spsint kld_debug = 0; 5260786Sps#endif 5360786Sps 5489022Spsstatic char *linker_search_path(const char *name); 5589022Spsstatic const char *linker_basename(const char* path); 56170259Sdelphij 57170259SdelphijMALLOC_DEFINE(M_LINKER, "linker", "kernel linker"); 5860786Sps 5960786Spslinker_file_t linker_kernel_file; 6060786Sps 6160786Spsstatic struct lock lock; /* lock for the file list */ 6289022Spsstatic linker_class_list_t classes; 6360786Spsstatic linker_file_list_t linker_files; 6460786Spsstatic int next_file_id = 1; 6560786Sps 6660786Sps/* XXX wrong name; we're looking at version provision tags here, not modules */ 6760786Spstypedef TAILQ_HEAD(, modlist) modlisthead_t; 6860786Spsstruct modlist { 6960786Sps TAILQ_ENTRY(modlist) link; /* chain together all modules */ 7060786Sps linker_file_t container; 7160786Sps const char *name; 7260786Sps}; 7360786Spstypedef struct modlist *modlist_t; 74170259Sdelphijstatic modlisthead_t found_modules; 7560786Sps 7660786Spsstatic char * 7760786Spslinker_strdup(const char *str) 7889022Sps{ 7960786Sps char *result; 8060786Sps 8160786Sps if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL) 8260786Sps strcpy(result, str); 8360786Sps return(result); 8460786Sps} 8560786Sps 8660786Spsstatic void 8760786Spslinker_init(void* arg) 8860786Sps{ 8960786Sps lockinit(&lock, PVM, "klink", 0, 0); 9060786Sps TAILQ_INIT(&classes); 9160786Sps TAILQ_INIT(&linker_files); 9260786Sps} 9360786Sps 9460786SpsSYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0); 9560786Sps 9660786Spsint 9760786Spslinker_add_class(linker_class_t lc) 9860786Sps{ 9960786Sps kobj_class_compile((kobj_class_t) lc); 10060786Sps TAILQ_INSERT_TAIL(&classes, lc, link); 10160786Sps return 0; 10260786Sps} 10360786Sps 10460786Spsstatic void 10560786Spslinker_file_sysinit(linker_file_t lf) 10660786Sps{ 10760786Sps struct linker_set* sysinits; 10860786Sps struct sysinit** sipp; 10960786Sps struct sysinit** xipp; 11060786Sps struct sysinit* save; 11160786Sps 11260786Sps KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 11360786Sps lf->filename)); 11460786Sps 11560786Sps sysinits = (struct linker_set*) 11660786Sps linker_file_lookup_symbol(lf, "sysinit_set", 0); 11760786Sps 11860786Sps KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits)); 119128348Stjr if (!sysinits) 120128348Stjr return; 121128348Stjr /* 122128348Stjr * Perform a bubble sort of the system initialization objects by 12360786Sps * their subsystem (primary key) and order (secondary key). 12460786Sps * 12560786Sps * Since some things care about execution order, this is the 126128348Stjr * operation which ensures continued function. 127128348Stjr */ 128128348Stjr for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 129128348Stjr for (xipp = sipp + 1; *xipp; xipp++) { 130128348Stjr if ((*sipp)->subsystem < (*xipp)->subsystem || 131128348Stjr ((*sipp)->subsystem == (*xipp)->subsystem && 132128348Stjr (*sipp)->order <= (*xipp)->order)) 133128348Stjr continue; /* skip*/ 134128348Stjr save = *sipp; 135128348Stjr *sipp = *xipp; 136128348Stjr *xipp = save; 137128348Stjr } 138128348Stjr } 13960786Sps 14060786Sps 14160786Sps /* 142128348Stjr * Traverse the (now) ordered list of system initialization tasks. 143128348Stjr * Perform each task, and continue on to the next task. 14460786Sps */ 145128348Stjr for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 14660786Sps if ((*sipp)->subsystem == SI_SUB_DUMMY) 147128348Stjr continue; /* skip dummy task(s)*/ 14860786Sps 14960786Sps /* Call function */ 15060786Sps (*((*sipp)->func))((*sipp)->udata); 15160786Sps } 15260786Sps} 15360786Sps 15460786Spsstatic void 15560786Spslinker_file_sysuninit(linker_file_t lf) 15660786Sps{ 15760786Sps struct linker_set* sysuninits; 15860786Sps struct sysinit** sipp; 15960786Sps struct sysinit** xipp; 16060786Sps struct sysinit* save; 16160786Sps 16260786Sps KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 16360786Sps lf->filename)); 16460786Sps 16560786Sps sysuninits = (struct linker_set*) 16660786Sps linker_file_lookup_symbol(lf, "sysuninit_set", 0); 16760786Sps 16860786Sps KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits)); 16960786Sps if (!sysuninits) 170161478Sdelphij return; 17160786Sps 17260786Sps /* 17360786Sps * Perform a reverse bubble sort of the system initialization objects 17460786Sps * by their subsystem (primary key) and order (secondary key). 17560786Sps * 17660786Sps * Since some things care about execution order, this is the 17760786Sps * operation which ensures continued function. 17860786Sps */ 17960786Sps for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { 18060786Sps for (xipp = sipp + 1; *xipp; xipp++) { 18160786Sps if ((*sipp)->subsystem > (*xipp)->subsystem || 18260786Sps ((*sipp)->subsystem == (*xipp)->subsystem && 18360786Sps (*sipp)->order >= (*xipp)->order)) 18460786Sps continue; /* skip*/ 18560786Sps save = *sipp; 18660786Sps *sipp = *xipp; 18760786Sps *xipp = save; 18860786Sps } 18960786Sps } 19060786Sps 19160786Sps /* 19260786Sps * Traverse the (now) ordered list of system initialization tasks. 19360786Sps * Perform each task, and continue on to the next task. 19460786Sps */ 19560786Sps for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { 19660786Sps if ((*sipp)->subsystem == SI_SUB_DUMMY) 19760786Sps continue; /* skip dummy task(s)*/ 19860786Sps 199191930Sdelphij /* Call function */ 20060786Sps (*((*sipp)->func))((*sipp)->udata); 20160786Sps } 20260786Sps} 20360786Sps 20460786Spsstatic void 20560786Spslinker_file_register_sysctls(linker_file_t lf) 206161478Sdelphij{ 20760786Sps struct linker_set* sysctls; 20860786Sps 209128348Stjr KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 21089022Sps lf->filename)); 211128348Stjr 212128348Stjr sysctls = (struct linker_set*) 213128348Stjr linker_file_lookup_symbol(lf, "sysctl_set", 0); 21460786Sps 215128348Stjr KLD_DPF(FILE, ("linker_file_register_sysctls: SYSCTLs %p\n", sysctls)); 21689022Sps if (!sysctls) 217128348Stjr return; 218128348Stjr 219128348Stjr sysctl_register_set(sysctls); 22060786Sps} 22160786Sps 22260786Spsstatic void 22360786Spslinker_file_unregister_sysctls(linker_file_t lf) 22460786Sps{ 22560786Sps struct linker_set* sysctls; 22660786Sps 22760786Sps KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n", 22860786Sps lf->filename)); 22960786Sps 23060786Sps sysctls = (struct linker_set*) 231128348Stjr linker_file_lookup_symbol(lf, "sysctl_set", 0); 23289022Sps 23389022Sps KLD_DPF(FILE, ("linker_file_unregister_sysctls: SYSCTLs %p\n", sysctls)); 234128348Stjr if (!sysctls) 23560786Sps return; 23660786Sps 23760786Sps sysctl_unregister_set(sysctls); 23860786Sps} 23960786Sps 24060786Spsextern struct linker_set modmetadata_set; 24160786Sps 24260786Spsstatic int 24360786Spslinker_file_register_modules(linker_file_t lf) 24460786Sps{ 24560786Sps int error; 24660786Sps struct linker_set *modules; 24760786Sps struct mod_metadata **mdpp; 24860786Sps const moduledata_t *moddata; 24960786Sps 25060786Sps KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n", 25160786Sps lf->filename)); 25260786Sps 25360786Sps modules = (struct linker_set*) 25460786Sps linker_file_lookup_symbol(lf, "modmetadata_set", 0); 25560786Sps 256128348Stjr if (!modules && lf == linker_kernel_file) 257128348Stjr modules = &modmetadata_set; 25860786Sps 25960786Sps if (modules == NULL) 260161478Sdelphij return 0; 261161478Sdelphij for (mdpp = (struct mod_metadata**)modules->ls_items; *mdpp; mdpp++) { 262161478Sdelphij if ((*mdpp)->md_type != MDT_MODULE) 26360786Sps continue; 26460786Sps moddata = (*mdpp)->md_data; 26560786Sps KLD_DPF(FILE, ("Registering module %s in %s\n", 26660786Sps moddata->name, lf->filename)); 26760786Sps if (module_lookupbyname(moddata->name) != NULL) { 26860786Sps printf("Warning: module %s already exists\n", moddata->name); 26960786Sps continue; /* or return a error ? */ 27060786Sps } 27160786Sps error = module_register(moddata, lf); 27260786Sps if (error) 27360786Sps printf("Module %s failed to register: %d\n", moddata->name, error); 27460786Sps } 27560786Sps return 0; 276128348Stjr} 277128348Stjr 278161478Sdelphijstatic void 27960786Spslinker_init_kernel_modules(void) 28060786Sps{ 28160786Sps linker_file_register_modules(linker_kernel_file); 282161478Sdelphij} 283161478Sdelphij 28460786SpsSYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0); 285161478Sdelphij 28660786Spsint 287161478Sdelphijlinker_load_file(const char* filename, linker_file_t* result) 288161478Sdelphij{ 289161478Sdelphij linker_class_t lc; 29060786Sps linker_file_t lf; 291161478Sdelphij int foundfile, error = 0; 292161478Sdelphij 293161478Sdelphij /* Refuse to load modules if securelevel raised */ 294161478Sdelphij if (securelevel > 0) 295161478Sdelphij return EPERM; 296161478Sdelphij 297161478Sdelphij lf = linker_find_file_by_name(filename); 29860786Sps if (lf) { 29960786Sps KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); 30060786Sps *result = lf; 30160786Sps lf->refs++; 30260786Sps goto out; 30360786Sps } 30460786Sps 305128348Stjr lf = NULL; 30660786Sps foundfile = 0; 307221715Sdelphij TAILQ_FOREACH(lc, &classes, link) { 308221715Sdelphij KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n", 309221715Sdelphij filename, lc->desc)); 31060786Sps error = LINKER_LOAD_FILE(lc, filename, &lf); 311128348Stjr /* 31289022Sps * If we got something other than ENOENT, then it exists but we cannot 31389022Sps * load it for some other reason. 31489022Sps */ 315128348Stjr if (error != ENOENT) 31689022Sps foundfile = 1; 31760786Sps if (lf) { 31860786Sps linker_file_register_modules(lf); 319128348Stjr linker_file_register_sysctls(lf); 320128348Stjr linker_file_sysinit(lf); 321128348Stjr lf->flags |= LINKER_FILE_LINKED; 32260786Sps 32360786Sps *result = lf; 32460786Sps error = 0; 32560786Sps goto out; 32660786Sps } 32760786Sps } 328128348Stjr /* 32960786Sps * Less than ideal, but tells the user whether it failed to load or 33060786Sps * the module was not found. 331128348Stjr */ 33260786Sps if (foundfile) 33360786Sps error = ENOEXEC; /* Format not recognised (or unloadable) */ 334128348Stjr else 33589022Sps error = ENOENT; /* Nothing found */ 33689022Sps 33789022Spsout: 33889022Sps return error; 339128348Stjr} 34089022Sps 34160786Spslinker_file_t 34260786Spslinker_find_file_by_name(const char* filename) 34360786Sps{ 34460786Sps linker_file_t lf = 0; 34560786Sps char *koname; 34660786Sps 34760786Sps koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 34860786Sps if (koname == NULL) 34960786Sps goto out; 35060786Sps sprintf(koname, "%s.ko", filename); 351128348Stjr 352128348Stjr lockmgr(&lock, LK_SHARED, 0, curproc); 35360786Sps TAILQ_FOREACH(lf, &linker_files, link) { 354128348Stjr if (!strcmp(lf->filename, koname)) 35560786Sps break; 35660786Sps if (!strcmp(lf->filename, filename)) 357128348Stjr break; 35860786Sps } 35960786Sps lockmgr(&lock, LK_RELEASE, 0, curproc); 36060786Sps 36160786Spsout: 36260786Sps if (koname) 36360786Sps free(koname, M_LINKER); 36460786Sps return lf; 36560786Sps} 36660786Sps 36760786Spslinker_file_t 36860786Spslinker_find_file_by_id(int fileid) 36960786Sps{ 370221715Sdelphij linker_file_t lf = 0; 37160786Sps 37289022Sps lockmgr(&lock, LK_SHARED, 0, curproc); 373128348Stjr TAILQ_FOREACH(lf, &linker_files, link) 37489022Sps if (lf->id == fileid) 37589022Sps break; 37689022Sps lockmgr(&lock, LK_RELEASE, 0, curproc); 377128348Stjr 37889022Sps return lf; 37989022Sps} 38060786Sps 38160786Spslinker_file_t 38260786Spslinker_make_file(const char* pathname, linker_class_t lc) 383128348Stjr{ 384128348Stjr linker_file_t lf = 0; 38560786Sps const char *filename; 38660786Sps 38760786Sps filename = linker_basename(pathname); 38860786Sps 38960786Sps KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); 39060786Sps lockmgr(&lock, LK_EXCLUSIVE, 0, curproc); 39160786Sps lf = (linker_file_t) kobj_create((kobj_class_t) lc, M_LINKER, M_WAITOK); 39260786Sps if (!lf) 39360786Sps goto out; 39460786Sps 39560786Sps lf->refs = 1; 39660786Sps lf->userrefs = 0; 397229196Sdim lf->flags = 0; 39860786Sps lf->filename = linker_strdup(filename); 399229196Sdim lf->id = next_file_id++; 40060786Sps lf->ndeps = 0; 40160786Sps lf->deps = NULL; 40260786Sps STAILQ_INIT(&lf->common); 40360786Sps TAILQ_INIT(&lf->modules); 40460786Sps 40560786Sps TAILQ_INSERT_TAIL(&linker_files, lf, link); 40660786Sps 40760786Spsout: 40860786Sps lockmgr(&lock, LK_RELEASE, 0, curproc); 40960786Sps return lf; 41060786Sps} 41160786Sps 41260786Spsint 41360786Spslinker_file_unload(linker_file_t file) 41460786Sps{ 41560786Sps module_t mod, next; 41660786Sps modlist_t ml, nextml; 41760786Sps struct common_symbol* cp; 41860786Sps int error = 0; 41960786Sps int i; 42060786Sps 42160786Sps /* Refuse to unload modules if securelevel raised */ 42260786Sps if (securelevel > 0) 42360786Sps return EPERM; 42460786Sps 42560786Sps KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 42660786Sps lockmgr(&lock, LK_EXCLUSIVE, 0, curproc); 42760786Sps if (file->refs == 1) { 42860786Sps KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 42960786Sps /* 43060786Sps * Inform any modules associated with this file. 43160786Sps */ 43260786Sps for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 43360786Sps next = module_getfnext(mod); 43460786Sps 43560786Sps /* 43660786Sps * Give the module a chance to veto the unload. 43760786Sps */ 43860786Sps if ((error = module_unload(mod)) != 0) { 43960786Sps KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", 44060786Sps mod)); 44160786Sps lockmgr(&lock, LK_RELEASE, 0, curproc); 44260786Sps goto out; 44360786Sps } 44460786Sps 44560786Sps module_release(mod); 44660786Sps } 44760786Sps } 44860786Sps 44960786Sps file->refs--; 45060786Sps if (file->refs > 0) { 45160786Sps lockmgr(&lock, LK_RELEASE, 0, curproc); 45260786Sps goto out; 45360786Sps } 45460786Sps 455229196Sdim for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) { 45660786Sps nextml = TAILQ_NEXT(ml, link); 457229196Sdim if (ml->container == file) { 45860786Sps TAILQ_REMOVE(&found_modules, ml, link); 45960786Sps } 46060786Sps } 46160786Sps 46260786Sps /* Don't try to run SYSUNINITs if we are unloaded due to a link error */ 46360786Sps if (file->flags & LINKER_FILE_LINKED) { 46460786Sps linker_file_sysuninit(file); 46560786Sps linker_file_unregister_sysctls(file); 46660786Sps } 46760786Sps 46860786Sps TAILQ_REMOVE(&linker_files, file, link); 46960786Sps lockmgr(&lock, LK_RELEASE, 0, curproc); 47060786Sps 47160786Sps if (file->deps) { 47260786Sps for (i = 0; i < file->ndeps; i++) 47360786Sps linker_file_unload(file->deps[i]); 47460786Sps free(file->deps, M_LINKER); 47560786Sps file->deps = NULL; 47660786Sps } 47760786Sps 47860786Sps for (cp = STAILQ_FIRST(&file->common); cp; 47960786Sps cp = STAILQ_FIRST(&file->common)) { 48060786Sps STAILQ_REMOVE(&file->common, cp, common_symbol, link); 481229196Sdim free(cp, M_LINKER); 48260786Sps } 48360786Sps 484229196Sdim LINKER_UNLOAD(file); 48560786Sps if (file->filename) { 48660786Sps free(file->filename, M_LINKER); 48760786Sps file->filename = NULL; 48860786Sps } 48960786Sps kobj_delete((kobj_t) file, M_LINKER); 49060786Sps 49160786Spsout: 49260786Sps return error; 49360786Sps} 49460786Sps 49560786Spsint 49660786Spslinker_file_add_dependancy(linker_file_t file, linker_file_t dep) 49760786Sps{ 49860786Sps linker_file_t* newdeps; 49960786Sps 50060786Sps newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*), 50160786Sps M_LINKER, M_WAITOK | M_ZERO); 50260786Sps if (newdeps == NULL) 50360786Sps return ENOMEM; 50460786Sps 50560786Sps if (file->deps) { 50660786Sps bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 50760786Sps free(file->deps, M_LINKER); 50860786Sps } 50960786Sps file->deps = newdeps; 51060786Sps file->deps[file->ndeps] = dep; 51160786Sps file->ndeps++; 51260786Sps 51360786Sps return 0; 51460786Sps} 51560786Sps 51660786Spscaddr_t 51760786Spslinker_file_lookup_symbol(linker_file_t file, const char* name, int deps) 51860786Sps{ 51960786Sps c_linker_sym_t sym; 52060786Sps linker_symval_t symval; 52160786Sps caddr_t address; 52260786Sps size_t common_size = 0; 52360786Sps int i; 52460786Sps 52560786Sps KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", 52660786Sps file, name, deps)); 52760786Sps 52860786Sps if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) { 52960786Sps LINKER_SYMBOL_VALUES(file, sym, &symval); 53060786Sps if (symval.value == 0) 53160786Sps /* 53260786Sps * For commons, first look them up in the dependancies and 53360786Sps * only allocate space if not found there. 53460786Sps */ 53560786Sps common_size = symval.size; 53660786Sps else { 53760786Sps KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value)); 53860786Sps return symval.value; 53960786Sps } 540161478Sdelphij } 54160786Sps 54260786Sps if (deps) { 54360786Sps for (i = 0; i < file->ndeps; i++) { 54460786Sps address = linker_file_lookup_symbol(file->deps[i], name, 0); 54560786Sps if (address) { 54660786Sps KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address)); 54760786Sps return address; 54860786Sps } 54960786Sps } 55060786Sps } 55160786Sps 55260786Sps if (common_size > 0) { 55360786Sps /* 55460786Sps * This is a common symbol which was not found in the 55560786Sps * dependancies. We maintain a simple common symbol table in 55660786Sps * the file object. 55760786Sps */ 558229195Sdim struct common_symbol* cp; 55960786Sps 56060786Sps STAILQ_FOREACH(cp, &file->common, link) 56160786Sps if (!strcmp(cp->name, name)) { 56260786Sps KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); 56360786Sps return cp->address; 56460786Sps } 56560786Sps 56660786Sps /* 56760786Sps * Round the symbol size up to align. 56860786Sps */ 56960786Sps common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 57089022Sps cp = malloc(sizeof(struct common_symbol) 571170259Sdelphij + common_size 57289022Sps + strlen(name) + 1, 573170259Sdelphij M_LINKER, M_WAITOK | M_ZERO); 57489022Sps if (!cp) { 575229195Sdim KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); 57689022Sps return 0; 57789022Sps } 57889022Sps 57960786Sps cp->address = (caddr_t) (cp + 1); 58089022Sps cp->name = cp->address + common_size; 58189022Sps strcpy(cp->name, name); 58289022Sps bzero(cp->address, common_size); 58389022Sps STAILQ_INSERT_TAIL(&file->common, cp, link); 58489022Sps 58589022Sps KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); 58689022Sps return cp->address; 587229195Sdim } 58889022Sps 589 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 590 return 0; 591} 592 593#ifdef DDB 594/* 595 * DDB Helpers. DDB has to look across multiple files with their own 596 * symbol tables and string tables. 597 * 598 * Note that we do not obey list locking protocols here. We really don't 599 * need DDB to hang because somebody's got the lock held. We'll take the 600 * chance that the files list is inconsistant instead. 601 */ 602 603int 604linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 605{ 606 linker_file_t lf; 607 608 TAILQ_FOREACH(lf, &linker_files, link) { 609 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0) 610 return 0; 611 } 612 return ENOENT; 613} 614 615int 616linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 617{ 618 linker_file_t lf; 619 u_long off = (uintptr_t)value; 620 u_long diff, bestdiff; 621 c_linker_sym_t best; 622 c_linker_sym_t es; 623 624 best = 0; 625 bestdiff = off; 626 TAILQ_FOREACH(lf, &linker_files, link) { 627 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0) 628 continue; 629 if (es != 0 && diff < bestdiff) { 630 best = es; 631 bestdiff = diff; 632 } 633 if (bestdiff == 0) 634 break; 635 } 636 if (best) { 637 *sym = best; 638 *diffp = bestdiff; 639 return 0; 640 } else { 641 *sym = 0; 642 *diffp = off; 643 return ENOENT; 644 } 645} 646 647int 648linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 649{ 650 linker_file_t lf; 651 652 TAILQ_FOREACH(lf, &linker_files, link) { 653 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0) 654 return 0; 655 } 656 return ENOENT; 657} 658 659#endif 660 661/* 662 * Syscalls. 663 */ 664 665int 666kldload(struct proc* p, struct kldload_args* uap) 667{ 668 char* pathname, *realpath; 669 const char *filename; 670 linker_file_t lf; 671 int error = 0; 672 673 p->p_retval[0] = -1; 674 675 if (securelevel > 0) /* redundant, but that's OK */ 676 return EPERM; 677 678 if ((error = suser(p)) != 0) 679 return error; 680 681 realpath = NULL; 682 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 683 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0) 684 goto out; 685 686 realpath = linker_search_path(pathname); 687 if (realpath == NULL) { 688 error = ENOENT; 689 goto out; 690 } 691 /* Can't load more than one file with the same name */ 692 filename = linker_basename(realpath); 693 if (linker_find_file_by_name(filename)) { 694 error = EEXIST; 695 goto out; 696 } 697 698 if ((error = linker_load_file(realpath, &lf)) != 0) 699 goto out; 700 701 lf->userrefs++; 702 p->p_retval[0] = lf->id; 703 704out: 705 if (pathname) 706 free(pathname, M_TEMP); 707 if (realpath) 708 free(realpath, M_LINKER); 709 return error; 710} 711 712int 713kldunload(struct proc* p, struct kldunload_args* uap) 714{ 715 linker_file_t lf; 716 int error = 0; 717 718 if (securelevel > 0) /* redundant, but that's OK */ 719 return EPERM; 720 721 if ((error = suser(p)) != 0) 722 return error; 723 724 lf = linker_find_file_by_id(SCARG(uap, fileid)); 725 if (lf) { 726 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 727 if (lf->userrefs == 0) { 728 printf("kldunload: attempt to unload file that was loaded by the kernel\n"); 729 error = EBUSY; 730 goto out; 731 } 732 lf->userrefs--; 733 error = linker_file_unload(lf); 734 if (error) 735 lf->userrefs++; 736 } else 737 error = ENOENT; 738 739out: 740 return error; 741} 742 743int 744kldfind(struct proc* p, struct kldfind_args* uap) 745{ 746 char* pathname; 747 const char *filename; 748 linker_file_t lf; 749 int error = 0; 750 751 p->p_retval[0] = -1; 752 753 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 754 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0) 755 goto out; 756 757 filename = linker_basename(pathname); 758 759 lf = linker_find_file_by_name(filename); 760 if (lf) 761 p->p_retval[0] = lf->id; 762 else 763 error = ENOENT; 764 765out: 766 if (pathname) 767 free(pathname, M_TEMP); 768 return error; 769} 770 771int 772kldnext(struct proc* p, struct kldnext_args* uap) 773{ 774 linker_file_t lf; 775 int error = 0; 776 777 if (SCARG(uap, fileid) == 0) { 778 if (TAILQ_FIRST(&linker_files)) 779 p->p_retval[0] = TAILQ_FIRST(&linker_files)->id; 780 else 781 p->p_retval[0] = 0; 782 return 0; 783 } 784 785 lf = linker_find_file_by_id(SCARG(uap, fileid)); 786 if (lf) { 787 if (TAILQ_NEXT(lf, link)) 788 p->p_retval[0] = TAILQ_NEXT(lf, link)->id; 789 else 790 p->p_retval[0] = 0; 791 } else 792 error = ENOENT; 793 794 return error; 795} 796 797int 798kldstat(struct proc* p, struct kldstat_args* uap) 799{ 800 linker_file_t lf; 801 int error = 0; 802 int version; 803 struct kld_file_stat* stat; 804 int namelen; 805 806 lf = linker_find_file_by_id(SCARG(uap, fileid)); 807 if (!lf) { 808 error = ENOENT; 809 goto out; 810 } 811 812 stat = SCARG(uap, stat); 813 814 /* 815 * Check the version of the user's structure. 816 */ 817 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0) 818 goto out; 819 if (version != sizeof(struct kld_file_stat)) { 820 error = EINVAL; 821 goto out; 822 } 823 824 namelen = strlen(lf->filename) + 1; 825 if (namelen > MAXPATHLEN) 826 namelen = MAXPATHLEN; 827 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0) 828 goto out; 829 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0) 830 goto out; 831 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0) 832 goto out; 833 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0) 834 goto out; 835 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0) 836 goto out; 837 838 p->p_retval[0] = 0; 839 840out: 841 return error; 842} 843 844int 845kldfirstmod(struct proc* p, struct kldfirstmod_args* uap) 846{ 847 linker_file_t lf; 848 int error = 0; 849 850 lf = linker_find_file_by_id(SCARG(uap, fileid)); 851 if (lf) { 852 if (TAILQ_FIRST(&lf->modules)) 853 p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules)); 854 else 855 p->p_retval[0] = 0; 856 } else 857 error = ENOENT; 858 859 return error; 860} 861 862int 863kldsym(struct proc *p, struct kldsym_args *uap) 864{ 865 char *symstr = NULL; 866 c_linker_sym_t sym; 867 linker_symval_t symval; 868 linker_file_t lf; 869 struct kld_sym_lookup lookup; 870 int error = 0; 871 872 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0) 873 goto out; 874 if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) { 875 error = EINVAL; 876 goto out; 877 } 878 879 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 880 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 881 goto out; 882 883 if (SCARG(uap, fileid) != 0) { 884 lf = linker_find_file_by_id(SCARG(uap, fileid)); 885 if (lf == NULL) { 886 error = ENOENT; 887 goto out; 888 } 889 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 890 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 891 lookup.symvalue = (uintptr_t)symval.value; 892 lookup.symsize = symval.size; 893 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 894 } else 895 error = ENOENT; 896 } else { 897 TAILQ_FOREACH(lf, &linker_files, link) { 898 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 899 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 900 lookup.symvalue = (uintptr_t)symval.value; 901 lookup.symsize = symval.size; 902 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 903 break; 904 } 905 } 906 if (!lf) 907 error = ENOENT; 908 } 909out: 910 if (symstr) 911 free(symstr, M_TEMP); 912 return error; 913} 914 915/* 916 * Preloaded module support 917 */ 918 919static modlist_t 920modlist_lookup(const char *name) 921{ 922 modlist_t mod; 923 924 TAILQ_FOREACH(mod, &found_modules, link) { 925 if (!strcmp(mod->name, name)) 926 return mod; 927 } 928 return NULL; 929} 930 931/* 932 * This routine is cheap and nasty but will work for data pointers. 933 */ 934static void * 935linker_reloc_ptr(linker_file_t lf, void *offset) 936{ 937 return lf->address + (uintptr_t)offset; 938} 939 940static void 941linker_preload(void* arg) 942{ 943 caddr_t modptr; 944 char *modname, *nmodname; 945 char *modtype; 946 linker_file_t lf; 947 linker_class_t lc; 948 int error; 949 struct linker_set *sysinits; 950 linker_file_list_t loaded_files; 951 linker_file_list_t depended_files; 952 struct linker_set *deps; 953 struct mod_metadata *mp, *nmp; 954 int i, j; 955 int resolves; 956 modlist_t mod; 957 958 TAILQ_INIT(&loaded_files); 959 TAILQ_INIT(&depended_files); 960 TAILQ_INIT(&found_modules); 961 error = 0; 962 963 modptr = NULL; 964 while ((modptr = preload_search_next_name(modptr)) != NULL) { 965 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 966 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 967 if (modname == NULL) { 968 printf("Preloaded module at %p does not have a name!\n", modptr); 969 continue; 970 } 971 if (modtype == NULL) { 972 printf("Preloaded module at %p does not have a type!\n", modptr); 973 continue; 974 } 975 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr); 976 lf = NULL; 977 TAILQ_FOREACH(lc, &classes, link) { 978 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 979 if (error) { 980 lf = NULL; 981 break; 982 } 983 } 984 if (lf) 985 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 986 } 987 988 /* 989 * First get a list of stuff in the kernel. 990 */ 991 deps = (struct linker_set*) 992 linker_file_lookup_symbol(linker_kernel_file, MDT_SETNAME, 0); 993 if (deps) { 994 for (i = 0; i < deps->ls_length; i++) { 995 mp = deps->ls_items[i]; 996 if (mp->md_type != MDT_VERSION) 997 continue; 998 modname = mp->md_cval; 999 if (modlist_lookup(modname) != NULL) { 1000 printf("module %s already present!\n", modname); 1001 /* XXX what can we do? this is a build error. :-( */ 1002 continue; 1003 } 1004 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT|M_ZERO); 1005 if (mod == NULL) 1006 panic("no memory for module list"); 1007 mod->container = linker_kernel_file; 1008 mod->name = modname; 1009 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1010 } 1011 } 1012 1013 /* 1014 * this is a once-off kinky bubble sort 1015 * resolve relocation dependency requirements 1016 */ 1017restart: 1018 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1019 deps = (struct linker_set*) 1020 linker_file_lookup_symbol(lf, MDT_SETNAME, 0); 1021 /* 1022 * First, look to see if we would successfully link with this stuff. 1023 */ 1024 resolves = 1; /* unless we know otherwise */ 1025 if (deps) { 1026 for (i = 0; i < deps->ls_length; i++) { 1027 mp = linker_reloc_ptr(lf, deps->ls_items[i]); 1028 if (mp->md_type != MDT_DEPEND) 1029 continue; 1030 modname = linker_reloc_ptr(lf, mp->md_cval); 1031 for (j = 0; j < deps->ls_length; j++) { 1032 nmp = linker_reloc_ptr(lf, deps->ls_items[j]); 1033 if (nmp->md_type != MDT_VERSION) 1034 continue; 1035 nmodname = linker_reloc_ptr(lf, nmp->md_cval); 1036 if (strcmp(modname, nmodname) == 0) 1037 break; 1038 } 1039 if (j < deps->ls_length) /* it's a self reference */ 1040 continue; 1041 if (modlist_lookup(modname) == NULL) { 1042 /* ok, the module isn't here yet, we are not finished */ 1043 resolves = 0; 1044 } 1045 } 1046 } 1047 /* 1048 * OK, if we found our modules, we can link. So, "provide" the 1049 * modules inside and add it to the end of the link order list. 1050 */ 1051 if (resolves) { 1052 if (deps) { 1053 for (i = 0; i < deps->ls_length; i++) { 1054 mp = linker_reloc_ptr(lf, deps->ls_items[i]); 1055 if (mp->md_type != MDT_VERSION) 1056 continue; 1057 modname = linker_reloc_ptr(lf, mp->md_cval); 1058 if (modlist_lookup(modname) != NULL) { 1059 printf("module %s already present!\n", modname); 1060 linker_file_unload(lf); 1061 TAILQ_REMOVE(&loaded_files, lf, loaded); 1062 goto restart; /* we changed the tailq next ptr */ 1063 } 1064 mod = malloc(sizeof(struct modlist), M_LINKER, 1065 M_NOWAIT|M_ZERO); 1066 if (mod == NULL) 1067 panic("no memory for module list"); 1068 mod->container = lf; 1069 mod->name = modname; 1070 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1071 } 1072 } 1073 TAILQ_REMOVE(&loaded_files, lf, loaded); 1074 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1075 /* 1076 * Since we provided modules, we need to restart the sort so 1077 * that the previous files that depend on us have a chance. 1078 * Also, we've busted the tailq next pointer with the REMOVE. 1079 */ 1080 goto restart; 1081 } 1082 } 1083 1084 /* 1085 * At this point, we check to see what could not be resolved.. 1086 */ 1087 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1088 printf("KLD file %s is missing dependencies\n", lf->filename); 1089 linker_file_unload(lf); 1090 TAILQ_REMOVE(&loaded_files, lf, loaded); 1091 } 1092 1093 /* 1094 * We made it. Finish off the linking in the order we determined. 1095 */ 1096 TAILQ_FOREACH(lf, &depended_files, loaded) { 1097 if (linker_kernel_file) { 1098 linker_kernel_file->refs++; 1099 error = linker_file_add_dependancy(lf, linker_kernel_file); 1100 if (error) 1101 panic("cannot add dependency"); 1102 } 1103 lf->userrefs++; /* so we can (try to) kldunload it */ 1104 deps = (struct linker_set*) 1105 linker_file_lookup_symbol(lf, MDT_SETNAME, 0); 1106 if (deps) { 1107 for (i = 0; i < deps->ls_length; i++) { 1108 mp = linker_reloc_ptr(lf, deps->ls_items[i]); 1109 if (mp->md_type != MDT_DEPEND) 1110 continue; 1111 modname = linker_reloc_ptr(lf, mp->md_cval); 1112 mod = modlist_lookup(modname); 1113 mod->container->refs++; 1114 error = linker_file_add_dependancy(lf, mod->container); 1115 if (error) 1116 panic("cannot add dependency"); 1117 } 1118 } 1119 1120 /* Now do relocation etc using the symbol search paths established by the dependencies */ 1121 error = LINKER_LINK_PRELOAD_FINISH(lf); 1122 if (error) { 1123 printf("KLD file %s - could not finalize loading\n", lf->filename); 1124 linker_file_unload(lf); 1125 continue; 1126 } 1127 1128 linker_file_register_modules(lf); 1129 sysinits = (struct linker_set*) 1130 linker_file_lookup_symbol(lf, "sysinit_set", 0); 1131 if (sysinits) 1132 sysinit_add((struct sysinit **)sysinits->ls_items); 1133 linker_file_register_sysctls(lf); 1134 lf->flags |= LINKER_FILE_LINKED; 1135 } 1136 /* woohoo! we made it! */ 1137} 1138 1139SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1140 1141/* 1142 * Search for a not-loaded module by name. 1143 * 1144 * Modules may be found in the following locations: 1145 * 1146 * - preloaded (result is just the module name) 1147 * - on disk (result is full path to module) 1148 * 1149 * If the module name is qualified in any way (contains path, etc.) 1150 * the we simply return a copy of it. 1151 * 1152 * The search path can be manipulated via sysctl. Note that we use the ';' 1153 * character as a separator to be consistent with the bootloader. 1154 */ 1155 1156static char def_linker_path[] = "/boot/modules/;/modules/;/boot/kernel/"; 1157static char linker_path[MAXPATHLEN] = ""; 1158 1159SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1160 sizeof(linker_path), "module load search path"); 1161 1162TUNABLE_STR_DECL("module_path", def_linker_path, linker_path, 1163 sizeof(linker_path)); 1164 1165static char *linker_ext_list[] = { 1166 ".ko", 1167 "", 1168 NULL 1169}; 1170 1171static char * 1172linker_search_path(const char *name) 1173{ 1174 struct nameidata nd; 1175 struct proc *p = curproc; /* XXX */ 1176 char *cp, *ep, *result, **cpp; 1177 int error, extlen, len, flags; 1178 enum vtype type; 1179 1180 /* qualified at all? */ 1181 if (index(name, '/')) 1182 return(linker_strdup(name)); 1183 1184 extlen = 0; 1185 for (cpp = linker_ext_list; *cpp; cpp++) { 1186 len = strlen(*cpp); 1187 if (len > extlen) 1188 extlen = len; 1189 } 1190 extlen++; /* trailing '\0' */ 1191 1192 /* traverse the linker path */ 1193 cp = linker_path; 1194 len = strlen(name); 1195 for (;;) { 1196 1197 /* find the end of this component */ 1198 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 1199 ; 1200 result = malloc((len + (ep - cp) + extlen + 1), M_LINKER, M_WAITOK); 1201 if (result == NULL) /* actually ENOMEM */ 1202 return(NULL); 1203 for (cpp = linker_ext_list; *cpp; cpp++) { 1204 strncpy(result, cp, ep - cp); 1205 strcpy(result + (ep - cp), "/"); 1206 strcat(result, name); 1207 strcat(result, *cpp); 1208 /* 1209 * Attempt to open the file, and return the path if we succeed 1210 * and it's a regular file. 1211 */ 1212 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p); 1213 flags = FREAD; 1214 error = vn_open(&nd, &flags, 0); 1215 if (error == 0) { 1216 NDFREE(&nd, NDF_ONLY_PNBUF); 1217 type = nd.ni_vp->v_type; 1218 VOP_UNLOCK(nd.ni_vp, 0, p); 1219 vn_close(nd.ni_vp, FREAD, p->p_ucred, p); 1220 if (type == VREG) 1221 return(result); 1222 } 1223 } 1224 free(result, M_LINKER); 1225 1226 if (*ep == 0) 1227 break; 1228 cp = ep + 1; 1229 } 1230 return(NULL); 1231} 1232 1233static const char * 1234linker_basename(const char* path) 1235{ 1236 const char *filename; 1237 1238 filename = rindex(path, '/'); 1239 if (filename == NULL) 1240 return path; 1241 if (filename[1]) 1242 filename++; 1243 return filename; 1244} 1245 1246/* 1247 * Find a file which contains given module and load it, 1248 * if "parent" is not NULL, register a reference to it. 1249 */ 1250static int 1251linker_load_module(const char *modname, struct linker_file *parent) 1252{ 1253 linker_file_t lfdep; 1254 const char *filename; 1255 char *pathname; 1256 int error; 1257 1258 /* 1259 * There will be a system to look up or guess a file name from 1260 * a module name. 1261 * For now we just try to load a file with the same name. 1262 */ 1263 pathname = linker_search_path(modname); 1264 if (pathname == NULL) 1265 return ENOENT; 1266 1267 /* Can't load more than one file with the same basename */ 1268 filename = linker_basename(pathname); 1269 if (linker_find_file_by_name(filename)) { 1270 error = EEXIST; 1271 goto out; 1272 } 1273 1274 do { 1275 error = linker_load_file(pathname, &lfdep); 1276 if (error) 1277 break; 1278 if (parent) { 1279 error = linker_file_add_dependancy(parent, lfdep); 1280 if (error) 1281 break; 1282 } 1283 } while(0); 1284out: 1285 if (pathname) 1286 free(pathname, M_LINKER); 1287 return error; 1288} 1289 1290/* 1291 * This routine is responsible for finding dependencies of userland 1292 * initiated kldload(2)'s of files. 1293 */ 1294int 1295linker_load_dependancies(linker_file_t lf) 1296{ 1297 linker_file_t lfdep; 1298 struct linker_set *deps; 1299 struct mod_metadata *mp, *nmp; 1300 modlist_t mod; 1301 char *modname, *nmodname; 1302 int i, j, error = 0; 1303 1304 /* 1305 * All files are dependant on /kernel. 1306 */ 1307 if (linker_kernel_file) { 1308 linker_kernel_file->refs++; 1309 error = linker_file_add_dependancy(lf, linker_kernel_file); 1310 if (error) 1311 return error; 1312 } 1313 1314 deps = (struct linker_set*) 1315 linker_file_lookup_symbol(lf, MDT_SETNAME, 0); 1316 if (deps != NULL) { 1317 for (i = 0; i < deps->ls_length; i++) { 1318 mp = linker_reloc_ptr(lf, deps->ls_items[i]); 1319 if (mp->md_type != MDT_VERSION) 1320 continue; 1321 modname = linker_reloc_ptr(lf, mp->md_cval); 1322 if (modlist_lookup(modname) != NULL) { 1323 printf("module %s already present!\n", modname); 1324 return EEXIST; 1325 } 1326 } 1327 } 1328 if (deps != NULL) { 1329 for (i = 0; i < deps->ls_length; i++) { 1330 mp = linker_reloc_ptr(lf, deps->ls_items[i]); 1331 if (mp->md_type != MDT_DEPEND) 1332 continue; 1333 modname = linker_reloc_ptr(lf, mp->md_cval); 1334 nmodname = NULL; 1335 for (j = 0; j < deps->ls_length; j++) { 1336 nmp = linker_reloc_ptr(lf, deps->ls_items[j]); 1337 if (nmp->md_type != MDT_VERSION) 1338 continue; 1339 nmodname = linker_reloc_ptr(lf, nmp->md_cval); 1340 if (strcmp(modname, nmodname) == 0) 1341 break; 1342 } 1343 if (j < deps->ls_length) /* early exit, it's a self reference */ 1344 continue; 1345 mod = modlist_lookup(modname); 1346 if (mod) { /* woohoo, it's loaded already */ 1347 lfdep = mod->container; 1348 lfdep->refs++; 1349 error = linker_file_add_dependancy(lf, lfdep); 1350 if (error) 1351 break; 1352 continue; 1353 } 1354 error = linker_load_module(modname, lf); 1355 if (error) { 1356 printf("KLD %s: depends on %s - not available\n", 1357 lf->filename, modname); 1358 break; 1359 } 1360 } 1361 1362 } 1363 if (error == 0 && deps) { 1364 for (i = 0; i < deps->ls_length; i++) { 1365 mp = linker_reloc_ptr(lf, deps->ls_items[i]); 1366 if (mp->md_type != MDT_VERSION) 1367 continue; 1368 modname = linker_reloc_ptr(lf, mp->md_cval); 1369 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT|M_ZERO); 1370 if (mod == NULL) 1371 panic("no memory for module list"); 1372 mod->container = lf; 1373 mod->name = modname; 1374 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1375 } 1376 } 1377 return error; 1378} 1379