1997-05-07 16:05:47 +00:00
|
|
|
/*-
|
2000-04-24 17:08:04 +00:00
|
|
|
* Copyright (c) 1997-2000 Doug Rabson
|
1997-05-07 16:05:47 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1997-05-07 16:05:47 +00:00
|
|
|
*/
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
#include "opt_ddb.h"
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/lock.h>
|
2001-09-01 19:04:37 +00:00
|
|
|
#include <sys/mutex.h>
|
1997-05-07 16:05:47 +00:00
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/linker.h>
|
1998-10-10 00:07:53 +00:00
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/libkern.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/sysctl.h>
|
1997-05-07 16:05:47 +00:00
|
|
|
|
1999-12-15 23:02:35 +00:00
|
|
|
|
2000-04-24 17:08:04 +00:00
|
|
|
#include "linker_if.h"
|
|
|
|
|
1998-11-06 15:10:17 +00:00
|
|
|
#ifdef KLD_DEBUG
|
|
|
|
int kld_debug = 0;
|
|
|
|
#endif
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
/*static char *linker_search_path(const char *name, struct mod_depend *verinfo);*/
|
2000-04-29 13:19:31 +00:00
|
|
|
static const char *linker_basename(const char* path);
|
2001-09-11 01:09:24 +00:00
|
|
|
static int linker_load_module(const char *kldname, const char *modname,
|
|
|
|
struct linker_file *parent, struct mod_depend *verinfo,
|
|
|
|
struct linker_file **lfpp);
|
2000-04-29 13:19:31 +00:00
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
/* Metadata from the static kernel */
|
|
|
|
SET_DECLARE(modmetadata_set, struct mod_metadata);
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
|
|
|
|
|
1998-11-04 15:20:58 +00:00
|
|
|
linker_file_t linker_kernel_file;
|
1997-11-20 20:07:59 +00:00
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
static struct lock lock; /* lock for the file list */
|
|
|
|
static linker_class_list_t classes;
|
1999-08-20 00:18:07 +00:00
|
|
|
static linker_file_list_t linker_files;
|
1997-05-07 16:05:47 +00:00
|
|
|
static int next_file_id = 1;
|
|
|
|
|
2001-11-18 18:19:35 +00:00
|
|
|
#define LINKER_GET_NEXT_FILE_ID(a) do { \
|
|
|
|
linker_file_t lftmp; \
|
|
|
|
\
|
|
|
|
retry: \
|
|
|
|
TAILQ_FOREACH(lftmp, &linker_files, link) { \
|
|
|
|
if (next_file_id == lftmp->id) { \
|
|
|
|
next_file_id++; \
|
|
|
|
goto retry; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
(a) = next_file_id; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
/* XXX wrong name; we're looking at version provision tags here, not modules */
|
2000-05-26 02:09:24 +00:00
|
|
|
typedef TAILQ_HEAD(, modlist) modlisthead_t;
|
2000-04-29 13:19:31 +00:00
|
|
|
struct modlist {
|
2000-05-26 02:09:24 +00:00
|
|
|
TAILQ_ENTRY(modlist) link; /* chain together all modules */
|
2000-04-29 13:19:31 +00:00
|
|
|
linker_file_t container;
|
|
|
|
const char *name;
|
2001-03-22 08:58:45 +00:00
|
|
|
int version;
|
2000-04-29 13:19:31 +00:00
|
|
|
};
|
|
|
|
typedef struct modlist *modlist_t;
|
|
|
|
static modlisthead_t found_modules;
|
|
|
|
|
2000-04-24 17:08:04 +00:00
|
|
|
static char *
|
|
|
|
linker_strdup(const char *str)
|
|
|
|
{
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
|
|
|
|
strcpy(result, str);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
static void
|
|
|
|
linker_init(void* arg)
|
|
|
|
{
|
|
|
|
lockinit(&lock, PVM, "klink", 0, 0);
|
|
|
|
TAILQ_INIT(&classes);
|
1999-08-20 00:18:07 +00:00
|
|
|
TAILQ_INIT(&linker_files);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
int
|
2000-04-24 17:08:04 +00:00
|
|
|
linker_add_class(linker_class_t lc)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2000-04-24 17:08:04 +00:00
|
|
|
kobj_class_compile((kobj_class_t) lc);
|
2000-04-29 13:19:31 +00:00
|
|
|
TAILQ_INSERT_TAIL(&classes, lc, link);
|
1997-05-07 16:05:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linker_file_sysinit(linker_file_t lf)
|
|
|
|
{
|
2001-06-13 10:58:39 +00:00
|
|
|
struct sysinit** start, ** stop;
|
1997-05-07 16:05:47 +00:00
|
|
|
struct sysinit** sipp;
|
|
|
|
struct sysinit** xipp;
|
|
|
|
struct sysinit* save;
|
|
|
|
|
|
|
|
KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
|
|
|
|
lf->filename));
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* Perform a bubble sort of the system initialization objects by
|
|
|
|
* their subsystem (primary key) and order (secondary key).
|
|
|
|
*
|
|
|
|
* Since some things care about execution order, this is the
|
|
|
|
* operation which ensures continued function.
|
|
|
|
*/
|
2001-06-13 10:58:39 +00:00
|
|
|
for (sipp = start; sipp < stop; sipp++) {
|
|
|
|
for (xipp = sipp + 1; xipp < stop; xipp++) {
|
2000-07-09 23:58:56 +00:00
|
|
|
if ((*sipp)->subsystem < (*xipp)->subsystem ||
|
1998-11-10 08:49:28 +00:00
|
|
|
((*sipp)->subsystem == (*xipp)->subsystem &&
|
|
|
|
(*sipp)->order <= (*xipp)->order))
|
1997-05-07 16:05:47 +00:00
|
|
|
continue; /* skip*/
|
|
|
|
save = *sipp;
|
|
|
|
*sipp = *xipp;
|
|
|
|
*xipp = save;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse the (now) ordered list of system initialization tasks.
|
|
|
|
* Perform each task, and continue on to the next task.
|
|
|
|
*/
|
2001-06-13 10:58:39 +00:00
|
|
|
for (sipp = start; sipp < stop; sipp++) {
|
1998-11-10 08:49:28 +00:00
|
|
|
if ((*sipp)->subsystem == SI_SUB_DUMMY)
|
1997-05-07 16:05:47 +00:00
|
|
|
continue; /* skip dummy task(s)*/
|
|
|
|
|
1999-07-01 13:21:46 +00:00
|
|
|
/* Call function */
|
|
|
|
(*((*sipp)->func))((*sipp)->udata);
|
1998-11-10 08:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linker_file_sysuninit(linker_file_t lf)
|
|
|
|
{
|
2001-06-13 10:58:39 +00:00
|
|
|
struct sysinit** start, ** stop;
|
1998-11-10 08:49:28 +00:00
|
|
|
struct sysinit** sipp;
|
|
|
|
struct sysinit** xipp;
|
|
|
|
struct sysinit* save;
|
|
|
|
|
|
|
|
KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
|
|
|
|
lf->filename));
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
|
1998-11-10 08:49:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a reverse bubble sort of the system initialization objects
|
|
|
|
* by their subsystem (primary key) and order (secondary key).
|
|
|
|
*
|
|
|
|
* Since some things care about execution order, this is the
|
|
|
|
* operation which ensures continued function.
|
|
|
|
*/
|
2001-06-13 10:58:39 +00:00
|
|
|
for (sipp = start; sipp < stop; sipp++) {
|
|
|
|
for (xipp = sipp + 1; xipp < stop; xipp++) {
|
2000-07-09 23:58:56 +00:00
|
|
|
if ((*sipp)->subsystem > (*xipp)->subsystem ||
|
1998-11-10 08:49:28 +00:00
|
|
|
((*sipp)->subsystem == (*xipp)->subsystem &&
|
|
|
|
(*sipp)->order >= (*xipp)->order))
|
|
|
|
continue; /* skip*/
|
|
|
|
save = *sipp;
|
|
|
|
*sipp = *xipp;
|
|
|
|
*xipp = save;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse the (now) ordered list of system initialization tasks.
|
|
|
|
* Perform each task, and continue on to the next task.
|
|
|
|
*/
|
2001-06-13 10:58:39 +00:00
|
|
|
for (sipp = start; sipp < stop; sipp++) {
|
1998-11-10 08:49:28 +00:00
|
|
|
if ((*sipp)->subsystem == SI_SUB_DUMMY)
|
|
|
|
continue; /* skip dummy task(s)*/
|
|
|
|
|
1999-07-01 13:21:46 +00:00
|
|
|
/* Call function */
|
|
|
|
(*((*sipp)->func))((*sipp)->udata);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-16 10:49:55 +00:00
|
|
|
static void
|
|
|
|
linker_file_register_sysctls(linker_file_t lf)
|
|
|
|
{
|
2001-06-13 10:58:39 +00:00
|
|
|
struct sysctl_oid **start, **stop, **oidp;
|
1999-02-16 10:49:55 +00:00
|
|
|
|
|
|
|
KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
|
|
|
|
lf->filename));
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
|
1999-02-16 10:49:55 +00:00
|
|
|
return;
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
for (oidp = start; oidp < stop; oidp++)
|
|
|
|
sysctl_register_oid(*oidp);
|
1999-02-16 10:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linker_file_unregister_sysctls(linker_file_t lf)
|
|
|
|
{
|
2001-06-13 10:58:39 +00:00
|
|
|
struct sysctl_oid **start, **stop, **oidp;
|
1999-02-16 10:49:55 +00:00
|
|
|
|
|
|
|
KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
|
|
|
|
lf->filename));
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
|
1999-02-16 10:49:55 +00:00
|
|
|
return;
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
for (oidp = start; oidp < stop; oidp++)
|
|
|
|
sysctl_unregister_oid(*oidp);
|
1999-02-16 10:49:55 +00:00
|
|
|
}
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
static int
|
|
|
|
linker_file_register_modules(linker_file_t lf)
|
|
|
|
{
|
2001-03-22 07:14:42 +00:00
|
|
|
int error;
|
2001-06-13 10:58:39 +00:00
|
|
|
struct mod_metadata **start, **stop;
|
|
|
|
struct mod_metadata **mdp;
|
2000-04-29 13:19:31 +00:00
|
|
|
const moduledata_t *moddata;
|
|
|
|
|
|
|
|
KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
|
|
|
|
lf->filename));
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
if (linker_file_lookup_set(lf, "modmetadata_set", &start, &stop, 0) != 0) {
|
|
|
|
/*
|
|
|
|
* This fallback should be unnecessary, but if we get booted from
|
|
|
|
* boot2 instead of loader and we are missing our metadata then
|
|
|
|
* we have to try the best we can.
|
|
|
|
*/
|
|
|
|
if (lf == linker_kernel_file) {
|
|
|
|
start = SET_BEGIN(modmetadata_set);
|
|
|
|
stop = SET_LIMIT(modmetadata_set);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
|
|
|
if ((*mdp)->md_type != MDT_MODULE)
|
2001-03-22 07:14:42 +00:00
|
|
|
continue;
|
2001-06-13 10:58:39 +00:00
|
|
|
moddata = (*mdp)->md_data;
|
2001-03-22 07:14:42 +00:00
|
|
|
KLD_DPF(FILE, ("Registering module %s in %s\n",
|
|
|
|
moddata->name, lf->filename));
|
|
|
|
if (module_lookupbyname(moddata->name) != NULL) {
|
|
|
|
printf("Warning: module %s already exists\n", moddata->name);
|
|
|
|
continue; /* or return a error ? */
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
2001-03-22 07:14:42 +00:00
|
|
|
error = module_register(moddata, lf);
|
|
|
|
if (error)
|
|
|
|
printf("Module %s failed to register: %d\n", moddata->name, error);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
2001-03-22 07:14:42 +00:00
|
|
|
return 0;
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linker_init_kernel_modules(void)
|
|
|
|
{
|
|
|
|
linker_file_register_modules(linker_kernel_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0);
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
|
|
|
linker_load_file(const char* filename, linker_file_t* result)
|
|
|
|
{
|
|
|
|
linker_class_t lc;
|
|
|
|
linker_file_t lf;
|
1999-01-17 17:58:52 +00:00
|
|
|
int foundfile, error = 0;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2000-06-29 17:57:04 +00:00
|
|
|
/* Refuse to load modules if securelevel raised */
|
|
|
|
if (securelevel > 0)
|
|
|
|
return EPERM;
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
lf = linker_find_file_by_name(filename);
|
|
|
|
if (lf) {
|
|
|
|
KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
|
|
|
|
*result = lf;
|
|
|
|
lf->refs++;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
lf = NULL;
|
1999-01-17 17:58:52 +00:00
|
|
|
foundfile = 0;
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lc, &classes, link) {
|
2001-07-31 03:51:07 +00:00
|
|
|
KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
|
|
|
|
filename));
|
2000-04-29 13:19:31 +00:00
|
|
|
error = LINKER_LOAD_FILE(lc, filename, &lf);
|
1999-01-17 17:58:52 +00:00
|
|
|
/*
|
|
|
|
* If we got something other than ENOENT, then it exists but we cannot
|
|
|
|
* load it for some other reason.
|
|
|
|
*/
|
|
|
|
if (error != ENOENT)
|
|
|
|
foundfile = 1;
|
1997-05-07 16:05:47 +00:00
|
|
|
if (lf) {
|
2000-04-29 13:19:31 +00:00
|
|
|
linker_file_register_modules(lf);
|
1999-02-16 10:49:55 +00:00
|
|
|
linker_file_register_sysctls(lf);
|
1999-03-07 16:06:41 +00:00
|
|
|
linker_file_sysinit(lf);
|
2000-04-29 13:19:31 +00:00
|
|
|
lf->flags |= LINKER_FILE_LINKED;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
*result = lf;
|
1998-11-03 14:27:05 +00:00
|
|
|
error = 0;
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
1999-01-17 17:58:52 +00:00
|
|
|
/*
|
|
|
|
* Less than ideal, but tells the user whether it failed to load or
|
|
|
|
* the module was not found.
|
|
|
|
*/
|
|
|
|
if (foundfile)
|
|
|
|
error = ENOEXEC; /* Format not recognised (or unloadable) */
|
|
|
|
else
|
|
|
|
error = ENOENT; /* Nothing found */
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
/* XXX: function parameters are incomplete */
|
2001-06-18 15:09:33 +00:00
|
|
|
int
|
|
|
|
linker_reference_module(const char *modname, linker_file_t *result)
|
|
|
|
{
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
return linker_load_module(NULL, modname, NULL, NULL, result);
|
2001-06-18 15:09:33 +00:00
|
|
|
}
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
linker_file_t
|
|
|
|
linker_find_file_by_name(const char* filename)
|
|
|
|
{
|
|
|
|
linker_file_t lf = 0;
|
1998-11-03 14:27:05 +00:00
|
|
|
char *koname;
|
|
|
|
|
|
|
|
koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
|
|
|
|
if (koname == NULL)
|
|
|
|
goto out;
|
|
|
|
sprintf(koname, "%s.ko", filename);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_SHARED, 0, curthread);
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
1998-11-03 14:27:05 +00:00
|
|
|
if (!strcmp(lf->filename, koname))
|
|
|
|
break;
|
1997-05-07 16:05:47 +00:00
|
|
|
if (!strcmp(lf->filename, filename))
|
|
|
|
break;
|
1998-11-03 14:27:05 +00:00
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_RELEASE, 0, curthread);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
1998-11-03 14:27:05 +00:00
|
|
|
out:
|
|
|
|
if (koname)
|
|
|
|
free(koname, M_LINKER);
|
1997-05-07 16:05:47 +00:00
|
|
|
return lf;
|
|
|
|
}
|
|
|
|
|
|
|
|
linker_file_t
|
|
|
|
linker_find_file_by_id(int fileid)
|
|
|
|
{
|
|
|
|
linker_file_t lf = 0;
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_SHARED, 0, curthread);
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link)
|
1997-05-07 16:05:47 +00:00
|
|
|
if (lf->id == fileid)
|
|
|
|
break;
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_RELEASE, 0, curthread);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
return lf;
|
|
|
|
}
|
|
|
|
|
|
|
|
linker_file_t
|
2000-04-24 17:08:04 +00:00
|
|
|
linker_make_file(const char* pathname, linker_class_t lc)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
linker_file_t lf = 0;
|
1998-10-10 00:07:53 +00:00
|
|
|
const char *filename;
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
filename = linker_basename(pathname);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
|
2000-04-24 17:08:04 +00:00
|
|
|
lf = (linker_file_t) kobj_create((kobj_class_t) lc, M_LINKER, M_WAITOK);
|
1997-05-07 16:05:47 +00:00
|
|
|
if (!lf)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
lf->refs = 1;
|
|
|
|
lf->userrefs = 0;
|
1999-01-25 08:42:24 +00:00
|
|
|
lf->flags = 0;
|
2000-04-24 17:08:04 +00:00
|
|
|
lf->filename = linker_strdup(filename);
|
2001-11-18 18:19:35 +00:00
|
|
|
LINKER_GET_NEXT_FILE_ID(lf->id);
|
1997-05-07 16:05:47 +00:00
|
|
|
lf->ndeps = 0;
|
|
|
|
lf->deps = NULL;
|
|
|
|
STAILQ_INIT(&lf->common);
|
|
|
|
TAILQ_INIT(&lf->modules);
|
|
|
|
|
1999-08-20 00:18:07 +00:00
|
|
|
TAILQ_INSERT_TAIL(&linker_files, lf, link);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
out:
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_RELEASE, 0, curthread);
|
1997-05-07 16:05:47 +00:00
|
|
|
return lf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linker_file_unload(linker_file_t file)
|
|
|
|
{
|
|
|
|
module_t mod, next;
|
2000-04-29 13:19:31 +00:00
|
|
|
modlist_t ml, nextml;
|
1997-05-07 16:05:47 +00:00
|
|
|
struct common_symbol* cp;
|
|
|
|
int error = 0;
|
|
|
|
int i;
|
|
|
|
|
2000-06-29 17:57:04 +00:00
|
|
|
/* Refuse to unload modules if securelevel raised */
|
|
|
|
if (securelevel > 0)
|
|
|
|
return EPERM;
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
|
1997-05-07 16:05:47 +00:00
|
|
|
if (file->refs == 1) {
|
|
|
|
KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
|
|
|
|
/*
|
|
|
|
* Inform any modules associated with this file.
|
|
|
|
*/
|
|
|
|
for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
|
|
|
|
next = module_getfnext(mod);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give the module a chance to veto the unload.
|
|
|
|
*/
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = module_unload(mod)) != 0) {
|
1997-05-07 16:05:47 +00:00
|
|
|
KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
|
|
|
|
mod));
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_RELEASE, 0, curthread);
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
module_release(mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file->refs--;
|
|
|
|
if (file->refs > 0) {
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_RELEASE, 0, curthread);
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) {
|
|
|
|
nextml = TAILQ_NEXT(ml, link);
|
|
|
|
if (ml->container == file) {
|
|
|
|
TAILQ_REMOVE(&found_modules, ml, link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-25 08:42:24 +00:00
|
|
|
/* Don't try to run SYSUNINITs if we are unloaded due to a link error */
|
1999-02-16 10:49:55 +00:00
|
|
|
if (file->flags & LINKER_FILE_LINKED) {
|
1999-01-25 08:42:24 +00:00
|
|
|
linker_file_sysuninit(file);
|
1999-02-16 10:49:55 +00:00
|
|
|
linker_file_unregister_sysctls(file);
|
|
|
|
}
|
1998-11-10 08:49:28 +00:00
|
|
|
|
1999-08-20 00:18:07 +00:00
|
|
|
TAILQ_REMOVE(&linker_files, file, link);
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&lock, LK_RELEASE, 0, curthread);
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
if (file->deps) {
|
|
|
|
for (i = 0; i < file->ndeps; i++)
|
|
|
|
linker_file_unload(file->deps[i]);
|
|
|
|
free(file->deps, M_LINKER);
|
|
|
|
file->deps = NULL;
|
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
for (cp = STAILQ_FIRST(&file->common); cp;
|
|
|
|
cp = STAILQ_FIRST(&file->common)) {
|
2000-05-26 02:09:24 +00:00
|
|
|
STAILQ_REMOVE(&file->common, cp, common_symbol, link);
|
1997-05-07 16:05:47 +00:00
|
|
|
free(cp, M_LINKER);
|
|
|
|
}
|
|
|
|
|
2000-04-24 17:08:04 +00:00
|
|
|
LINKER_UNLOAD(file);
|
2000-04-29 13:19:31 +00:00
|
|
|
if (file->filename) {
|
|
|
|
free(file->filename, M_LINKER);
|
|
|
|
file->filename = NULL;
|
|
|
|
}
|
2000-04-24 17:08:04 +00:00
|
|
|
kobj_delete((kobj_t) file, M_LINKER);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-11-16 21:08:40 +00:00
|
|
|
linker_file_add_dependency(linker_file_t file, linker_file_t dep)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
linker_file_t* newdeps;
|
|
|
|
|
|
|
|
newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
|
2000-12-08 21:51:06 +00:00
|
|
|
M_LINKER, M_WAITOK | M_ZERO);
|
1997-05-07 16:05:47 +00:00
|
|
|
if (newdeps == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
if (file->deps) {
|
|
|
|
bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
|
|
|
|
free(file->deps, M_LINKER);
|
|
|
|
}
|
|
|
|
file->deps = newdeps;
|
|
|
|
file->deps[file->ndeps] = dep;
|
|
|
|
file->ndeps++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
/*
|
|
|
|
* Locate a linker set and its contents.
|
|
|
|
* This is a helper function to avoid linker_if.h exposure elsewhere.
|
|
|
|
* Note: firstp and lastp are really void ***
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
linker_file_lookup_set(linker_file_t file, const char *name,
|
|
|
|
void *firstp, void *lastp, int *countp)
|
|
|
|
{
|
|
|
|
|
|
|
|
return LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
|
|
|
|
}
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
caddr_t
|
|
|
|
linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
|
|
|
|
{
|
1999-01-27 23:45:44 +00:00
|
|
|
c_linker_sym_t sym;
|
1998-08-12 08:44:21 +00:00
|
|
|
linker_symval_t symval;
|
1997-05-07 16:05:47 +00:00
|
|
|
caddr_t address;
|
|
|
|
size_t common_size = 0;
|
|
|
|
int i;
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
|
1997-05-07 16:05:47 +00:00
|
|
|
file, name, deps));
|
|
|
|
|
2000-04-24 17:08:04 +00:00
|
|
|
if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
|
|
|
|
LINKER_SYMBOL_VALUES(file, sym, &symval);
|
1998-08-12 08:44:21 +00:00
|
|
|
if (symval.value == 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
/*
|
2001-11-16 21:08:40 +00:00
|
|
|
* For commons, first look them up in the dependencies and
|
1997-05-07 16:05:47 +00:00
|
|
|
* only allocate space if not found there.
|
|
|
|
*/
|
1998-08-12 08:44:21 +00:00
|
|
|
common_size = symval.size;
|
1998-10-10 00:07:53 +00:00
|
|
|
else {
|
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
|
1998-08-12 08:44:21 +00:00
|
|
|
return symval.value;
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
1998-08-12 08:44:21 +00:00
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
|
1999-01-19 22:26:46 +00:00
|
|
|
if (deps) {
|
1997-05-07 16:05:47 +00:00
|
|
|
for (i = 0; i < file->ndeps; i++) {
|
|
|
|
address = linker_file_lookup_symbol(file->deps[i], name, 0);
|
1998-10-10 00:07:53 +00:00
|
|
|
if (address) {
|
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
|
1997-05-07 16:05:47 +00:00
|
|
|
return address;
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
1999-01-19 22:26:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
if (common_size > 0) {
|
|
|
|
/*
|
|
|
|
* This is a common symbol which was not found in the
|
2001-11-16 21:08:40 +00:00
|
|
|
* dependencies. We maintain a simple common symbol table in
|
1997-05-07 16:05:47 +00:00
|
|
|
* the file object.
|
|
|
|
*/
|
|
|
|
struct common_symbol* cp;
|
|
|
|
|
2001-02-04 16:08:18 +00:00
|
|
|
STAILQ_FOREACH(cp, &file->common, link)
|
1998-10-10 00:07:53 +00:00
|
|
|
if (!strcmp(cp->name, name)) {
|
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
|
1997-05-07 16:05:47 +00:00
|
|
|
return cp->address;
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Round the symbol size up to align.
|
|
|
|
*/
|
|
|
|
common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
|
|
|
|
cp = malloc(sizeof(struct common_symbol)
|
|
|
|
+ common_size
|
|
|
|
+ strlen(name) + 1,
|
2000-12-08 21:51:06 +00:00
|
|
|
M_LINKER, M_WAITOK | M_ZERO);
|
1998-10-10 00:07:53 +00:00
|
|
|
if (!cp) {
|
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
|
1997-05-07 16:05:47 +00:00
|
|
|
return 0;
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
cp->address = (caddr_t) (cp + 1);
|
|
|
|
cp->name = cp->address + common_size;
|
|
|
|
strcpy(cp->name, name);
|
|
|
|
bzero(cp->address, common_size);
|
|
|
|
STAILQ_INSERT_TAIL(&file->common, cp, link);
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
|
1997-05-07 16:05:47 +00:00
|
|
|
return cp->address;
|
|
|
|
}
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
|
1997-05-07 16:05:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
#ifdef DDB
|
|
|
|
/*
|
|
|
|
* DDB Helpers. DDB has to look across multiple files with their own
|
|
|
|
* symbol tables and string tables.
|
|
|
|
*
|
|
|
|
* Note that we do not obey list locking protocols here. We really don't
|
|
|
|
* need DDB to hang because somebody's got the lock held. We'll take the
|
|
|
|
* chance that the files list is inconsistant instead.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
1999-01-27 23:45:44 +00:00
|
|
|
linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
|
1998-10-10 00:07:53 +00:00
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
2000-04-24 17:08:04 +00:00
|
|
|
if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
|
1998-10-10 00:07:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-01-27 23:45:44 +00:00
|
|
|
linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
|
1998-10-10 00:07:53 +00:00
|
|
|
{
|
|
|
|
linker_file_t lf;
|
1999-08-24 00:38:52 +00:00
|
|
|
u_long off = (uintptr_t)value;
|
1998-10-10 00:07:53 +00:00
|
|
|
u_long diff, bestdiff;
|
1999-01-27 23:45:44 +00:00
|
|
|
c_linker_sym_t best;
|
|
|
|
c_linker_sym_t es;
|
1998-10-10 00:07:53 +00:00
|
|
|
|
|
|
|
best = 0;
|
|
|
|
bestdiff = off;
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
2000-04-24 17:08:04 +00:00
|
|
|
if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
|
1998-10-10 00:07:53 +00:00
|
|
|
continue;
|
|
|
|
if (es != 0 && diff < bestdiff) {
|
|
|
|
best = es;
|
|
|
|
bestdiff = diff;
|
|
|
|
}
|
|
|
|
if (bestdiff == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (best) {
|
|
|
|
*sym = best;
|
|
|
|
*diffp = bestdiff;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
*sym = 0;
|
|
|
|
*diffp = off;
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-01-27 23:45:44 +00:00
|
|
|
linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
|
1998-10-10 00:07:53 +00:00
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
2000-04-24 17:08:04 +00:00
|
|
|
if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
|
1998-10-10 00:07:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
/*
|
|
|
|
* Syscalls.
|
|
|
|
*/
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
kldload(struct thread* td, struct kldload_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2001-09-11 01:09:24 +00:00
|
|
|
char *kldname, *modname;
|
2001-09-01 19:04:37 +00:00
|
|
|
char *pathname = NULL;
|
1997-05-07 16:05:47 +00:00
|
|
|
linker_file_t lf;
|
|
|
|
int error = 0;
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = -1;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2000-06-29 17:57:04 +00:00
|
|
|
if (securelevel > 0) /* redundant, but that's OK */
|
1997-05-07 16:05:47 +00:00
|
|
|
return EPERM;
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
|
|
|
|
2001-11-01 20:56:57 +00:00
|
|
|
if ((error = suser_xxx(td->td_proc->p_ucred, NULL, 0)) != 0)
|
2001-09-01 19:04:37 +00:00
|
|
|
goto out;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
|
|
|
|
if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
/*
|
|
|
|
* If path do not contain qualified name or any dot in it (kldname.ko, or
|
|
|
|
* kldname.ver.ko) treat it as interface name.
|
|
|
|
*/
|
|
|
|
if (index(pathname, '/') || index(pathname, '.')) {
|
|
|
|
kldname = pathname;
|
|
|
|
modname = NULL;
|
|
|
|
} else {
|
|
|
|
kldname = NULL;
|
|
|
|
modname = pathname;
|
1999-01-05 20:24:28 +00:00
|
|
|
}
|
2001-09-11 01:09:24 +00:00
|
|
|
error = linker_load_module(kldname, modname, NULL, NULL, &lf);
|
|
|
|
if (error)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
lf->userrefs++;
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = lf->id;
|
1998-10-10 00:07:53 +00:00
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
out:
|
2000-04-29 13:19:31 +00:00
|
|
|
if (pathname)
|
|
|
|
free(pathname, M_TEMP);
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
kldunload(struct thread* td, struct kldunload_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
int error = 0;
|
|
|
|
|
2000-06-29 17:57:04 +00:00
|
|
|
if (securelevel > 0) /* redundant, but that's OK */
|
1997-05-07 16:05:47 +00:00
|
|
|
return EPERM;
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
|
|
|
|
2001-11-01 20:56:57 +00:00
|
|
|
if ((error = suser_xxx(td->td_proc->p_ucred, NULL, 0)) != 0)
|
2001-09-01 19:04:37 +00:00
|
|
|
goto out;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
lf = linker_find_file_by_id(SCARG(uap, fileid));
|
|
|
|
if (lf) {
|
|
|
|
KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
|
|
|
|
if (lf->userrefs == 0) {
|
2000-04-29 13:19:31 +00:00
|
|
|
printf("kldunload: attempt to unload file that was loaded by the kernel\n");
|
1997-05-07 16:05:47 +00:00
|
|
|
error = EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
1999-01-23 03:45:22 +00:00
|
|
|
lf->userrefs--;
|
1997-05-07 16:05:47 +00:00
|
|
|
error = linker_file_unload(lf);
|
1999-01-19 16:26:32 +00:00
|
|
|
if (error)
|
1999-01-23 03:45:22 +00:00
|
|
|
lf->userrefs++;
|
2001-09-01 19:04:37 +00:00
|
|
|
} else {
|
1997-05-07 16:05:47 +00:00
|
|
|
error = ENOENT;
|
2001-09-01 19:04:37 +00:00
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
out:
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
kldfind(struct thread* td, struct kldfind_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2000-04-29 13:19:31 +00:00
|
|
|
char* pathname;
|
|
|
|
const char *filename;
|
1997-05-07 16:05:47 +00:00
|
|
|
linker_file_t lf;
|
|
|
|
int error = 0;
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = -1;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
|
|
|
|
if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
filename = linker_basename(pathname);
|
1999-01-05 20:24:28 +00:00
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
lf = linker_find_file_by_name(filename);
|
1997-05-07 16:05:47 +00:00
|
|
|
if (lf)
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = lf->id;
|
1997-05-07 16:05:47 +00:00
|
|
|
else
|
|
|
|
error = ENOENT;
|
1998-10-10 00:07:53 +00:00
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
out:
|
2000-04-29 13:19:31 +00:00
|
|
|
if (pathname)
|
|
|
|
free(pathname, M_TEMP);
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
kldnext(struct thread* td, struct kldnext_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
int error = 0;
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
if (SCARG(uap, fileid) == 0) {
|
1999-08-20 00:18:07 +00:00
|
|
|
if (TAILQ_FIRST(&linker_files))
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = TAILQ_FIRST(&linker_files)->id;
|
1997-05-07 16:05:47 +00:00
|
|
|
else
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = 0;
|
2001-09-01 19:04:37 +00:00
|
|
|
goto out;
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
1998-10-10 00:07:53 +00:00
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
lf = linker_find_file_by_id(SCARG(uap, fileid));
|
|
|
|
if (lf) {
|
|
|
|
if (TAILQ_NEXT(lf, link))
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = TAILQ_NEXT(lf, link)->id;
|
1997-05-07 16:05:47 +00:00
|
|
|
else
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = 0;
|
2001-09-01 19:04:37 +00:00
|
|
|
} else {
|
1997-05-07 16:05:47 +00:00
|
|
|
error = ENOENT;
|
2001-09-01 19:04:37 +00:00
|
|
|
}
|
|
|
|
out:
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
kldstat(struct thread* td, struct kldstat_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
int error = 0;
|
|
|
|
int version;
|
|
|
|
struct kld_file_stat* stat;
|
|
|
|
int namelen;
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
lf = linker_find_file_by_id(SCARG(uap, fileid));
|
|
|
|
if (!lf) {
|
|
|
|
error = ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = SCARG(uap, stat);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the version of the user's structure.
|
|
|
|
*/
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
if (version != sizeof(struct kld_file_stat)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
namelen = strlen(lf->filename) + 1;
|
|
|
|
if (namelen > MAXPATHLEN)
|
|
|
|
namelen = MAXPATHLEN;
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = 0;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
out:
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
kldfirstmod(struct thread* td, struct kldfirstmod_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
int error = 0;
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
1997-05-07 16:05:47 +00:00
|
|
|
lf = linker_find_file_by_id(SCARG(uap, fileid));
|
|
|
|
if (lf) {
|
|
|
|
if (TAILQ_FIRST(&lf->modules))
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
|
1997-05-07 16:05:47 +00:00
|
|
|
else
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = 0;
|
2001-09-01 19:04:37 +00:00
|
|
|
} else {
|
1997-05-07 16:05:47 +00:00
|
|
|
error = ENOENT;
|
2001-09-01 19:04:37 +00:00
|
|
|
}
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1998-11-11 13:04:40 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
kldsym(struct thread *td, struct kldsym_args *uap)
|
1998-11-11 13:04:40 +00:00
|
|
|
{
|
|
|
|
char *symstr = NULL;
|
1999-01-27 23:45:44 +00:00
|
|
|
c_linker_sym_t sym;
|
1998-11-11 13:04:40 +00:00
|
|
|
linker_symval_t symval;
|
|
|
|
linker_file_t lf;
|
|
|
|
struct kld_sym_lookup lookup;
|
|
|
|
int error = 0;
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
|
|
|
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
|
1998-11-11 13:04:40 +00:00
|
|
|
goto out;
|
|
|
|
if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
|
1998-11-11 13:04:40 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (SCARG(uap, fileid) != 0) {
|
|
|
|
lf = linker_find_file_by_id(SCARG(uap, fileid));
|
|
|
|
if (lf == NULL) {
|
|
|
|
error = ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2000-04-24 17:08:04 +00:00
|
|
|
if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
|
|
|
|
LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
|
1999-08-24 00:38:52 +00:00
|
|
|
lookup.symvalue = (uintptr_t)symval.value;
|
1998-11-11 13:04:40 +00:00
|
|
|
lookup.symsize = symval.size;
|
|
|
|
error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
|
|
|
|
} else
|
|
|
|
error = ENOENT;
|
|
|
|
} else {
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
2000-04-24 17:08:04 +00:00
|
|
|
if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
|
|
|
|
LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
|
1999-08-24 00:38:52 +00:00
|
|
|
lookup.symvalue = (uintptr_t)symval.value;
|
1998-11-11 13:04:40 +00:00
|
|
|
lookup.symsize = symval.size;
|
|
|
|
error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!lf)
|
|
|
|
error = ENOENT;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (symstr)
|
|
|
|
free(symstr, M_TEMP);
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return (error);
|
1998-11-11 13:04:40 +00:00
|
|
|
}
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
/*
|
|
|
|
* Preloaded module support
|
|
|
|
*/
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
static modlist_t
|
2001-03-22 08:58:45 +00:00
|
|
|
modlist_lookup(const char *name, int ver)
|
2000-04-29 13:19:31 +00:00
|
|
|
{
|
|
|
|
modlist_t mod;
|
|
|
|
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(mod, &found_modules, link) {
|
2001-03-22 08:58:45 +00:00
|
|
|
if (strcmp(mod->name, name) == 0 && (ver == 0 || mod->version == ver))
|
2000-04-29 13:19:31 +00:00
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
static modlist_t
|
|
|
|
modlist_lookup2(const char *name, struct mod_depend *verinfo)
|
|
|
|
{
|
|
|
|
modlist_t mod, bestmod;
|
|
|
|
int ver;
|
|
|
|
|
|
|
|
if (verinfo == NULL)
|
|
|
|
return modlist_lookup(name, 0);
|
|
|
|
bestmod = NULL;
|
|
|
|
for (mod = TAILQ_FIRST(&found_modules); mod; mod = TAILQ_NEXT(mod, link)) {
|
|
|
|
if (strcmp(mod->name, name) != 0)
|
|
|
|
continue;
|
|
|
|
ver = mod->version;
|
|
|
|
if (ver == verinfo->md_ver_preferred)
|
|
|
|
return mod;
|
|
|
|
if (ver >= verinfo->md_ver_minimum &&
|
|
|
|
ver <= verinfo->md_ver_maximum &&
|
|
|
|
ver > bestmod->version)
|
|
|
|
bestmod = mod;
|
|
|
|
}
|
|
|
|
return bestmod;
|
|
|
|
}
|
|
|
|
|
2001-03-22 08:58:45 +00:00
|
|
|
static modlist_t
|
2001-06-20 16:08:45 +00:00
|
|
|
modlist_newmodule(const char *modname, int version, linker_file_t container)
|
2001-03-22 08:58:45 +00:00
|
|
|
{
|
|
|
|
modlist_t mod;
|
|
|
|
|
|
|
|
mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT);
|
|
|
|
if (mod == NULL)
|
|
|
|
panic("no memory for module list");
|
|
|
|
bzero(mod, sizeof(*mod));
|
|
|
|
mod->container = container;
|
|
|
|
mod->name = modname;
|
|
|
|
mod->version = version;
|
|
|
|
TAILQ_INSERT_TAIL(&found_modules, mod, link);
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
/*
|
|
|
|
* This routine is cheap and nasty but will work for data pointers.
|
|
|
|
*/
|
|
|
|
static void *
|
2001-06-20 16:08:45 +00:00
|
|
|
linker_reloc_ptr(linker_file_t lf, const void *offset)
|
2000-04-29 13:19:31 +00:00
|
|
|
{
|
|
|
|
return lf->address + (uintptr_t)offset;
|
|
|
|
}
|
|
|
|
|
2001-03-22 08:58:45 +00:00
|
|
|
/*
|
|
|
|
* Dereference MDT_VERSION metadata into module name and version
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
linker_mdt_version(linker_file_t lf, struct mod_metadata *mp,
|
2001-06-20 16:08:45 +00:00
|
|
|
const char **modname, int *version)
|
2001-03-22 08:58:45 +00:00
|
|
|
{
|
|
|
|
struct mod_version *mvp;
|
|
|
|
|
|
|
|
if (modname)
|
|
|
|
*modname = linker_reloc_ptr(lf, mp->md_cval);
|
|
|
|
if (version) {
|
|
|
|
mvp = linker_reloc_ptr(lf, mp->md_data);
|
|
|
|
*version = mvp->mv_version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dereference MDT_DEPEND metadata into module name and mod_depend structure
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
linker_mdt_depend(linker_file_t lf, struct mod_metadata *mp,
|
2001-06-20 16:08:45 +00:00
|
|
|
const char **modname, struct mod_depend **verinfo)
|
2001-03-22 08:58:45 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (modname)
|
|
|
|
*modname = linker_reloc_ptr(lf, mp->md_cval);
|
|
|
|
if (verinfo)
|
|
|
|
*verinfo = linker_reloc_ptr(lf, mp->md_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-06-13 10:58:39 +00:00
|
|
|
linker_addmodules(linker_file_t lf, struct mod_metadata **start,
|
|
|
|
struct mod_metadata **stop, int preload)
|
2001-03-22 08:58:45 +00:00
|
|
|
{
|
2001-06-13 10:58:39 +00:00
|
|
|
struct mod_metadata *mp, **mdp;
|
2001-06-20 16:08:45 +00:00
|
|
|
const char *modname;
|
2001-06-13 10:58:39 +00:00
|
|
|
int ver;
|
2001-03-22 08:58:45 +00:00
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
2001-03-22 08:58:45 +00:00
|
|
|
if (preload)
|
2001-06-13 10:58:39 +00:00
|
|
|
mp = *mdp;
|
2001-03-22 08:58:45 +00:00
|
|
|
else
|
2001-06-13 10:58:39 +00:00
|
|
|
mp = linker_reloc_ptr(lf, *mdp);
|
2001-03-22 08:58:45 +00:00
|
|
|
if (mp->md_type != MDT_VERSION)
|
|
|
|
continue;
|
|
|
|
if (preload) {
|
|
|
|
modname = mp->md_cval;
|
|
|
|
ver = ((struct mod_version*)mp->md_data)->mv_version;
|
|
|
|
} else
|
|
|
|
linker_mdt_version(lf, mp, &modname, &ver);
|
|
|
|
if (modlist_lookup(modname, ver) != NULL) {
|
|
|
|
printf("module %s already present!\n", modname);
|
|
|
|
/* XXX what can we do? this is a build error. :-( */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
modlist_newmodule(modname, ver, lf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
static void
|
|
|
|
linker_preload(void* arg)
|
|
|
|
{
|
|
|
|
caddr_t modptr;
|
2001-06-20 16:08:45 +00:00
|
|
|
const char *modname, *nmodname;
|
1998-10-10 02:29:07 +00:00
|
|
|
char *modtype;
|
1998-10-10 00:07:53 +00:00
|
|
|
linker_file_t lf;
|
|
|
|
linker_class_t lc;
|
2001-03-22 07:14:42 +00:00
|
|
|
int error;
|
2000-04-29 13:19:31 +00:00
|
|
|
linker_file_list_t loaded_files;
|
|
|
|
linker_file_list_t depended_files;
|
2000-08-02 21:08:53 +00:00
|
|
|
struct mod_metadata *mp, *nmp;
|
2001-06-13 10:58:39 +00:00
|
|
|
struct mod_metadata **start, **stop, **mdp, **nmdp;
|
2001-03-22 08:58:45 +00:00
|
|
|
struct mod_depend *verinfo;
|
2001-06-13 10:58:39 +00:00
|
|
|
int nver;
|
2000-04-29 13:19:31 +00:00
|
|
|
int resolves;
|
|
|
|
modlist_t mod;
|
2001-06-13 10:58:39 +00:00
|
|
|
struct sysinit **si_start, **si_stop;
|
2000-04-29 13:19:31 +00:00
|
|
|
|
|
|
|
TAILQ_INIT(&loaded_files);
|
|
|
|
TAILQ_INIT(&depended_files);
|
|
|
|
TAILQ_INIT(&found_modules);
|
|
|
|
error = 0;
|
1998-10-10 00:07:53 +00:00
|
|
|
|
|
|
|
modptr = NULL;
|
|
|
|
while ((modptr = preload_search_next_name(modptr)) != NULL) {
|
|
|
|
modname = (char *)preload_search_info(modptr, MODINFO_NAME);
|
1998-10-10 02:29:07 +00:00
|
|
|
modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
|
1998-10-10 00:07:53 +00:00
|
|
|
if (modname == NULL) {
|
1998-10-24 18:35:09 +00:00
|
|
|
printf("Preloaded module at %p does not have a name!\n", modptr);
|
1998-10-10 00:07:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
1998-10-10 02:29:07 +00:00
|
|
|
if (modtype == NULL) {
|
1998-10-24 18:35:09 +00:00
|
|
|
printf("Preloaded module at %p does not have a type!\n", modptr);
|
1998-10-10 02:29:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
1998-10-24 18:35:09 +00:00
|
|
|
printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
|
1998-10-10 00:07:53 +00:00
|
|
|
lf = NULL;
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lc, &classes, link) {
|
2000-04-29 13:19:31 +00:00
|
|
|
error = LINKER_LINK_PRELOAD(lc, modname, &lf);
|
1998-10-10 00:07:53 +00:00
|
|
|
if (error) {
|
|
|
|
lf = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
if (lf)
|
|
|
|
TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First get a list of stuff in the kernel.
|
|
|
|
*/
|
2001-06-13 10:58:39 +00:00
|
|
|
if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, &stop,
|
|
|
|
NULL) == 0)
|
|
|
|
linker_addmodules(linker_kernel_file, start, stop, 1);
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
/*
|
|
|
|
* this is a once-off kinky bubble sort
|
|
|
|
* resolve relocation dependency requirements
|
|
|
|
*/
|
|
|
|
restart:
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lf, &loaded_files, loaded) {
|
2001-06-13 10:58:39 +00:00
|
|
|
error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
|
2000-04-29 13:19:31 +00:00
|
|
|
/*
|
|
|
|
* First, look to see if we would successfully link with this stuff.
|
|
|
|
*/
|
|
|
|
resolves = 1; /* unless we know otherwise */
|
2001-06-13 10:58:39 +00:00
|
|
|
if (!error) {
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
|
|
|
mp = linker_reloc_ptr(lf, *mdp);
|
2000-04-29 13:19:31 +00:00
|
|
|
if (mp->md_type != MDT_DEPEND)
|
|
|
|
continue;
|
2001-03-22 08:58:45 +00:00
|
|
|
linker_mdt_depend(lf, mp, &modname, &verinfo);
|
2001-06-13 10:58:39 +00:00
|
|
|
for (nmdp = start; nmdp < stop; nmdp++) {
|
|
|
|
nmp = linker_reloc_ptr(lf, *nmdp);
|
2000-08-02 21:08:53 +00:00
|
|
|
if (nmp->md_type != MDT_VERSION)
|
|
|
|
continue;
|
2001-03-22 08:58:45 +00:00
|
|
|
linker_mdt_version(lf, nmp, &nmodname, NULL);
|
2000-08-02 21:08:53 +00:00
|
|
|
nmodname = linker_reloc_ptr(lf, nmp->md_cval);
|
|
|
|
if (strcmp(modname, nmodname) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2001-06-13 10:58:39 +00:00
|
|
|
if (nmdp < stop) /* it's a self reference */
|
2000-08-02 21:08:53 +00:00
|
|
|
continue;
|
2001-09-11 01:09:24 +00:00
|
|
|
if (modlist_lookup2(modname, verinfo) == NULL) {
|
2000-04-29 13:19:31 +00:00
|
|
|
/* ok, the module isn't here yet, we are not finished */
|
|
|
|
resolves = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* OK, if we found our modules, we can link. So, "provide" the
|
|
|
|
* modules inside and add it to the end of the link order list.
|
|
|
|
*/
|
|
|
|
if (resolves) {
|
2001-06-13 10:58:39 +00:00
|
|
|
if (!error) {
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
|
|
|
mp = linker_reloc_ptr(lf, *mdp);
|
2000-04-29 13:19:31 +00:00
|
|
|
if (mp->md_type != MDT_VERSION)
|
|
|
|
continue;
|
2001-03-22 08:58:45 +00:00
|
|
|
linker_mdt_version(lf, mp, &modname, &nver);
|
|
|
|
if (modlist_lookup(modname, nver) != NULL) {
|
2000-04-29 13:19:31 +00:00
|
|
|
printf("module %s already present!\n", modname);
|
|
|
|
linker_file_unload(lf);
|
|
|
|
TAILQ_REMOVE(&loaded_files, lf, loaded);
|
|
|
|
goto restart; /* we changed the tailq next ptr */
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
2001-03-22 08:58:45 +00:00
|
|
|
modlist_newmodule(modname, nver, lf);
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
TAILQ_REMOVE(&loaded_files, lf, loaded);
|
|
|
|
TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
|
|
|
|
/*
|
|
|
|
* Since we provided modules, we need to restart the sort so
|
|
|
|
* that the previous files that depend on us have a chance.
|
|
|
|
* Also, we've busted the tailq next pointer with the REMOVE.
|
|
|
|
*/
|
|
|
|
goto restart;
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, we check to see what could not be resolved..
|
|
|
|
*/
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lf, &loaded_files, loaded) {
|
2000-04-29 13:19:31 +00:00
|
|
|
printf("KLD file %s is missing dependencies\n", lf->filename);
|
|
|
|
linker_file_unload(lf);
|
|
|
|
TAILQ_REMOVE(&loaded_files, lf, loaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We made it. Finish off the linking in the order we determined.
|
|
|
|
*/
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(lf, &depended_files, loaded) {
|
2000-04-29 13:19:31 +00:00
|
|
|
if (linker_kernel_file) {
|
|
|
|
linker_kernel_file->refs++;
|
2001-11-16 21:08:40 +00:00
|
|
|
error = linker_file_add_dependency(lf, linker_kernel_file);
|
2000-04-29 13:19:31 +00:00
|
|
|
if (error)
|
|
|
|
panic("cannot add dependency");
|
|
|
|
}
|
|
|
|
lf->userrefs++; /* so we can (try to) kldunload it */
|
2001-06-13 10:58:39 +00:00
|
|
|
error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
|
|
|
|
if (!error) {
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
|
|
|
mp = linker_reloc_ptr(lf, *mdp);
|
2000-04-29 13:19:31 +00:00
|
|
|
if (mp->md_type != MDT_DEPEND)
|
|
|
|
continue;
|
2001-03-22 08:58:45 +00:00
|
|
|
linker_mdt_depend(lf, mp, &modname, &verinfo);
|
2001-09-11 01:09:24 +00:00
|
|
|
mod = modlist_lookup2(modname, verinfo);
|
2000-04-29 13:19:31 +00:00
|
|
|
mod->container->refs++;
|
2001-11-16 21:08:40 +00:00
|
|
|
error = linker_file_add_dependency(lf, mod->container);
|
2000-04-29 13:19:31 +00:00
|
|
|
if (error)
|
|
|
|
panic("cannot add dependency");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
/*
|
|
|
|
* Now do relocation etc using the symbol search paths established by
|
|
|
|
* the dependencies
|
|
|
|
*/
|
2000-04-29 13:19:31 +00:00
|
|
|
error = LINKER_LINK_PRELOAD_FINISH(lf);
|
|
|
|
if (error) {
|
|
|
|
printf("KLD file %s - could not finalize loading\n", lf->filename);
|
|
|
|
linker_file_unload(lf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-03-22 07:14:42 +00:00
|
|
|
linker_file_register_modules(lf);
|
2001-06-13 10:58:39 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0)
|
|
|
|
sysinit_add(si_start, si_stop);
|
2000-04-29 13:19:31 +00:00
|
|
|
linker_file_register_sysctls(lf);
|
|
|
|
lf->flags |= LINKER_FILE_LINKED;
|
|
|
|
}
|
|
|
|
/* woohoo! we made it! */
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for a not-loaded module by name.
|
|
|
|
*
|
|
|
|
* Modules may be found in the following locations:
|
|
|
|
*
|
|
|
|
* - preloaded (result is just the module name)
|
|
|
|
* - on disk (result is full path to module)
|
|
|
|
*
|
|
|
|
* If the module name is qualified in any way (contains path, etc.)
|
|
|
|
* the we simply return a copy of it.
|
|
|
|
*
|
|
|
|
* The search path can be manipulated via sysctl. Note that we use the ';'
|
|
|
|
* character as a separator to be consistent with the bootloader.
|
|
|
|
*/
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
static char linker_hintfile[] = "linker.hints";
|
2001-09-12 00:50:23 +00:00
|
|
|
static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules;/modules";
|
1998-10-10 00:07:53 +00:00
|
|
|
|
|
|
|
SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
|
|
|
|
sizeof(linker_path), "module load search path");
|
|
|
|
|
2001-06-06 22:17:08 +00:00
|
|
|
TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
|
2000-12-28 08:14:58 +00:00
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
static char *linker_ext_list[] = {
|
|
|
|
"",
|
2001-09-11 01:09:24 +00:00
|
|
|
".ko",
|
2000-04-29 13:19:31 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
/*
|
|
|
|
* Check if file actually exists either with or without extension listed
|
|
|
|
* in the linker_ext_list.
|
|
|
|
* (probably should be generic for the rest of the kernel)
|
|
|
|
*/
|
2000-04-29 13:19:31 +00:00
|
|
|
static char *
|
2001-09-11 01:09:24 +00:00
|
|
|
linker_lookup_file(const char *path, int pathlen,
|
|
|
|
const char *name, int namelen, struct vattr *vap)
|
1998-10-10 00:07:53 +00:00
|
|
|
{
|
|
|
|
struct nameidata nd;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread; /* XXX */
|
2001-09-12 00:50:23 +00:00
|
|
|
char *result, **cpp, *sep;
|
|
|
|
int error, len, extlen, reclen, flags;
|
1998-10-10 00:07:53 +00:00
|
|
|
enum vtype type;
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
extlen = 0;
|
|
|
|
for (cpp = linker_ext_list; *cpp; cpp++) {
|
|
|
|
len = strlen(*cpp);
|
|
|
|
if (len > extlen)
|
|
|
|
extlen = len;
|
|
|
|
}
|
|
|
|
extlen++; /* trailing '\0' */
|
2001-09-12 00:50:23 +00:00
|
|
|
sep = (path[pathlen - 1] != '/') ? "/" : "";
|
2001-09-11 01:09:24 +00:00
|
|
|
|
2001-09-12 00:50:23 +00:00
|
|
|
reclen = pathlen + strlen(sep) + namelen + extlen + 1;
|
|
|
|
result = malloc(reclen, M_LINKER, M_WAITOK);
|
2001-09-11 01:09:24 +00:00
|
|
|
for (cpp = linker_ext_list; *cpp; cpp++) {
|
2001-09-12 00:50:23 +00:00
|
|
|
snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
|
|
|
|
namelen, name, *cpp);
|
2001-09-11 01:09:24 +00:00
|
|
|
/*
|
|
|
|
* Attempt to open the file, and return the path if we succeed
|
|
|
|
* and it's a regular file.
|
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
|
2001-09-11 01:09:24 +00:00
|
|
|
flags = FREAD;
|
|
|
|
error = vn_open(&nd, &flags, 0);
|
|
|
|
if (error == 0) {
|
|
|
|
NDFREE(&nd, NDF_ONLY_PNBUF);
|
|
|
|
type = nd.ni_vp->v_type;
|
|
|
|
if (vap)
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_GETATTR(nd.ni_vp, vap, td->td_proc->p_ucred, td);
|
|
|
|
VOP_UNLOCK(nd.ni_vp, 0, td);
|
|
|
|
vn_close(nd.ni_vp, FREAD, td->td_proc->p_ucred, td);
|
2001-09-11 01:09:24 +00:00
|
|
|
if (type == VREG)
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(result, M_LINKER);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INT_ALIGN(base, ptr) ptr = \
|
|
|
|
(base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup KLD which contains requested module in the "linker.hints" file.
|
|
|
|
* If version specification is available, then try to find the best KLD.
|
|
|
|
* Otherwise just find the latest one.
|
2002-02-10 21:29:02 +00:00
|
|
|
*
|
|
|
|
* XXX: Vnode locking here is hosed; lock should be held for calls to
|
|
|
|
* VOP_GETATTR() and vn_rdwr().
|
2001-09-11 01:09:24 +00:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
linker_hints_lookup(const char *path, int pathlen,
|
|
|
|
const char *modname, int modnamelen,
|
|
|
|
struct mod_depend *verinfo)
|
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread; /* XXX */
|
|
|
|
struct ucred *cred = td ? td->td_proc->p_ucred : NULL;
|
2001-09-11 01:09:24 +00:00
|
|
|
struct nameidata nd;
|
|
|
|
struct vattr vattr, mattr;
|
|
|
|
u_char *hints = NULL;
|
2001-09-12 00:50:23 +00:00
|
|
|
u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
|
2001-09-11 01:09:24 +00:00
|
|
|
int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
|
|
|
|
|
|
|
|
result = NULL;
|
|
|
|
bestver = found = 0;
|
|
|
|
|
2001-09-12 00:50:23 +00:00
|
|
|
sep = (path[pathlen - 1] != '/') ? "/" : "";
|
|
|
|
reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
|
|
|
|
strlen(sep) + 1;
|
2001-09-11 01:09:24 +00:00
|
|
|
pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
|
2001-09-12 00:50:23 +00:00
|
|
|
snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, linker_hintfile);
|
2001-09-11 01:09:24 +00:00
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
|
2001-09-11 01:09:24 +00:00
|
|
|
flags = FREAD;
|
|
|
|
error = vn_open(&nd, &flags, 0);
|
|
|
|
if (error)
|
|
|
|
goto bad;
|
|
|
|
NDFREE(&nd, NDF_ONLY_PNBUF);
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(nd.ni_vp, 0, td);
|
2001-09-11 01:09:24 +00:00
|
|
|
if (nd.ni_vp->v_type != VREG)
|
|
|
|
goto bad;
|
|
|
|
best = cp = NULL;
|
2001-09-12 08:38:13 +00:00
|
|
|
error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td);
|
2001-09-11 01:09:24 +00:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
|
|
|
/*
|
|
|
|
* XXX: we need to limit this number to some reasonable value
|
|
|
|
*/
|
|
|
|
if (vattr.va_size > 100 * 1024) {
|
|
|
|
printf("hints file too large %ld\n", (long)vattr.va_size);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
|
|
|
|
if (hints == NULL)
|
|
|
|
goto bad;
|
|
|
|
error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
|
2001-09-12 08:38:13 +00:00
|
|
|
UIO_SYSSPACE, IO_NODELOCKED, cred, &reclen, td);
|
2001-09-11 01:09:24 +00:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_close(nd.ni_vp, FREAD, cred, td);
|
2001-09-11 01:09:24 +00:00
|
|
|
nd.ni_vp = NULL;
|
|
|
|
if (reclen != 0) {
|
|
|
|
printf("can't read %d\n", reclen);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
intp = (int*)hints;
|
|
|
|
ival = *intp++;
|
|
|
|
if (ival != LINKER_HINTS_VERSION) {
|
|
|
|
printf("hints file version mismatch %d\n", ival);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
bufend = hints + vattr.va_size;
|
|
|
|
recptr = (u_char*)intp;
|
|
|
|
clen = blen = 0;
|
|
|
|
while (recptr < bufend && !found) {
|
|
|
|
intp = (int*)recptr;
|
|
|
|
reclen = *intp++;
|
|
|
|
ival = *intp++;
|
|
|
|
cp = (char*)intp;
|
|
|
|
switch (ival) {
|
|
|
|
case MDT_VERSION:
|
|
|
|
clen = *cp++;
|
|
|
|
if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
|
|
|
|
break;
|
|
|
|
cp += clen;
|
|
|
|
INT_ALIGN(hints, cp);
|
|
|
|
ival = *(int*)cp;
|
|
|
|
cp += sizeof(int);
|
|
|
|
clen = *cp++;
|
|
|
|
if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ival >= verinfo->md_ver_minimum &&
|
|
|
|
ival <= verinfo->md_ver_maximum &&
|
|
|
|
ival > bestver) {
|
|
|
|
bestver = ival;
|
|
|
|
best = cp;
|
|
|
|
blen = clen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
recptr += reclen + sizeof(int);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Finally check if KLD is in the place
|
|
|
|
*/
|
|
|
|
if (found)
|
|
|
|
result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
|
|
|
|
else if (best)
|
|
|
|
result = linker_lookup_file(path, pathlen, best, blen, &mattr);
|
|
|
|
if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) {
|
|
|
|
/*
|
|
|
|
* KLD is newer than hints file. What we should do now ?
|
|
|
|
*/
|
2001-09-12 00:50:23 +00:00
|
|
|
printf("warning: KLD '%s' is newer than the linker.hints file\n",
|
|
|
|
result);
|
2001-09-11 01:09:24 +00:00
|
|
|
}
|
|
|
|
bad:
|
|
|
|
if (hints)
|
|
|
|
free(hints, M_TEMP);
|
|
|
|
if (nd.ni_vp != NULL)
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_close(nd.ni_vp, FREAD, cred, td);
|
2001-09-11 01:09:24 +00:00
|
|
|
/*
|
|
|
|
* If nothing found or hints is absent - fallback to the old way
|
|
|
|
* by using "kldname[.ko]" as module name.
|
|
|
|
*/
|
|
|
|
if (!found && !bestver && result == NULL)
|
|
|
|
result = linker_lookup_file(path, pathlen, modname, modnamelen, NULL);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup KLD which contains requested module in the all directories.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
linker_search_module(const char *modname, int modnamelen,
|
|
|
|
struct mod_depend *verinfo)
|
|
|
|
{
|
|
|
|
char *cp, *ep, *result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* traverse the linker path
|
|
|
|
*/
|
|
|
|
for (cp = linker_path; *cp; cp = ep + 1) {
|
|
|
|
|
|
|
|
/* find the end of this component */
|
|
|
|
for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
|
|
|
|
;
|
|
|
|
result = linker_hints_lookup(cp, ep - cp, modname, modnamelen, verinfo);
|
|
|
|
if (result != NULL)
|
|
|
|
return(result);
|
|
|
|
if (*ep == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for module in all directories listed in the linker_path.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
linker_search_kld(const char *name)
|
|
|
|
{
|
|
|
|
char *cp, *ep, *result, **cpp;
|
|
|
|
int extlen, len;
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
/* qualified at all? */
|
1999-12-10 17:38:41 +00:00
|
|
|
if (index(name, '/'))
|
1998-10-10 00:07:53 +00:00
|
|
|
return(linker_strdup(name));
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
extlen = 0;
|
|
|
|
for (cpp = linker_ext_list; *cpp; cpp++) {
|
|
|
|
len = strlen(*cpp);
|
|
|
|
if (len > extlen)
|
|
|
|
extlen = len;
|
|
|
|
}
|
|
|
|
extlen++; /* trailing '\0' */
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
/* traverse the linker path */
|
2000-04-29 13:19:31 +00:00
|
|
|
len = strlen(name);
|
2001-09-11 01:09:24 +00:00
|
|
|
for (ep = linker_path; *ep; ep++) {
|
|
|
|
cp = ep;
|
1998-10-10 00:07:53 +00:00
|
|
|
/* find the end of this component */
|
2001-09-11 01:09:24 +00:00
|
|
|
for (; *ep != 0 && *ep != ';'; ep++)
|
1998-10-10 00:07:53 +00:00
|
|
|
;
|
2001-09-11 01:09:24 +00:00
|
|
|
result = linker_lookup_file(cp, ep - cp, name, len, NULL);
|
|
|
|
if (result != NULL)
|
|
|
|
return(result);
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
linker_basename(const char* path)
|
|
|
|
{
|
|
|
|
const char *filename;
|
|
|
|
|
|
|
|
filename = rindex(path, '/');
|
|
|
|
if (filename == NULL)
|
|
|
|
return path;
|
|
|
|
if (filename[1])
|
|
|
|
filename++;
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a file which contains given module and load it,
|
|
|
|
* if "parent" is not NULL, register a reference to it.
|
|
|
|
*/
|
|
|
|
static int
|
2001-09-11 01:09:24 +00:00
|
|
|
linker_load_module(const char *kldname, const char *modname,
|
|
|
|
struct linker_file *parent, struct mod_depend *verinfo,
|
|
|
|
struct linker_file **lfpp)
|
2000-04-29 13:19:31 +00:00
|
|
|
{
|
|
|
|
linker_file_t lfdep;
|
|
|
|
const char *filename;
|
|
|
|
char *pathname;
|
|
|
|
int error;
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
if (modname == NULL) {
|
|
|
|
/*
|
|
|
|
* We have to load KLD
|
|
|
|
*/
|
|
|
|
KASSERT(verinfo == NULL, ("linker_load_module: verinfo is not NULL"));
|
|
|
|
pathname = linker_search_kld(kldname);
|
|
|
|
} else {
|
|
|
|
if (modlist_lookup2(modname, verinfo) != NULL)
|
|
|
|
return (EEXIST);
|
|
|
|
if (kldname == NULL) {
|
|
|
|
/*
|
|
|
|
* Need to find a KLD with required module
|
|
|
|
*/
|
|
|
|
pathname = linker_search_module(modname, strlen(modname), verinfo);
|
|
|
|
} else
|
|
|
|
pathname = linker_strdup(kldname);
|
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
if (pathname == NULL)
|
2001-09-11 01:09:24 +00:00
|
|
|
return (ENOENT);
|
2000-04-29 13:19:31 +00:00
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
/*
|
|
|
|
* Can't load more than one file with the same basename
|
|
|
|
* XXX: Actually it should be possible to have multiple KLDs
|
|
|
|
* with the same basename but different path because they can provide
|
|
|
|
* different versions of the same modules.
|
|
|
|
*/
|
2000-04-29 13:19:31 +00:00
|
|
|
filename = linker_basename(pathname);
|
|
|
|
if (linker_find_file_by_name(filename)) {
|
|
|
|
error = EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
error = linker_load_file(pathname, &lfdep);
|
|
|
|
if (error)
|
|
|
|
break;
|
2001-09-11 01:09:24 +00:00
|
|
|
if (modname && verinfo && modlist_lookup2(modname, verinfo) == NULL) {
|
|
|
|
linker_file_unload(lfdep);
|
|
|
|
error = ENOENT;
|
|
|
|
break;
|
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
if (parent) {
|
2001-11-16 21:08:40 +00:00
|
|
|
error = linker_file_add_dependency(parent, lfdep);
|
2000-04-29 13:19:31 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
2001-09-11 01:09:24 +00:00
|
|
|
if (lfpp)
|
|
|
|
*lfpp = lfdep;
|
2000-04-29 13:19:31 +00:00
|
|
|
} while(0);
|
|
|
|
out:
|
|
|
|
if (pathname)
|
|
|
|
free(pathname, M_LINKER);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine is responsible for finding dependencies of userland
|
|
|
|
* initiated kldload(2)'s of files.
|
|
|
|
*/
|
|
|
|
int
|
2001-11-16 21:08:40 +00:00
|
|
|
linker_load_dependencies(linker_file_t lf)
|
2000-04-29 13:19:31 +00:00
|
|
|
{
|
|
|
|
linker_file_t lfdep;
|
2001-06-13 10:58:39 +00:00
|
|
|
struct mod_metadata **start, **stop, **mdp, **nmdp;
|
2000-04-29 13:19:31 +00:00
|
|
|
struct mod_metadata *mp, *nmp;
|
2001-09-11 01:09:24 +00:00
|
|
|
struct mod_depend *verinfo;
|
2000-04-29 13:19:31 +00:00
|
|
|
modlist_t mod;
|
2001-06-20 16:08:45 +00:00
|
|
|
const char *modname, *nmodname;
|
2001-06-13 10:58:39 +00:00
|
|
|
int ver, error = 0, count;
|
2000-04-29 13:19:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* All files are dependant on /kernel.
|
|
|
|
*/
|
|
|
|
if (linker_kernel_file) {
|
|
|
|
linker_kernel_file->refs++;
|
2001-11-16 21:08:40 +00:00
|
|
|
error = linker_file_add_dependency(lf, linker_kernel_file);
|
2000-04-29 13:19:31 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, &count) != 0)
|
2001-03-22 07:55:33 +00:00
|
|
|
return 0;
|
2001-06-13 10:58:39 +00:00
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
|
|
|
mp = linker_reloc_ptr(lf, *mdp);
|
2001-03-22 07:55:33 +00:00
|
|
|
if (mp->md_type != MDT_VERSION)
|
|
|
|
continue;
|
2001-03-22 08:58:45 +00:00
|
|
|
linker_mdt_version(lf, mp, &modname, &ver);
|
|
|
|
mod = modlist_lookup(modname, ver);
|
|
|
|
if (mod != NULL) {
|
|
|
|
printf("interface %s.%d already present in the KLD '%s'!\n",
|
|
|
|
modname, ver, mod->container->filename);
|
2001-03-22 07:55:33 +00:00
|
|
|
return EEXIST;
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
2001-03-22 08:58:45 +00:00
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
|
|
|
mp = linker_reloc_ptr(lf, *mdp);
|
2001-03-22 07:55:33 +00:00
|
|
|
if (mp->md_type != MDT_DEPEND)
|
|
|
|
continue;
|
2001-09-11 01:09:24 +00:00
|
|
|
linker_mdt_depend(lf, mp, &modname, &verinfo);
|
2001-03-22 07:55:33 +00:00
|
|
|
nmodname = NULL;
|
2001-06-13 10:58:39 +00:00
|
|
|
for (nmdp = start; nmdp < stop; nmdp++) {
|
|
|
|
nmp = linker_reloc_ptr(lf, *nmdp);
|
2001-03-22 07:55:33 +00:00
|
|
|
if (nmp->md_type != MDT_VERSION)
|
2000-04-29 13:19:31 +00:00
|
|
|
continue;
|
2001-03-22 07:55:33 +00:00
|
|
|
nmodname = linker_reloc_ptr(lf, nmp->md_cval);
|
|
|
|
if (strcmp(modname, nmodname) == 0)
|
2000-04-29 13:19:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-06-13 10:58:39 +00:00
|
|
|
if (nmdp < stop) /* early exit, it's a self reference */
|
2001-03-22 07:55:33 +00:00
|
|
|
continue;
|
2001-09-11 01:09:24 +00:00
|
|
|
mod = modlist_lookup2(modname, verinfo);
|
2001-03-22 07:55:33 +00:00
|
|
|
if (mod) { /* woohoo, it's loaded already */
|
|
|
|
lfdep = mod->container;
|
|
|
|
lfdep->refs++;
|
2001-11-16 21:08:40 +00:00
|
|
|
error = linker_file_add_dependency(lf, lfdep);
|
2001-03-22 07:55:33 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
2001-09-11 01:09:24 +00:00
|
|
|
error = linker_load_module(NULL, modname, lf, verinfo, NULL);
|
2001-03-22 07:55:33 +00:00
|
|
|
if (error) {
|
|
|
|
printf("KLD %s: depends on %s - not available\n",
|
|
|
|
lf->filename, modname);
|
|
|
|
break;
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
2001-03-22 07:55:33 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
return error;
|
2001-06-13 10:58:39 +00:00
|
|
|
linker_addmodules(lf, start, stop, 0);
|
2000-04-29 13:19:31 +00:00
|
|
|
return error;
|
|
|
|
}
|
2001-10-30 15:21:45 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
sysctl_kern_function_list_iterate(const char *name, void *opaque)
|
|
|
|
{
|
|
|
|
struct sysctl_req *req;
|
|
|
|
|
|
|
|
req = opaque;
|
|
|
|
return (SYSCTL_OUT(req, name, strlen(name) + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Export a nul-separated, double-nul-terminated list of all function names
|
|
|
|
* in the kernel.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
|
|
|
error = LINKER_EACH_FUNCTION_NAME(lf,
|
|
|
|
sysctl_kern_function_list_iterate, req);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
return (SYSCTL_OUT(req, "", 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
|
|
|
|
NULL, 0, sysctl_kern_function_list, "", "kernel function list");
|