Deleted Added
full compact
kern_linker.c (92016) kern_linker.c (92032)
1/*-
2 * Copyright (c) 1997-2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
1/*-
2 * Copyright (c) 1997-2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/kern/kern_linker.c 92016 2002-03-10 19:20:01Z sobomax $
26 * $FreeBSD: head/sys/kern/kern_linker.c 92032 2002-03-10 23:12:43Z dwmalone $
27 */
28
29#include "opt_ddb.h"
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/sysproto.h>
36#include <sys/sysent.h>
37#include <sys/proc.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/module.h>
41#include <sys/linker.h>
42#include <sys/fcntl.h>
43#include <sys/libkern.h>
44#include <sys/namei.h>
45#include <sys/vnode.h>
46#include <sys/sysctl.h>
47
48
49#include "linker_if.h"
50
51#ifdef KLD_DEBUG
52int kld_debug = 0;
53#endif
54
55/*
56 * static char *linker_search_path(const char *name, struct mod_depend
57 * *verinfo);
58 */
59static const char *linker_basename(const char *path);
60static int linker_load_module(const char *kldname, const char *modname,
61 struct linker_file *parent, struct mod_depend *verinfo,
62 struct linker_file **lfpp);
63
64/* Metadata from the static kernel */
65SET_DECLARE(modmetadata_set, struct mod_metadata);
66
67MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
68
69linker_file_t linker_kernel_file;
70
71static struct lock lock; /* lock for the file list */
72static linker_class_list_t classes;
73static linker_file_list_t linker_files;
74static int next_file_id = 1;
75
76#define LINKER_GET_NEXT_FILE_ID(a) do { \
77 linker_file_t lftmp; \
78 \
79retry: \
80 TAILQ_FOREACH(lftmp, &linker_files, link) { \
81 if (next_file_id == lftmp->id) { \
82 next_file_id++; \
83 goto retry; \
84 } \
85 } \
86 (a) = next_file_id; \
87} while(0)
88
89
90/* XXX wrong name; we're looking at version provision tags here, not modules */
91typedef TAILQ_HEAD(, modlist) modlisthead_t;
92struct modlist {
93 TAILQ_ENTRY(modlist) link; /* chain together all modules */
94 linker_file_t container;
95 const char *name;
96 int version;
97};
98typedef struct modlist *modlist_t;
99static modlisthead_t found_modules;
100
101static char *
102linker_strdup(const char *str)
103{
104 char *result;
105
106 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
107 strcpy(result, str);
108 return (result);
109}
110
111static void
112linker_init(void *arg)
113{
114
115 lockinit(&lock, PVM, "klink", 0, 0);
116 TAILQ_INIT(&classes);
117 TAILQ_INIT(&linker_files);
118}
119
120SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0)
121
122int
123linker_add_class(linker_class_t lc)
124{
125
126 kobj_class_compile((kobj_class_t) lc);
127 TAILQ_INSERT_TAIL(&classes, lc, link);
128 return (0);
129}
130
131static void
132linker_file_sysinit(linker_file_t lf)
133{
134 struct sysinit **start, **stop, **sipp, **xipp, *save;
135
136 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
137 lf->filename));
138
139 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
140 return;
141 /*
142 * Perform a bubble sort of the system initialization objects by
143 * their subsystem (primary key) and order (secondary key).
144 *
145 * Since some things care about execution order, this is the operation
146 * which ensures continued function.
147 */
148 for (sipp = start; sipp < stop; sipp++) {
149 for (xipp = sipp + 1; xipp < stop; xipp++) {
150 if ((*sipp)->subsystem < (*xipp)->subsystem ||
151 ((*sipp)->subsystem == (*xipp)->subsystem &&
152 (*sipp)->order <= (*xipp)->order))
153 continue; /* skip */
154 save = *sipp;
155 *sipp = *xipp;
156 *xipp = save;
157 }
158 }
159
160 /*
161 * Traverse the (now) ordered list of system initialization tasks.
162 * Perform each task, and continue on to the next task.
163 */
164 for (sipp = start; sipp < stop; sipp++) {
165 if ((*sipp)->subsystem == SI_SUB_DUMMY)
166 continue; /* skip dummy task(s) */
167
168 /* Call function */
169 (*((*sipp)->func)) ((*sipp)->udata);
170 }
171}
172
173static void
174linker_file_sysuninit(linker_file_t lf)
175{
176 struct sysinit **start, **stop, **sipp, **xipp, *save;
177
178 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
179 lf->filename));
180
181 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
182 NULL) != 0)
183 return;
184
185 /*
186 * Perform a reverse bubble sort of the system initialization objects
187 * by their subsystem (primary key) and order (secondary key).
188 *
189 * Since some things care about execution order, this is the operation
190 * which ensures continued function.
191 */
192 for (sipp = start; sipp < stop; sipp++) {
193 for (xipp = sipp + 1; xipp < stop; xipp++) {
194 if ((*sipp)->subsystem > (*xipp)->subsystem ||
195 ((*sipp)->subsystem == (*xipp)->subsystem &&
196 (*sipp)->order >= (*xipp)->order))
197 continue; /* skip */
198 save = *sipp;
199 *sipp = *xipp;
200 *xipp = save;
201 }
202 }
203
204 /*
205 * Traverse the (now) ordered list of system initialization tasks.
206 * Perform each task, and continue on to the next task.
207 */
208 for (sipp = start; sipp < stop; sipp++) {
209 if ((*sipp)->subsystem == SI_SUB_DUMMY)
210 continue; /* skip dummy task(s) */
211
212 /* Call function */
213 (*((*sipp)->func)) ((*sipp)->udata);
214 }
215}
216
217static void
218linker_file_register_sysctls(linker_file_t lf)
219{
220 struct sysctl_oid **start, **stop, **oidp;
221
222 KLD_DPF(FILE,
223 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
224 lf->filename));
225
226 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
227 return;
228
229 for (oidp = start; oidp < stop; oidp++)
230 sysctl_register_oid(*oidp);
231}
232
233static void
234linker_file_unregister_sysctls(linker_file_t lf)
235{
236 struct sysctl_oid **start, **stop, **oidp;
237
238 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
239 " for %s\n", lf->filename));
240
241 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
242 return;
243
244 for (oidp = start; oidp < stop; oidp++)
245 sysctl_unregister_oid(*oidp);
246}
247
248static int
249linker_file_register_modules(linker_file_t lf)
250{
251 struct mod_metadata **start, **stop, **mdp;
252 const moduledata_t *moddata;
253 int error;
254
255 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
256 " in %s\n", lf->filename));
257
258 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
259 &stop, 0) != 0) {
260 /*
261 * This fallback should be unnecessary, but if we get booted
262 * from boot2 instead of loader and we are missing our
263 * metadata then we have to try the best we can.
264 */
265 if (lf == linker_kernel_file) {
266 start = SET_BEGIN(modmetadata_set);
267 stop = SET_LIMIT(modmetadata_set);
268 } else
269 return (0);
270 }
271 for (mdp = start; mdp < stop; mdp++) {
272 if ((*mdp)->md_type != MDT_MODULE)
273 continue;
274 moddata = (*mdp)->md_data;
275 KLD_DPF(FILE, ("Registering module %s in %s\n",
276 moddata->name, lf->filename));
277 if (module_lookupbyname(moddata->name) != NULL) {
278 printf("Warning: module %s already exists\n",
279 moddata->name);
280 continue; /* or return a error ? */
281 }
282 error = module_register(moddata, lf);
283 if (error)
284 printf("Module %s failed to register: %d\n",
285 moddata->name, error);
286 }
287 return (0);
288}
289
290static void
291linker_init_kernel_modules(void)
292{
293
294 linker_file_register_modules(linker_kernel_file);
295}
296
297SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0)
298
299int
300linker_load_file(const char *filename, linker_file_t *result)
301{
302 linker_class_t lc;
303 linker_file_t lf;
304 int foundfile, error = 0;
305
306 /* Refuse to load modules if securelevel raised */
307 if (securelevel > 0)
308 return (EPERM);
309
310 lf = linker_find_file_by_name(filename);
311 if (lf) {
312 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
313 " incrementing refs\n", filename));
314 *result = lf;
315 lf->refs++;
316 goto out;
317 }
318 lf = NULL;
319 foundfile = 0;
320 TAILQ_FOREACH(lc, &classes, link) {
321 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
322 filename));
323 error = LINKER_LOAD_FILE(lc, filename, &lf);
324 /*
325 * If we got something other than ENOENT, then it exists but
326 * we cannot load it for some other reason.
327 */
328 if (error != ENOENT)
329 foundfile = 1;
330 if (lf) {
331 linker_file_register_modules(lf);
332 linker_file_register_sysctls(lf);
333 linker_file_sysinit(lf);
334 lf->flags |= LINKER_FILE_LINKED;
335 *result = lf;
336 error = 0;
337 goto out;
338 }
339 }
340 /*
341 * Less than ideal, but tells the user whether it failed to load or
342 * the module was not found.
343 */
344 if (foundfile)
345 /* Format not recognized (or unloadable). */
346 error = ENOEXEC;
347 else
348 error = ENOENT; /* Nothing found */
349out:
350 return (error);
351}
352
353/* XXX: function parameters are incomplete */
354int
355linker_reference_module(const char *modname, linker_file_t *result)
356{
357 return (linker_load_module(NULL, modname, NULL, NULL, result));
358}
359
360linker_file_t
361linker_find_file_by_name(const char *filename)
362{
363 linker_file_t lf = 0;
364 char *koname;
27 */
28
29#include "opt_ddb.h"
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/sysproto.h>
36#include <sys/sysent.h>
37#include <sys/proc.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/module.h>
41#include <sys/linker.h>
42#include <sys/fcntl.h>
43#include <sys/libkern.h>
44#include <sys/namei.h>
45#include <sys/vnode.h>
46#include <sys/sysctl.h>
47
48
49#include "linker_if.h"
50
51#ifdef KLD_DEBUG
52int kld_debug = 0;
53#endif
54
55/*
56 * static char *linker_search_path(const char *name, struct mod_depend
57 * *verinfo);
58 */
59static const char *linker_basename(const char *path);
60static int linker_load_module(const char *kldname, const char *modname,
61 struct linker_file *parent, struct mod_depend *verinfo,
62 struct linker_file **lfpp);
63
64/* Metadata from the static kernel */
65SET_DECLARE(modmetadata_set, struct mod_metadata);
66
67MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
68
69linker_file_t linker_kernel_file;
70
71static struct lock lock; /* lock for the file list */
72static linker_class_list_t classes;
73static linker_file_list_t linker_files;
74static int next_file_id = 1;
75
76#define LINKER_GET_NEXT_FILE_ID(a) do { \
77 linker_file_t lftmp; \
78 \
79retry: \
80 TAILQ_FOREACH(lftmp, &linker_files, link) { \
81 if (next_file_id == lftmp->id) { \
82 next_file_id++; \
83 goto retry; \
84 } \
85 } \
86 (a) = next_file_id; \
87} while(0)
88
89
90/* XXX wrong name; we're looking at version provision tags here, not modules */
91typedef TAILQ_HEAD(, modlist) modlisthead_t;
92struct modlist {
93 TAILQ_ENTRY(modlist) link; /* chain together all modules */
94 linker_file_t container;
95 const char *name;
96 int version;
97};
98typedef struct modlist *modlist_t;
99static modlisthead_t found_modules;
100
101static char *
102linker_strdup(const char *str)
103{
104 char *result;
105
106 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
107 strcpy(result, str);
108 return (result);
109}
110
111static void
112linker_init(void *arg)
113{
114
115 lockinit(&lock, PVM, "klink", 0, 0);
116 TAILQ_INIT(&classes);
117 TAILQ_INIT(&linker_files);
118}
119
120SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0)
121
122int
123linker_add_class(linker_class_t lc)
124{
125
126 kobj_class_compile((kobj_class_t) lc);
127 TAILQ_INSERT_TAIL(&classes, lc, link);
128 return (0);
129}
130
131static void
132linker_file_sysinit(linker_file_t lf)
133{
134 struct sysinit **start, **stop, **sipp, **xipp, *save;
135
136 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
137 lf->filename));
138
139 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
140 return;
141 /*
142 * Perform a bubble sort of the system initialization objects by
143 * their subsystem (primary key) and order (secondary key).
144 *
145 * Since some things care about execution order, this is the operation
146 * which ensures continued function.
147 */
148 for (sipp = start; sipp < stop; sipp++) {
149 for (xipp = sipp + 1; xipp < stop; xipp++) {
150 if ((*sipp)->subsystem < (*xipp)->subsystem ||
151 ((*sipp)->subsystem == (*xipp)->subsystem &&
152 (*sipp)->order <= (*xipp)->order))
153 continue; /* skip */
154 save = *sipp;
155 *sipp = *xipp;
156 *xipp = save;
157 }
158 }
159
160 /*
161 * Traverse the (now) ordered list of system initialization tasks.
162 * Perform each task, and continue on to the next task.
163 */
164 for (sipp = start; sipp < stop; sipp++) {
165 if ((*sipp)->subsystem == SI_SUB_DUMMY)
166 continue; /* skip dummy task(s) */
167
168 /* Call function */
169 (*((*sipp)->func)) ((*sipp)->udata);
170 }
171}
172
173static void
174linker_file_sysuninit(linker_file_t lf)
175{
176 struct sysinit **start, **stop, **sipp, **xipp, *save;
177
178 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
179 lf->filename));
180
181 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
182 NULL) != 0)
183 return;
184
185 /*
186 * Perform a reverse bubble sort of the system initialization objects
187 * by their subsystem (primary key) and order (secondary key).
188 *
189 * Since some things care about execution order, this is the operation
190 * which ensures continued function.
191 */
192 for (sipp = start; sipp < stop; sipp++) {
193 for (xipp = sipp + 1; xipp < stop; xipp++) {
194 if ((*sipp)->subsystem > (*xipp)->subsystem ||
195 ((*sipp)->subsystem == (*xipp)->subsystem &&
196 (*sipp)->order >= (*xipp)->order))
197 continue; /* skip */
198 save = *sipp;
199 *sipp = *xipp;
200 *xipp = save;
201 }
202 }
203
204 /*
205 * Traverse the (now) ordered list of system initialization tasks.
206 * Perform each task, and continue on to the next task.
207 */
208 for (sipp = start; sipp < stop; sipp++) {
209 if ((*sipp)->subsystem == SI_SUB_DUMMY)
210 continue; /* skip dummy task(s) */
211
212 /* Call function */
213 (*((*sipp)->func)) ((*sipp)->udata);
214 }
215}
216
217static void
218linker_file_register_sysctls(linker_file_t lf)
219{
220 struct sysctl_oid **start, **stop, **oidp;
221
222 KLD_DPF(FILE,
223 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
224 lf->filename));
225
226 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
227 return;
228
229 for (oidp = start; oidp < stop; oidp++)
230 sysctl_register_oid(*oidp);
231}
232
233static void
234linker_file_unregister_sysctls(linker_file_t lf)
235{
236 struct sysctl_oid **start, **stop, **oidp;
237
238 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
239 " for %s\n", lf->filename));
240
241 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
242 return;
243
244 for (oidp = start; oidp < stop; oidp++)
245 sysctl_unregister_oid(*oidp);
246}
247
248static int
249linker_file_register_modules(linker_file_t lf)
250{
251 struct mod_metadata **start, **stop, **mdp;
252 const moduledata_t *moddata;
253 int error;
254
255 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
256 " in %s\n", lf->filename));
257
258 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
259 &stop, 0) != 0) {
260 /*
261 * This fallback should be unnecessary, but if we get booted
262 * from boot2 instead of loader and we are missing our
263 * metadata then we have to try the best we can.
264 */
265 if (lf == linker_kernel_file) {
266 start = SET_BEGIN(modmetadata_set);
267 stop = SET_LIMIT(modmetadata_set);
268 } else
269 return (0);
270 }
271 for (mdp = start; mdp < stop; mdp++) {
272 if ((*mdp)->md_type != MDT_MODULE)
273 continue;
274 moddata = (*mdp)->md_data;
275 KLD_DPF(FILE, ("Registering module %s in %s\n",
276 moddata->name, lf->filename));
277 if (module_lookupbyname(moddata->name) != NULL) {
278 printf("Warning: module %s already exists\n",
279 moddata->name);
280 continue; /* or return a error ? */
281 }
282 error = module_register(moddata, lf);
283 if (error)
284 printf("Module %s failed to register: %d\n",
285 moddata->name, error);
286 }
287 return (0);
288}
289
290static void
291linker_init_kernel_modules(void)
292{
293
294 linker_file_register_modules(linker_kernel_file);
295}
296
297SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0)
298
299int
300linker_load_file(const char *filename, linker_file_t *result)
301{
302 linker_class_t lc;
303 linker_file_t lf;
304 int foundfile, error = 0;
305
306 /* Refuse to load modules if securelevel raised */
307 if (securelevel > 0)
308 return (EPERM);
309
310 lf = linker_find_file_by_name(filename);
311 if (lf) {
312 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
313 " incrementing refs\n", filename));
314 *result = lf;
315 lf->refs++;
316 goto out;
317 }
318 lf = NULL;
319 foundfile = 0;
320 TAILQ_FOREACH(lc, &classes, link) {
321 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
322 filename));
323 error = LINKER_LOAD_FILE(lc, filename, &lf);
324 /*
325 * If we got something other than ENOENT, then it exists but
326 * we cannot load it for some other reason.
327 */
328 if (error != ENOENT)
329 foundfile = 1;
330 if (lf) {
331 linker_file_register_modules(lf);
332 linker_file_register_sysctls(lf);
333 linker_file_sysinit(lf);
334 lf->flags |= LINKER_FILE_LINKED;
335 *result = lf;
336 error = 0;
337 goto out;
338 }
339 }
340 /*
341 * Less than ideal, but tells the user whether it failed to load or
342 * the module was not found.
343 */
344 if (foundfile)
345 /* Format not recognized (or unloadable). */
346 error = ENOEXEC;
347 else
348 error = ENOENT; /* Nothing found */
349out:
350 return (error);
351}
352
353/* XXX: function parameters are incomplete */
354int
355linker_reference_module(const char *modname, linker_file_t *result)
356{
357 return (linker_load_module(NULL, modname, NULL, NULL, result));
358}
359
360linker_file_t
361linker_find_file_by_name(const char *filename)
362{
363 linker_file_t lf = 0;
364 char *koname;
365 int err;
366
367 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
368 if (koname == NULL)
369 goto out;
370 sprintf(koname, "%s.ko", filename);
371
372 lockmgr(&lock, LK_SHARED, 0, curthread);
373 TAILQ_FOREACH(lf, &linker_files, link) {
365
366 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
367 if (koname == NULL)
368 goto out;
369 sprintf(koname, "%s.ko", filename);
370
371 lockmgr(&lock, LK_SHARED, 0, curthread);
372 TAILQ_FOREACH(lf, &linker_files, link) {
374 err = strcmp(lf->filename, koname);
375 if (err == 0)
373 if (strcmp(lf->filename, koname) == 0)
376 break;
374 break;
377 err = strcmp(lf->filename, filename);
378 if (err == 0)
375 if (strcmp(lf->filename, filename) == 0)
379 break;
380 }
381 lockmgr(&lock, LK_RELEASE, 0, curthread);
382out:
383 if (koname)
384 free(koname, M_LINKER);
385 return (lf);
386}
387
388linker_file_t
389linker_find_file_by_id(int fileid)
390{
391 linker_file_t lf = 0;
392
393 lockmgr(&lock, LK_SHARED, 0, curthread);
394 TAILQ_FOREACH(lf, &linker_files, link)
395 if (lf->id == fileid)
396 break;
397 lockmgr(&lock, LK_RELEASE, 0, curthread);
398 return (lf);
399}
400
401linker_file_t
402linker_make_file(const char *pathname, linker_class_t lc)
403{
404 linker_file_t lf;
405 const char *filename;
406
407 lf = NULL;
408 filename = linker_basename(pathname);
409
410 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
411 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
412 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
413 if (lf == NULL)
414 goto out;
415 lf->refs = 1;
416 lf->userrefs = 0;
417 lf->flags = 0;
418 lf->filename = linker_strdup(filename);
419 LINKER_GET_NEXT_FILE_ID(lf->id);
420 lf->ndeps = 0;
421 lf->deps = NULL;
422 STAILQ_INIT(&lf->common);
423 TAILQ_INIT(&lf->modules);
424 TAILQ_INSERT_TAIL(&linker_files, lf, link);
425out:
426 lockmgr(&lock, LK_RELEASE, 0, curthread);
427 return (lf);
428}
429
430int
431linker_file_unload(linker_file_t file)
432{
433 module_t mod, next;
434 modlist_t ml, nextml;
435 struct common_symbol *cp;
436 int error, i;
437
438 error = 0;
439
440 /* Refuse to unload modules if securelevel raised. */
441 if (securelevel > 0)
442 return (EPERM);
443
444 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
445 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
446 if (file->refs == 1) {
447 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
448 " informing modules\n"));
449
450 /*
451 * Inform any modules associated with this file.
452 */
453 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
454 next = module_getfnext(mod);
455
456 /*
457 * Give the module a chance to veto the unload.
458 */
459 if ((error = module_unload(mod)) != 0) {
460 KLD_DPF(FILE, ("linker_file_unload: module %x"
461 " vetoes unload\n", mod));
462 lockmgr(&lock, LK_RELEASE, 0, curthread);
463 goto out;
464 }
465 module_release(mod);
466 }
467 }
468 file->refs--;
469 if (file->refs > 0) {
470 lockmgr(&lock, LK_RELEASE, 0, curthread);
471 goto out;
472 }
473 for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) {
474 nextml = TAILQ_NEXT(ml, link);
475 if (ml->container == file)
476 TAILQ_REMOVE(&found_modules, ml, link);
477 }
478
479 /*
480 * Don't try to run SYSUNINITs if we are unloaded due to a
481 * link error.
482 */
483 if (file->flags & LINKER_FILE_LINKED) {
484 linker_file_sysuninit(file);
485 linker_file_unregister_sysctls(file);
486 }
487 TAILQ_REMOVE(&linker_files, file, link);
488 lockmgr(&lock, LK_RELEASE, 0, curthread);
489
490 if (file->deps) {
491 for (i = 0; i < file->ndeps; i++)
492 linker_file_unload(file->deps[i]);
493 free(file->deps, M_LINKER);
494 file->deps = NULL;
495 }
496 for (cp = STAILQ_FIRST(&file->common); cp;
497 cp = STAILQ_FIRST(&file->common)) {
498 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
499 free(cp, M_LINKER);
500 }
501
502 LINKER_UNLOAD(file);
503 if (file->filename) {
504 free(file->filename, M_LINKER);
505 file->filename = NULL;
506 }
507 kobj_delete((kobj_t) file, M_LINKER);
508out:
509 return (error);
510}
511
512int
513linker_file_add_dependency(linker_file_t file, linker_file_t dep)
514{
515 linker_file_t *newdeps;
516
517 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
518 M_LINKER, M_WAITOK | M_ZERO);
519 if (newdeps == NULL)
520 return (ENOMEM);
521
522 if (file->deps) {
523 bcopy(file->deps, newdeps,
524 file->ndeps * sizeof(linker_file_t *));
525 free(file->deps, M_LINKER);
526 }
527 file->deps = newdeps;
528 file->deps[file->ndeps] = dep;
529 file->ndeps++;
530 return (0);
531}
532
533/*
534 * Locate a linker set and its contents. This is a helper function to avoid
535 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void ***
536 */
537int
538linker_file_lookup_set(linker_file_t file, const char *name,
539 void *firstp, void *lastp, int *countp)
540{
541
542 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
543}
544
545caddr_t
546linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
547{
548 c_linker_sym_t sym;
549 linker_symval_t symval;
550 caddr_t address;
551 size_t common_size = 0;
376 break;
377 }
378 lockmgr(&lock, LK_RELEASE, 0, curthread);
379out:
380 if (koname)
381 free(koname, M_LINKER);
382 return (lf);
383}
384
385linker_file_t
386linker_find_file_by_id(int fileid)
387{
388 linker_file_t lf = 0;
389
390 lockmgr(&lock, LK_SHARED, 0, curthread);
391 TAILQ_FOREACH(lf, &linker_files, link)
392 if (lf->id == fileid)
393 break;
394 lockmgr(&lock, LK_RELEASE, 0, curthread);
395 return (lf);
396}
397
398linker_file_t
399linker_make_file(const char *pathname, linker_class_t lc)
400{
401 linker_file_t lf;
402 const char *filename;
403
404 lf = NULL;
405 filename = linker_basename(pathname);
406
407 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
408 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
409 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
410 if (lf == NULL)
411 goto out;
412 lf->refs = 1;
413 lf->userrefs = 0;
414 lf->flags = 0;
415 lf->filename = linker_strdup(filename);
416 LINKER_GET_NEXT_FILE_ID(lf->id);
417 lf->ndeps = 0;
418 lf->deps = NULL;
419 STAILQ_INIT(&lf->common);
420 TAILQ_INIT(&lf->modules);
421 TAILQ_INSERT_TAIL(&linker_files, lf, link);
422out:
423 lockmgr(&lock, LK_RELEASE, 0, curthread);
424 return (lf);
425}
426
427int
428linker_file_unload(linker_file_t file)
429{
430 module_t mod, next;
431 modlist_t ml, nextml;
432 struct common_symbol *cp;
433 int error, i;
434
435 error = 0;
436
437 /* Refuse to unload modules if securelevel raised. */
438 if (securelevel > 0)
439 return (EPERM);
440
441 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
442 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread);
443 if (file->refs == 1) {
444 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
445 " informing modules\n"));
446
447 /*
448 * Inform any modules associated with this file.
449 */
450 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
451 next = module_getfnext(mod);
452
453 /*
454 * Give the module a chance to veto the unload.
455 */
456 if ((error = module_unload(mod)) != 0) {
457 KLD_DPF(FILE, ("linker_file_unload: module %x"
458 " vetoes unload\n", mod));
459 lockmgr(&lock, LK_RELEASE, 0, curthread);
460 goto out;
461 }
462 module_release(mod);
463 }
464 }
465 file->refs--;
466 if (file->refs > 0) {
467 lockmgr(&lock, LK_RELEASE, 0, curthread);
468 goto out;
469 }
470 for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) {
471 nextml = TAILQ_NEXT(ml, link);
472 if (ml->container == file)
473 TAILQ_REMOVE(&found_modules, ml, link);
474 }
475
476 /*
477 * Don't try to run SYSUNINITs if we are unloaded due to a
478 * link error.
479 */
480 if (file->flags & LINKER_FILE_LINKED) {
481 linker_file_sysuninit(file);
482 linker_file_unregister_sysctls(file);
483 }
484 TAILQ_REMOVE(&linker_files, file, link);
485 lockmgr(&lock, LK_RELEASE, 0, curthread);
486
487 if (file->deps) {
488 for (i = 0; i < file->ndeps; i++)
489 linker_file_unload(file->deps[i]);
490 free(file->deps, M_LINKER);
491 file->deps = NULL;
492 }
493 for (cp = STAILQ_FIRST(&file->common); cp;
494 cp = STAILQ_FIRST(&file->common)) {
495 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
496 free(cp, M_LINKER);
497 }
498
499 LINKER_UNLOAD(file);
500 if (file->filename) {
501 free(file->filename, M_LINKER);
502 file->filename = NULL;
503 }
504 kobj_delete((kobj_t) file, M_LINKER);
505out:
506 return (error);
507}
508
509int
510linker_file_add_dependency(linker_file_t file, linker_file_t dep)
511{
512 linker_file_t *newdeps;
513
514 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
515 M_LINKER, M_WAITOK | M_ZERO);
516 if (newdeps == NULL)
517 return (ENOMEM);
518
519 if (file->deps) {
520 bcopy(file->deps, newdeps,
521 file->ndeps * sizeof(linker_file_t *));
522 free(file->deps, M_LINKER);
523 }
524 file->deps = newdeps;
525 file->deps[file->ndeps] = dep;
526 file->ndeps++;
527 return (0);
528}
529
530/*
531 * Locate a linker set and its contents. This is a helper function to avoid
532 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void ***
533 */
534int
535linker_file_lookup_set(linker_file_t file, const char *name,
536 void *firstp, void *lastp, int *countp)
537{
538
539 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
540}
541
542caddr_t
543linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
544{
545 c_linker_sym_t sym;
546 linker_symval_t symval;
547 caddr_t address;
548 size_t common_size = 0;
552 int err, i;
549 int i;
553
554 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
555 file, name, deps));
556
557 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
558 LINKER_SYMBOL_VALUES(file, sym, &symval);
559 if (symval.value == 0)
560 /*
561 * For commons, first look them up in the
562 * dependencies and only allocate space if not found
563 * there.
564 */
565 common_size = symval.size;
566 else {
567 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
568 ".value=%x\n", symval.value));
569 return (symval.value);
570 }
571 }
572 if (deps) {
573 for (i = 0; i < file->ndeps; i++) {
574 address = linker_file_lookup_symbol(file->deps[i],
575 name, 0);
576 if (address) {
577 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
578 " deps value=%x\n", address));
579 return (address);
580 }
581 }
582 }
583 if (common_size > 0) {
584 /*
585 * This is a common symbol which was not found in the
586 * dependencies. We maintain a simple common symbol table in
587 * the file object.
588 */
589 struct common_symbol *cp;
590
591 STAILQ_FOREACH(cp, &file->common, link) {
550
551 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
552 file, name, deps));
553
554 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
555 LINKER_SYMBOL_VALUES(file, sym, &symval);
556 if (symval.value == 0)
557 /*
558 * For commons, first look them up in the
559 * dependencies and only allocate space if not found
560 * there.
561 */
562 common_size = symval.size;
563 else {
564 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
565 ".value=%x\n", symval.value));
566 return (symval.value);
567 }
568 }
569 if (deps) {
570 for (i = 0; i < file->ndeps; i++) {
571 address = linker_file_lookup_symbol(file->deps[i],
572 name, 0);
573 if (address) {
574 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
575 " deps value=%x\n", address));
576 return (address);
577 }
578 }
579 }
580 if (common_size > 0) {
581 /*
582 * This is a common symbol which was not found in the
583 * dependencies. We maintain a simple common symbol table in
584 * the file object.
585 */
586 struct common_symbol *cp;
587
588 STAILQ_FOREACH(cp, &file->common, link) {
592 err = strcmp(cp->name, name);
593 if (err == 0) {
589 if (strcmp(cp->name, name) == 0) {
594 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
595 " old common value=%x\n", cp->address));
596 return (cp->address);
597 }
598 }
599 /*
600 * Round the symbol size up to align.
601 */
602 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
603 cp = malloc(sizeof(struct common_symbol)
604 + common_size + strlen(name) + 1, M_LINKER,
605 M_WAITOK | M_ZERO);
606 if (cp == NULL) {
607 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
608 return (0);
609 }
610 cp->address = (caddr_t)(cp + 1);
611 cp->name = cp->address + common_size;
612 strcpy(cp->name, name);
613 bzero(cp->address, common_size);
614 STAILQ_INSERT_TAIL(&file->common, cp, link);
615
616 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
617 " value=%x\n", cp->address));
618 return (cp->address);
619 }
620 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
621 return (0);
622}
623
624#ifdef DDB
625/*
626 * DDB Helpers. DDB has to look across multiple files with their own symbol
627 * tables and string tables.
628 *
629 * Note that we do not obey list locking protocols here. We really don't need
630 * DDB to hang because somebody's got the lock held. We'll take the chance
631 * that the files list is inconsistant instead.
632 */
633
634int
635linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
636{
637 linker_file_t lf;
638
639 TAILQ_FOREACH(lf, &linker_files, link) {
640 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
641 return (0);
642 }
643 return (ENOENT);
644}
645
646int
647linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
648{
649 linker_file_t lf;
650 c_linker_sym_t best, es;
651 u_long diff, bestdiff, off;
652
653 best = 0;
654 off = (uintptr_t)value;
655 bestdiff = off;
656 TAILQ_FOREACH(lf, &linker_files, link) {
657 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
658 continue;
659 if (es != 0 && diff < bestdiff) {
660 best = es;
661 bestdiff = diff;
662 }
663 if (bestdiff == 0)
664 break;
665 }
666 if (best) {
667 *sym = best;
668 *diffp = bestdiff;
669 return (0);
670 } else {
671 *sym = 0;
672 *diffp = off;
673 return (ENOENT);
674 }
675}
676
677int
678linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
679{
680 linker_file_t lf;
681
682 TAILQ_FOREACH(lf, &linker_files, link) {
683 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
684 return (0);
685 }
686 return (ENOENT);
687}
688#endif
689
690/*
691 * Syscalls.
692 */
693/*
694 * MPSAFE
695 */
696int
697kldload(struct thread *td, struct kldload_args *uap)
698{
699 char *kldname, *modname;
700 char *pathname = NULL;
701 linker_file_t lf;
702 int error = 0;
703
704 td->td_retval[0] = -1;
705
706 if (securelevel > 0) /* redundant, but that's OK */
707 return (EPERM);
708
709 mtx_lock(&Giant);
710
711 if ((error = suser_xxx(td->td_ucred, NULL, 0)) != 0)
712 goto out;
713
714 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
715 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN,
716 NULL)) != 0)
717 goto out;
718
719 /*
720 * If path do not contain qualified name or any dot in it
721 * (kldname.ko, or kldname.ver.ko) treat it as interface
722 * name.
723 */
724 if (index(pathname, '/') || index(pathname, '.')) {
725 kldname = pathname;
726 modname = NULL;
727 } else {
728 kldname = NULL;
729 modname = pathname;
730 }
731 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
732 if (error)
733 goto out;
734
735 lf->userrefs++;
736 td->td_retval[0] = lf->id;
737out:
738 if (pathname)
739 free(pathname, M_TEMP);
740 mtx_unlock(&Giant);
741 return (error);
742}
743
744/*
745 * MPSAFE
746 */
747int
748kldunload(struct thread *td, struct kldunload_args *uap)
749{
750 linker_file_t lf;
751 int error = 0;
752
753 if (securelevel > 0) /* redundant, but that's OK */
754 return (EPERM);
755
756 mtx_lock(&Giant);
757
758 if ((error = suser_xxx(td->td_ucred, NULL, 0)) != 0)
759 goto out;
760
761 lf = linker_find_file_by_id(SCARG(uap, fileid));
762 if (lf) {
763 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
764 if (lf->userrefs == 0) {
765 printf("kldunload: attempt to unload file that was"
766 " loaded by the kernel\n");
767 error = EBUSY;
768 goto out;
769 }
770 lf->userrefs--;
771 error = linker_file_unload(lf);
772 if (error)
773 lf->userrefs++;
774 } else
775 error = ENOENT;
776out:
777 mtx_unlock(&Giant);
778 return (error);
779}
780
781/*
782 * MPSAFE
783 */
784int
785kldfind(struct thread *td, struct kldfind_args *uap)
786{
787 char *pathname;
788 const char *filename;
789 linker_file_t lf;
790 int error = 0;
791
792 mtx_lock(&Giant);
793 td->td_retval[0] = -1;
794
795 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
796 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN,
797 NULL)) != 0)
798 goto out;
799
800 filename = linker_basename(pathname);
801 lf = linker_find_file_by_name(filename);
802 if (lf)
803 td->td_retval[0] = lf->id;
804 else
805 error = ENOENT;
806out:
807 if (pathname)
808 free(pathname, M_TEMP);
809 mtx_unlock(&Giant);
810 return (error);
811}
812
813/*
814 * MPSAFE
815 */
816int
817kldnext(struct thread *td, struct kldnext_args *uap)
818{
819 linker_file_t lf;
820 int error = 0;
821
822 mtx_lock(&Giant);
823
824 if (SCARG(uap, fileid) == 0) {
825 if (TAILQ_FIRST(&linker_files))
826 td->td_retval[0] = TAILQ_FIRST(&linker_files)->id;
827 else
828 td->td_retval[0] = 0;
829 goto out;
830 }
831 lf = linker_find_file_by_id(SCARG(uap, fileid));
832 if (lf) {
833 if (TAILQ_NEXT(lf, link))
834 td->td_retval[0] = TAILQ_NEXT(lf, link)->id;
835 else
836 td->td_retval[0] = 0;
837 } else
838 error = ENOENT;
839out:
840 mtx_unlock(&Giant);
841 return (error);
842}
843
844/*
845 * MPSAFE
846 */
847int
848kldstat(struct thread *td, struct kldstat_args *uap)
849{
850 linker_file_t lf;
851 int error = 0;
852 int namelen, version;
853 struct kld_file_stat *stat;
854
855 mtx_lock(&Giant);
856
857 lf = linker_find_file_by_id(SCARG(uap, fileid));
858 if (lf == NULL) {
859 error = ENOENT;
860 goto out;
861 }
862 stat = SCARG(uap, stat);
863
864 /*
865 * Check the version of the user's structure.
866 */
867 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
868 goto out;
869 if (version != sizeof(struct kld_file_stat)) {
870 error = EINVAL;
871 goto out;
872 }
873 namelen = strlen(lf->filename) + 1;
874 if (namelen > MAXPATHLEN)
875 namelen = MAXPATHLEN;
876 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
877 goto out;
878 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
879 goto out;
880 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
881 goto out;
882 if ((error = copyout(&lf->address, &stat->address,
883 sizeof(caddr_t))) != 0)
884 goto out;
885 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
886 goto out;
887
888 td->td_retval[0] = 0;
889out:
890 mtx_unlock(&Giant);
891 return (error);
892}
893
894/*
895 * MPSAFE
896 */
897int
898kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
899{
900 linker_file_t lf;
901 module_t mp;
902 int error = 0;
903
904 mtx_lock(&Giant);
905 lf = linker_find_file_by_id(SCARG(uap, fileid));
906 if (lf) {
907 mp = TAILQ_FIRST(&lf->modules);
908 if (mp != NULL)
909 td->td_retval[0] = module_getid(mp);
910 else
911 td->td_retval[0] = 0;
912 } else
913 error = ENOENT;
914 mtx_unlock(&Giant);
915 return (error);
916}
917
918/*
919 * MPSAFE
920 */
921int
922kldsym(struct thread *td, struct kldsym_args *uap)
923{
924 char *symstr = NULL;
925 c_linker_sym_t sym;
926 linker_symval_t symval;
927 linker_file_t lf;
928 struct kld_sym_lookup lookup;
929 int error = 0;
930
931 mtx_lock(&Giant);
932
933 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
934 goto out;
935 if (lookup.version != sizeof(lookup) ||
936 SCARG(uap, cmd) != KLDSYM_LOOKUP) {
937 error = EINVAL;
938 goto out;
939 }
940 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
941 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
942 goto out;
943 if (SCARG(uap, fileid) != 0) {
944 lf = linker_find_file_by_id(SCARG(uap, fileid));
945 if (lf == NULL) {
946 error = ENOENT;
947 goto out;
948 }
949 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
950 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
951 lookup.symvalue = (uintptr_t) symval.value;
952 lookup.symsize = symval.size;
953 error = copyout(&lookup, SCARG(uap, data),
954 sizeof(lookup));
955 } else
956 error = ENOENT;
957 } else {
958 TAILQ_FOREACH(lf, &linker_files, link) {
959 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
960 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
961 lookup.symvalue = (uintptr_t)symval.value;
962 lookup.symsize = symval.size;
963 error = copyout(&lookup, SCARG(uap, data),
964 sizeof(lookup));
965 break;
966 }
967 }
968 if (lf == NULL)
969 error = ENOENT;
970 }
971out:
972 if (symstr)
973 free(symstr, M_TEMP);
974 mtx_unlock(&Giant);
975 return (error);
976}
977
978/*
979 * Preloaded module support
980 */
981
982static modlist_t
983modlist_lookup(const char *name, int ver)
984{
985 modlist_t mod;
590 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
591 " old common value=%x\n", cp->address));
592 return (cp->address);
593 }
594 }
595 /*
596 * Round the symbol size up to align.
597 */
598 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
599 cp = malloc(sizeof(struct common_symbol)
600 + common_size + strlen(name) + 1, M_LINKER,
601 M_WAITOK | M_ZERO);
602 if (cp == NULL) {
603 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
604 return (0);
605 }
606 cp->address = (caddr_t)(cp + 1);
607 cp->name = cp->address + common_size;
608 strcpy(cp->name, name);
609 bzero(cp->address, common_size);
610 STAILQ_INSERT_TAIL(&file->common, cp, link);
611
612 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
613 " value=%x\n", cp->address));
614 return (cp->address);
615 }
616 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
617 return (0);
618}
619
620#ifdef DDB
621/*
622 * DDB Helpers. DDB has to look across multiple files with their own symbol
623 * tables and string tables.
624 *
625 * Note that we do not obey list locking protocols here. We really don't need
626 * DDB to hang because somebody's got the lock held. We'll take the chance
627 * that the files list is inconsistant instead.
628 */
629
630int
631linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
632{
633 linker_file_t lf;
634
635 TAILQ_FOREACH(lf, &linker_files, link) {
636 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
637 return (0);
638 }
639 return (ENOENT);
640}
641
642int
643linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
644{
645 linker_file_t lf;
646 c_linker_sym_t best, es;
647 u_long diff, bestdiff, off;
648
649 best = 0;
650 off = (uintptr_t)value;
651 bestdiff = off;
652 TAILQ_FOREACH(lf, &linker_files, link) {
653 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
654 continue;
655 if (es != 0 && diff < bestdiff) {
656 best = es;
657 bestdiff = diff;
658 }
659 if (bestdiff == 0)
660 break;
661 }
662 if (best) {
663 *sym = best;
664 *diffp = bestdiff;
665 return (0);
666 } else {
667 *sym = 0;
668 *diffp = off;
669 return (ENOENT);
670 }
671}
672
673int
674linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
675{
676 linker_file_t lf;
677
678 TAILQ_FOREACH(lf, &linker_files, link) {
679 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
680 return (0);
681 }
682 return (ENOENT);
683}
684#endif
685
686/*
687 * Syscalls.
688 */
689/*
690 * MPSAFE
691 */
692int
693kldload(struct thread *td, struct kldload_args *uap)
694{
695 char *kldname, *modname;
696 char *pathname = NULL;
697 linker_file_t lf;
698 int error = 0;
699
700 td->td_retval[0] = -1;
701
702 if (securelevel > 0) /* redundant, but that's OK */
703 return (EPERM);
704
705 mtx_lock(&Giant);
706
707 if ((error = suser_xxx(td->td_ucred, NULL, 0)) != 0)
708 goto out;
709
710 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
711 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN,
712 NULL)) != 0)
713 goto out;
714
715 /*
716 * If path do not contain qualified name or any dot in it
717 * (kldname.ko, or kldname.ver.ko) treat it as interface
718 * name.
719 */
720 if (index(pathname, '/') || index(pathname, '.')) {
721 kldname = pathname;
722 modname = NULL;
723 } else {
724 kldname = NULL;
725 modname = pathname;
726 }
727 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
728 if (error)
729 goto out;
730
731 lf->userrefs++;
732 td->td_retval[0] = lf->id;
733out:
734 if (pathname)
735 free(pathname, M_TEMP);
736 mtx_unlock(&Giant);
737 return (error);
738}
739
740/*
741 * MPSAFE
742 */
743int
744kldunload(struct thread *td, struct kldunload_args *uap)
745{
746 linker_file_t lf;
747 int error = 0;
748
749 if (securelevel > 0) /* redundant, but that's OK */
750 return (EPERM);
751
752 mtx_lock(&Giant);
753
754 if ((error = suser_xxx(td->td_ucred, NULL, 0)) != 0)
755 goto out;
756
757 lf = linker_find_file_by_id(SCARG(uap, fileid));
758 if (lf) {
759 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
760 if (lf->userrefs == 0) {
761 printf("kldunload: attempt to unload file that was"
762 " loaded by the kernel\n");
763 error = EBUSY;
764 goto out;
765 }
766 lf->userrefs--;
767 error = linker_file_unload(lf);
768 if (error)
769 lf->userrefs++;
770 } else
771 error = ENOENT;
772out:
773 mtx_unlock(&Giant);
774 return (error);
775}
776
777/*
778 * MPSAFE
779 */
780int
781kldfind(struct thread *td, struct kldfind_args *uap)
782{
783 char *pathname;
784 const char *filename;
785 linker_file_t lf;
786 int error = 0;
787
788 mtx_lock(&Giant);
789 td->td_retval[0] = -1;
790
791 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
792 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN,
793 NULL)) != 0)
794 goto out;
795
796 filename = linker_basename(pathname);
797 lf = linker_find_file_by_name(filename);
798 if (lf)
799 td->td_retval[0] = lf->id;
800 else
801 error = ENOENT;
802out:
803 if (pathname)
804 free(pathname, M_TEMP);
805 mtx_unlock(&Giant);
806 return (error);
807}
808
809/*
810 * MPSAFE
811 */
812int
813kldnext(struct thread *td, struct kldnext_args *uap)
814{
815 linker_file_t lf;
816 int error = 0;
817
818 mtx_lock(&Giant);
819
820 if (SCARG(uap, fileid) == 0) {
821 if (TAILQ_FIRST(&linker_files))
822 td->td_retval[0] = TAILQ_FIRST(&linker_files)->id;
823 else
824 td->td_retval[0] = 0;
825 goto out;
826 }
827 lf = linker_find_file_by_id(SCARG(uap, fileid));
828 if (lf) {
829 if (TAILQ_NEXT(lf, link))
830 td->td_retval[0] = TAILQ_NEXT(lf, link)->id;
831 else
832 td->td_retval[0] = 0;
833 } else
834 error = ENOENT;
835out:
836 mtx_unlock(&Giant);
837 return (error);
838}
839
840/*
841 * MPSAFE
842 */
843int
844kldstat(struct thread *td, struct kldstat_args *uap)
845{
846 linker_file_t lf;
847 int error = 0;
848 int namelen, version;
849 struct kld_file_stat *stat;
850
851 mtx_lock(&Giant);
852
853 lf = linker_find_file_by_id(SCARG(uap, fileid));
854 if (lf == NULL) {
855 error = ENOENT;
856 goto out;
857 }
858 stat = SCARG(uap, stat);
859
860 /*
861 * Check the version of the user's structure.
862 */
863 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
864 goto out;
865 if (version != sizeof(struct kld_file_stat)) {
866 error = EINVAL;
867 goto out;
868 }
869 namelen = strlen(lf->filename) + 1;
870 if (namelen > MAXPATHLEN)
871 namelen = MAXPATHLEN;
872 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
873 goto out;
874 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
875 goto out;
876 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
877 goto out;
878 if ((error = copyout(&lf->address, &stat->address,
879 sizeof(caddr_t))) != 0)
880 goto out;
881 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
882 goto out;
883
884 td->td_retval[0] = 0;
885out:
886 mtx_unlock(&Giant);
887 return (error);
888}
889
890/*
891 * MPSAFE
892 */
893int
894kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
895{
896 linker_file_t lf;
897 module_t mp;
898 int error = 0;
899
900 mtx_lock(&Giant);
901 lf = linker_find_file_by_id(SCARG(uap, fileid));
902 if (lf) {
903 mp = TAILQ_FIRST(&lf->modules);
904 if (mp != NULL)
905 td->td_retval[0] = module_getid(mp);
906 else
907 td->td_retval[0] = 0;
908 } else
909 error = ENOENT;
910 mtx_unlock(&Giant);
911 return (error);
912}
913
914/*
915 * MPSAFE
916 */
917int
918kldsym(struct thread *td, struct kldsym_args *uap)
919{
920 char *symstr = NULL;
921 c_linker_sym_t sym;
922 linker_symval_t symval;
923 linker_file_t lf;
924 struct kld_sym_lookup lookup;
925 int error = 0;
926
927 mtx_lock(&Giant);
928
929 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
930 goto out;
931 if (lookup.version != sizeof(lookup) ||
932 SCARG(uap, cmd) != KLDSYM_LOOKUP) {
933 error = EINVAL;
934 goto out;
935 }
936 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
937 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
938 goto out;
939 if (SCARG(uap, fileid) != 0) {
940 lf = linker_find_file_by_id(SCARG(uap, fileid));
941 if (lf == NULL) {
942 error = ENOENT;
943 goto out;
944 }
945 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
946 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
947 lookup.symvalue = (uintptr_t) symval.value;
948 lookup.symsize = symval.size;
949 error = copyout(&lookup, SCARG(uap, data),
950 sizeof(lookup));
951 } else
952 error = ENOENT;
953 } else {
954 TAILQ_FOREACH(lf, &linker_files, link) {
955 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
956 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
957 lookup.symvalue = (uintptr_t)symval.value;
958 lookup.symsize = symval.size;
959 error = copyout(&lookup, SCARG(uap, data),
960 sizeof(lookup));
961 break;
962 }
963 }
964 if (lf == NULL)
965 error = ENOENT;
966 }
967out:
968 if (symstr)
969 free(symstr, M_TEMP);
970 mtx_unlock(&Giant);
971 return (error);
972}
973
974/*
975 * Preloaded module support
976 */
977
978static modlist_t
979modlist_lookup(const char *name, int ver)
980{
981 modlist_t mod;
986 int err;
987
988 TAILQ_FOREACH(mod, &found_modules, link) {
982
983 TAILQ_FOREACH(mod, &found_modules, link) {
989 err = strcmp(mod->name, name);
990 if (err == 0 && (ver == 0 || mod->version == ver))
984 if (strcmp(mod->name, name) == 0 &&
985 (ver == 0 || mod->version == ver))
991 return (mod);
992 }
993 return (NULL);
994}
995
996static modlist_t
997modlist_lookup2(const char *name, struct mod_depend *verinfo)
998{
999 modlist_t mod, bestmod;
986 return (mod);
987 }
988 return (NULL);
989}
990
991static modlist_t
992modlist_lookup2(const char *name, struct mod_depend *verinfo)
993{
994 modlist_t mod, bestmod;
1000 int err, ver;
995 int ver;
1001
1002 if (verinfo == NULL)
1003 return (modlist_lookup(name, 0));
1004 bestmod = NULL;
1005 for (mod = TAILQ_FIRST(&found_modules); mod;
1006 mod = TAILQ_NEXT(mod, link)) {
996
997 if (verinfo == NULL)
998 return (modlist_lookup(name, 0));
999 bestmod = NULL;
1000 for (mod = TAILQ_FIRST(&found_modules); mod;
1001 mod = TAILQ_NEXT(mod, link)) {
1007 err = strcmp(mod->name, name);
1008 if (err != 0)
1002 if (strcmp(mod->name, name) != 0)
1009 continue;
1010 ver = mod->version;
1011 if (ver == verinfo->md_ver_preferred)
1012 return (mod);
1013 if (ver >= verinfo->md_ver_minimum &&
1014 ver <= verinfo->md_ver_maximum &&
1015 ver > bestmod->version)
1016 bestmod = mod;
1017 }
1018 return (bestmod);
1019}
1020
1021static modlist_t
1022modlist_newmodule(const char *modname, int version, linker_file_t container)
1023{
1024 modlist_t mod;
1025
1026 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT);
1027 if (mod == NULL)
1028 panic("no memory for module list");
1029 bzero(mod, sizeof(*mod));
1030 mod->container = container;
1031 mod->name = modname;
1032 mod->version = version;
1033 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1034 return (mod);
1035}
1036
1037/*
1038 * This routine is cheap and nasty but will work for data pointers.
1039 */
1040static void *
1041linker_reloc_ptr(linker_file_t lf, const void *offset)
1042{
1043 return (lf->address + (uintptr_t)offset);
1044}
1045
1046/*
1047 * Dereference MDT_VERSION metadata into module name and version
1048 */
1049static void
1050linker_mdt_version(linker_file_t lf, struct mod_metadata *mp,
1051 const char **modname, int *version)
1052{
1053 struct mod_version *mvp;
1054
1055 if (modname)
1056 *modname = linker_reloc_ptr(lf, mp->md_cval);
1057 if (version) {
1058 mvp = linker_reloc_ptr(lf, mp->md_data);
1059 *version = mvp->mv_version;
1060 }
1061}
1062
1063/*
1064 * Dereference MDT_DEPEND metadata into module name and mod_depend structure
1065 */
1066static void
1067linker_mdt_depend(linker_file_t lf, struct mod_metadata *mp,
1068 const char **modname, struct mod_depend **verinfo)
1069{
1070
1071 if (modname)
1072 *modname = linker_reloc_ptr(lf, mp->md_cval);
1073 if (verinfo)
1074 *verinfo = linker_reloc_ptr(lf, mp->md_data);
1075}
1076
1077static void
1078linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1079 struct mod_metadata **stop, int preload)
1080{
1081 struct mod_metadata *mp, **mdp;
1082 const char *modname;
1083 int ver;
1084
1085 for (mdp = start; mdp < stop; mdp++) {
1086 if (preload)
1087 mp = *mdp;
1088 else
1089 mp = linker_reloc_ptr(lf, *mdp);
1090 if (mp->md_type != MDT_VERSION)
1091 continue;
1092 if (preload) {
1093 modname = mp->md_cval;
1094 ver = ((struct mod_version *)mp->md_data)->mv_version;
1095 } else
1096 linker_mdt_version(lf, mp, &modname, &ver);
1097 if (modlist_lookup(modname, ver) != NULL) {
1098 printf("module %s already present!\n", modname);
1099 /* XXX what can we do? this is a build error. :-( */
1100 continue;
1101 }
1102 modlist_newmodule(modname, ver, lf);
1103 }
1104}
1105
1106static void
1107linker_preload(void *arg)
1108{
1109 caddr_t modptr;
1110 const char *modname, *nmodname;
1111 char *modtype;
1112 linker_file_t lf;
1113 linker_class_t lc;
1003 continue;
1004 ver = mod->version;
1005 if (ver == verinfo->md_ver_preferred)
1006 return (mod);
1007 if (ver >= verinfo->md_ver_minimum &&
1008 ver <= verinfo->md_ver_maximum &&
1009 ver > bestmod->version)
1010 bestmod = mod;
1011 }
1012 return (bestmod);
1013}
1014
1015static modlist_t
1016modlist_newmodule(const char *modname, int version, linker_file_t container)
1017{
1018 modlist_t mod;
1019
1020 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT);
1021 if (mod == NULL)
1022 panic("no memory for module list");
1023 bzero(mod, sizeof(*mod));
1024 mod->container = container;
1025 mod->name = modname;
1026 mod->version = version;
1027 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1028 return (mod);
1029}
1030
1031/*
1032 * This routine is cheap and nasty but will work for data pointers.
1033 */
1034static void *
1035linker_reloc_ptr(linker_file_t lf, const void *offset)
1036{
1037 return (lf->address + (uintptr_t)offset);
1038}
1039
1040/*
1041 * Dereference MDT_VERSION metadata into module name and version
1042 */
1043static void
1044linker_mdt_version(linker_file_t lf, struct mod_metadata *mp,
1045 const char **modname, int *version)
1046{
1047 struct mod_version *mvp;
1048
1049 if (modname)
1050 *modname = linker_reloc_ptr(lf, mp->md_cval);
1051 if (version) {
1052 mvp = linker_reloc_ptr(lf, mp->md_data);
1053 *version = mvp->mv_version;
1054 }
1055}
1056
1057/*
1058 * Dereference MDT_DEPEND metadata into module name and mod_depend structure
1059 */
1060static void
1061linker_mdt_depend(linker_file_t lf, struct mod_metadata *mp,
1062 const char **modname, struct mod_depend **verinfo)
1063{
1064
1065 if (modname)
1066 *modname = linker_reloc_ptr(lf, mp->md_cval);
1067 if (verinfo)
1068 *verinfo = linker_reloc_ptr(lf, mp->md_data);
1069}
1070
1071static void
1072linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1073 struct mod_metadata **stop, int preload)
1074{
1075 struct mod_metadata *mp, **mdp;
1076 const char *modname;
1077 int ver;
1078
1079 for (mdp = start; mdp < stop; mdp++) {
1080 if (preload)
1081 mp = *mdp;
1082 else
1083 mp = linker_reloc_ptr(lf, *mdp);
1084 if (mp->md_type != MDT_VERSION)
1085 continue;
1086 if (preload) {
1087 modname = mp->md_cval;
1088 ver = ((struct mod_version *)mp->md_data)->mv_version;
1089 } else
1090 linker_mdt_version(lf, mp, &modname, &ver);
1091 if (modlist_lookup(modname, ver) != NULL) {
1092 printf("module %s already present!\n", modname);
1093 /* XXX what can we do? this is a build error. :-( */
1094 continue;
1095 }
1096 modlist_newmodule(modname, ver, lf);
1097 }
1098}
1099
1100static void
1101linker_preload(void *arg)
1102{
1103 caddr_t modptr;
1104 const char *modname, *nmodname;
1105 char *modtype;
1106 linker_file_t lf;
1107 linker_class_t lc;
1114 int error, err;
1108 int error;
1115 linker_file_list_t loaded_files;
1116 linker_file_list_t depended_files;
1117 struct mod_metadata *mp, *nmp;
1118 struct mod_metadata **start, **stop, **mdp, **nmdp;
1119 struct mod_depend *verinfo;
1120 int nver;
1121 int resolves;
1122 modlist_t mod;
1123 struct sysinit **si_start, **si_stop;
1124
1125 TAILQ_INIT(&loaded_files);
1126 TAILQ_INIT(&depended_files);
1127 TAILQ_INIT(&found_modules);
1128 error = 0;
1129
1130 modptr = NULL;
1131 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1132 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1133 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1134 if (modname == NULL) {
1135 printf("Preloaded module at %p does not have a"
1136 " name!\n", modptr);
1137 continue;
1138 }
1139 if (modtype == NULL) {
1140 printf("Preloaded module at %p does not have a type!\n",
1141 modptr);
1142 continue;
1143 }
1144 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1145 modptr);
1146 lf = NULL;
1147 TAILQ_FOREACH(lc, &classes, link) {
1148 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1149 if (error) {
1150 lf = NULL;
1151 break;
1152 }
1153 }
1154 if (lf)
1155 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1156 }
1157
1158 /*
1159 * First get a list of stuff in the kernel.
1160 */
1161 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1162 &stop, NULL) == 0)
1163 linker_addmodules(linker_kernel_file, start, stop, 1);
1164
1165 /*
1166 * this is a once-off kinky bubble sort resolve relocation dependency
1167 * requirements
1168 */
1169restart:
1170 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1171 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1172 &stop, NULL);
1173 /*
1174 * First, look to see if we would successfully link with this
1175 * stuff.
1176 */
1177 resolves = 1; /* unless we know otherwise */
1178 if (!error) {
1179 for (mdp = start; mdp < stop; mdp++) {
1180 mp = linker_reloc_ptr(lf, *mdp);
1181 if (mp->md_type != MDT_DEPEND)
1182 continue;
1183 linker_mdt_depend(lf, mp, &modname, &verinfo);
1184 for (nmdp = start; nmdp < stop; nmdp++) {
1185 nmp = linker_reloc_ptr(lf, *nmdp);
1186 if (nmp->md_type != MDT_VERSION)
1187 continue;
1188 linker_mdt_version(lf, nmp, &nmodname,
1189 NULL);
1190 nmodname = linker_reloc_ptr(lf,
1191 nmp->md_cval);
1109 linker_file_list_t loaded_files;
1110 linker_file_list_t depended_files;
1111 struct mod_metadata *mp, *nmp;
1112 struct mod_metadata **start, **stop, **mdp, **nmdp;
1113 struct mod_depend *verinfo;
1114 int nver;
1115 int resolves;
1116 modlist_t mod;
1117 struct sysinit **si_start, **si_stop;
1118
1119 TAILQ_INIT(&loaded_files);
1120 TAILQ_INIT(&depended_files);
1121 TAILQ_INIT(&found_modules);
1122 error = 0;
1123
1124 modptr = NULL;
1125 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1126 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1127 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1128 if (modname == NULL) {
1129 printf("Preloaded module at %p does not have a"
1130 " name!\n", modptr);
1131 continue;
1132 }
1133 if (modtype == NULL) {
1134 printf("Preloaded module at %p does not have a type!\n",
1135 modptr);
1136 continue;
1137 }
1138 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1139 modptr);
1140 lf = NULL;
1141 TAILQ_FOREACH(lc, &classes, link) {
1142 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1143 if (error) {
1144 lf = NULL;
1145 break;
1146 }
1147 }
1148 if (lf)
1149 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1150 }
1151
1152 /*
1153 * First get a list of stuff in the kernel.
1154 */
1155 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1156 &stop, NULL) == 0)
1157 linker_addmodules(linker_kernel_file, start, stop, 1);
1158
1159 /*
1160 * this is a once-off kinky bubble sort resolve relocation dependency
1161 * requirements
1162 */
1163restart:
1164 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1165 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1166 &stop, NULL);
1167 /*
1168 * First, look to see if we would successfully link with this
1169 * stuff.
1170 */
1171 resolves = 1; /* unless we know otherwise */
1172 if (!error) {
1173 for (mdp = start; mdp < stop; mdp++) {
1174 mp = linker_reloc_ptr(lf, *mdp);
1175 if (mp->md_type != MDT_DEPEND)
1176 continue;
1177 linker_mdt_depend(lf, mp, &modname, &verinfo);
1178 for (nmdp = start; nmdp < stop; nmdp++) {
1179 nmp = linker_reloc_ptr(lf, *nmdp);
1180 if (nmp->md_type != MDT_VERSION)
1181 continue;
1182 linker_mdt_version(lf, nmp, &nmodname,
1183 NULL);
1184 nmodname = linker_reloc_ptr(lf,
1185 nmp->md_cval);
1192 err = strcmp(modname, nmodname);
1193 if (err == 0)
1186 if (strcmp(modname, nmodname) == 0)
1194 break;
1195 }
1196 if (nmdp < stop) /* it's a self reference */
1197 continue;
1198
1199 /*
1200 * ok, the module isn't here yet, we
1201 * are not finished
1202 */
1203 if (modlist_lookup2(modname, verinfo) == NULL)
1204 resolves = 0;
1205 }
1206 }
1207 /*
1208 * OK, if we found our modules, we can link. So, "provide"
1209 * the modules inside and add it to the end of the link order
1210 * list.
1211 */
1212 if (resolves) {
1213 if (!error) {
1214 for (mdp = start; mdp < stop; mdp++) {
1215 mp = linker_reloc_ptr(lf, *mdp);
1216 if (mp->md_type != MDT_VERSION)
1217 continue;
1218 linker_mdt_version(lf, mp,
1219 &modname, &nver);
1220 if (modlist_lookup(modname,
1221 nver) != NULL) {
1222 printf("module %s already"
1223 " present!\n", modname);
1224 linker_file_unload(lf);
1225 TAILQ_REMOVE(&loaded_files,
1226 lf, loaded);
1227 /* we changed tailq next ptr */
1228 goto restart;
1229 }
1230 modlist_newmodule(modname, nver, lf);
1231 }
1232 }
1233 TAILQ_REMOVE(&loaded_files, lf, loaded);
1234 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1235 /*
1236 * Since we provided modules, we need to restart the
1237 * sort so that the previous files that depend on us
1238 * have a chance. Also, we've busted the tailq next
1239 * pointer with the REMOVE.
1240 */
1241 goto restart;
1242 }
1243 }
1244
1245 /*
1246 * At this point, we check to see what could not be resolved..
1247 */
1248 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1249 printf("KLD file %s is missing dependencies\n", lf->filename);
1250 linker_file_unload(lf);
1251 TAILQ_REMOVE(&loaded_files, lf, loaded);
1252 }
1253
1254 /*
1255 * We made it. Finish off the linking in the order we determined.
1256 */
1257 TAILQ_FOREACH(lf, &depended_files, loaded) {
1258 if (linker_kernel_file) {
1259 linker_kernel_file->refs++;
1260 error = linker_file_add_dependency(lf,
1261 linker_kernel_file);
1262 if (error)
1263 panic("cannot add dependency");
1264 }
1265 lf->userrefs++; /* so we can (try to) kldunload it */
1266 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1267 &stop, NULL);
1268 if (!error) {
1269 for (mdp = start; mdp < stop; mdp++) {
1270 mp = linker_reloc_ptr(lf, *mdp);
1271 if (mp->md_type != MDT_DEPEND)
1272 continue;
1273 linker_mdt_depend(lf, mp, &modname, &verinfo);
1274 mod = modlist_lookup2(modname, verinfo);
1275 mod->container->refs++;
1276 error = linker_file_add_dependency(lf,
1277 mod->container);
1278 if (error)
1279 panic("cannot add dependency");
1280 }
1281 }
1282 /*
1283 * Now do relocation etc using the symbol search paths
1284 * established by the dependencies
1285 */
1286 error = LINKER_LINK_PRELOAD_FINISH(lf);
1287 if (error) {
1288 printf("KLD file %s - could not finalize loading\n",
1289 lf->filename);
1290 linker_file_unload(lf);
1291 continue;
1292 }
1293 linker_file_register_modules(lf);
1294 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1295 &si_stop, NULL) == 0)
1296 sysinit_add(si_start, si_stop);
1297 linker_file_register_sysctls(lf);
1298 lf->flags |= LINKER_FILE_LINKED;
1299 }
1300 /* woohoo! we made it! */
1301}
1302
1303SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0)
1304
1305/*
1306 * Search for a not-loaded module by name.
1307 *
1308 * Modules may be found in the following locations:
1309 *
1310 * - preloaded (result is just the module name) - on disk (result is full path
1311 * to module)
1312 *
1313 * If the module name is qualified in any way (contains path, etc.) the we
1314 * simply return a copy of it.
1315 *
1316 * The search path can be manipulated via sysctl. Note that we use the ';'
1317 * character as a separator to be consistent with the bootloader.
1318 */
1319
1320static char linker_hintfile[] = "linker.hints";
1321static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules;/modules";
1322
1323SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1324 sizeof(linker_path), "module load search path");
1325
1326TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1327
1328static char *linker_ext_list[] = {
1329 "",
1330 ".ko",
1331 NULL
1332};
1333
1334/*
1335 * Check if file actually exists either with or without extension listed in
1336 * the linker_ext_list. (probably should be generic for the rest of the
1337 * kernel)
1338 */
1339static char *
1340linker_lookup_file(const char *path, int pathlen, const char *name,
1341 int namelen, struct vattr *vap)
1342{
1343 struct nameidata nd;
1344 struct thread *td = curthread; /* XXX */
1345 char *result, **cpp, *sep;
1346 int error, len, extlen, reclen, flags;
1347 enum vtype type;
1348
1349 extlen = 0;
1350 for (cpp = linker_ext_list; *cpp; cpp++) {
1351 len = strlen(*cpp);
1352 if (len > extlen)
1353 extlen = len;
1354 }
1355 extlen++; /* trailing '\0' */
1356 sep = (path[pathlen - 1] != '/') ? "/" : "";
1357
1358 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1359 result = malloc(reclen, M_LINKER, M_WAITOK);
1360 for (cpp = linker_ext_list; *cpp; cpp++) {
1361 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1362 namelen, name, *cpp);
1363 /*
1364 * Attempt to open the file, and return the path if
1365 * we succeed and it's a regular file.
1366 */
1367 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1368 flags = FREAD;
1369 error = vn_open(&nd, &flags, 0);
1370 if (error == 0) {
1371 NDFREE(&nd, NDF_ONLY_PNBUF);
1372 type = nd.ni_vp->v_type;
1373 if (vap)
1374 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td);
1375 VOP_UNLOCK(nd.ni_vp, 0, td);
1376 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1377 if (type == VREG)
1378 return (result);
1379 }
1380 }
1381 free(result, M_LINKER);
1382 return (NULL);
1383}
1384
1385#define INT_ALIGN(base, ptr) ptr = \
1386 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1387
1388/*
1389 * Lookup KLD which contains requested module in the "linker.hints" file. If
1390 * version specification is available, then try to find the best KLD.
1391 * Otherwise just find the latest one.
1392 *
1393 * XXX: Vnode locking here is hosed; lock should be held for calls to
1394 * VOP_GETATTR() and vn_rdwr().
1395 */
1396static char *
1397linker_hints_lookup(const char *path, int pathlen, const char *modname,
1398 int modnamelen, struct mod_depend *verinfo)
1399{
1400 struct thread *td = curthread; /* XXX */
1401 struct ucred *cred = td ? td->td_ucred : NULL;
1402 struct nameidata nd;
1403 struct vattr vattr, mattr;
1404 u_char *hints = NULL;
1405 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1406 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1407
1408 result = NULL;
1409 bestver = found = 0;
1410
1411 sep = (path[pathlen - 1] != '/') ? "/" : "";
1412 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1413 strlen(sep) + 1;
1414 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1415 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1416 linker_hintfile);
1417
1418 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1419 flags = FREAD;
1420 error = vn_open(&nd, &flags, 0);
1421 if (error)
1422 goto bad;
1423 NDFREE(&nd, NDF_ONLY_PNBUF);
1424 VOP_UNLOCK(nd.ni_vp, 0, td);
1425 if (nd.ni_vp->v_type != VREG)
1426 goto bad;
1427 best = cp = NULL;
1428 error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td);
1429 if (error)
1430 goto bad;
1431 /*
1432 * XXX: we need to limit this number to some reasonable value
1433 */
1434 if (vattr.va_size > 100 * 1024) {
1435 printf("hints file too large %ld\n", (long)vattr.va_size);
1436 goto bad;
1437 }
1438 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1439 if (hints == NULL)
1440 goto bad;
1441 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1442 UIO_SYSSPACE, IO_NODELOCKED, cred, &reclen, td);
1443 if (error)
1444 goto bad;
1445 vn_close(nd.ni_vp, FREAD, cred, td);
1446 nd.ni_vp = NULL;
1447 if (reclen != 0) {
1448 printf("can't read %d\n", reclen);
1449 goto bad;
1450 }
1451 intp = (int *)hints;
1452 ival = *intp++;
1453 if (ival != LINKER_HINTS_VERSION) {
1454 printf("hints file version mismatch %d\n", ival);
1455 goto bad;
1456 }
1457 bufend = hints + vattr.va_size;
1458 recptr = (u_char *)intp;
1459 clen = blen = 0;
1460 while (recptr < bufend && !found) {
1461 intp = (int *)recptr;
1462 reclen = *intp++;
1463 ival = *intp++;
1464 cp = (char *)intp;
1465 switch (ival) {
1466 case MDT_VERSION:
1467 clen = *cp++;
1468 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1469 break;
1470 cp += clen;
1471 INT_ALIGN(hints, cp);
1472 ival = *(int *)cp;
1473 cp += sizeof(int);
1474 clen = *cp++;
1475 if (verinfo == NULL ||
1476 ival == verinfo->md_ver_preferred) {
1477 found = 1;
1478 break;
1479 }
1480 if (ival >= verinfo->md_ver_minimum &&
1481 ival <= verinfo->md_ver_maximum &&
1482 ival > bestver) {
1483 bestver = ival;
1484 best = cp;
1485 blen = clen;
1486 }
1487 break;
1488 default:
1489 break;
1490 }
1491 recptr += reclen + sizeof(int);
1492 }
1493 /*
1494 * Finally check if KLD is in the place
1495 */
1496 if (found)
1497 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1498 else if (best)
1499 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1500
1501 /*
1502 * KLD is newer than hints file. What we should do now?
1503 */
1504 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1505 printf("warning: KLD '%s' is newer than the linker.hints"
1506 " file\n", result);
1507bad:
1508 if (hints)
1509 free(hints, M_TEMP);
1510 if (nd.ni_vp != NULL)
1511 vn_close(nd.ni_vp, FREAD, cred, td);
1512 /*
1513 * If nothing found or hints is absent - fallback to the old
1514 * way by using "kldname[.ko]" as module name.
1515 */
1516 if (!found && !bestver && result == NULL)
1517 result = linker_lookup_file(path, pathlen, modname,
1518 modnamelen, NULL);
1519 return (result);
1520}
1521
1522/*
1523 * Lookup KLD which contains requested module in the all directories.
1524 */
1525static char *
1526linker_search_module(const char *modname, int modnamelen,
1527 struct mod_depend *verinfo)
1528{
1529 char *cp, *ep, *result;
1530
1531 /*
1532 * traverse the linker path
1533 */
1534 for (cp = linker_path; *cp; cp = ep + 1) {
1535 /* find the end of this component */
1536 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1537 result = linker_hints_lookup(cp, ep - cp, modname,
1538 modnamelen, verinfo);
1539 if (result != NULL)
1540 return (result);
1541 if (*ep == 0)
1542 break;
1543 }
1544 return (NULL);
1545}
1546
1547/*
1548 * Search for module in all directories listed in the linker_path.
1549 */
1550static char *
1551linker_search_kld(const char *name)
1552{
1553 char *cp, *ep, *result, **cpp;
1554 int extlen, len;
1555
1556 /* qualified at all? */
1557 if (index(name, '/'))
1558 return (linker_strdup(name));
1559
1560 extlen = 0;
1561 for (cpp = linker_ext_list; *cpp; cpp++) {
1562 len = strlen(*cpp);
1563 if (len > extlen)
1564 extlen = len;
1565 }
1566 extlen++; /* trailing '\0' */
1567
1568 /* traverse the linker path */
1569 len = strlen(name);
1570 for (ep = linker_path; *ep; ep++) {
1571 cp = ep;
1572 /* find the end of this component */
1573 for (; *ep != 0 && *ep != ';'; ep++);
1574 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1575 if (result != NULL)
1576 return (result);
1577 }
1578 return (NULL);
1579}
1580
1581static const char *
1582linker_basename(const char *path)
1583{
1584 const char *filename;
1585
1586 filename = rindex(path, '/');
1587 if (filename == NULL)
1588 return path;
1589 if (filename[1])
1590 filename++;
1591 return (filename);
1592}
1593
1594/*
1595 * Find a file which contains given module and load it, if "parent" is not
1596 * NULL, register a reference to it.
1597 */
1598static int
1599linker_load_module(const char *kldname, const char *modname,
1600 struct linker_file *parent, struct mod_depend *verinfo,
1601 struct linker_file **lfpp)
1602{
1603 linker_file_t lfdep;
1604 const char *filename;
1605 char *pathname;
1606 int error;
1607
1608 if (modname == NULL) {
1609 /*
1610 * We have to load KLD
1611 */
1612 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1613 " is not NULL"));
1614 pathname = linker_search_kld(kldname);
1615 } else {
1616 if (modlist_lookup2(modname, verinfo) != NULL)
1617 return (EEXIST);
1618 if (kldname == NULL)
1619 /*
1620 * Need to find a KLD with required module
1621 */
1622 pathname = linker_search_module(modname,
1623 strlen(modname), verinfo);
1624 else
1625 pathname = linker_strdup(kldname);
1626 }
1627 if (pathname == NULL)
1628 return (ENOENT);
1629
1630 /*
1631 * Can't load more than one file with the same basename XXX:
1632 * Actually it should be possible to have multiple KLDs with
1633 * the same basename but different path because they can
1634 * provide different versions of the same modules.
1635 */
1636 filename = linker_basename(pathname);
1637 if (linker_find_file_by_name(filename)) {
1638 error = EEXIST;
1639 goto out;
1640 }
1641 do {
1642 error = linker_load_file(pathname, &lfdep);
1643 if (error)
1644 break;
1645 if (modname && verinfo &&
1646 modlist_lookup2(modname, verinfo) == NULL) {
1647 linker_file_unload(lfdep);
1648 error = ENOENT;
1649 break;
1650 }
1651 if (parent) {
1652 error = linker_file_add_dependency(parent, lfdep);
1653 if (error)
1654 break;
1655 }
1656 if (lfpp)
1657 *lfpp = lfdep;
1658 } while (0);
1659out:
1660 if (pathname)
1661 free(pathname, M_LINKER);
1662 return (error);
1663}
1664
1665/*
1666 * This routine is responsible for finding dependencies of userland initiated
1667 * kldload(2)'s of files.
1668 */
1669int
1670linker_load_dependencies(linker_file_t lf)
1671{
1672 linker_file_t lfdep;
1673 struct mod_metadata **start, **stop, **mdp, **nmdp;
1674 struct mod_metadata *mp, *nmp;
1675 struct mod_depend *verinfo;
1676 modlist_t mod;
1677 const char *modname, *nmodname;
1187 break;
1188 }
1189 if (nmdp < stop) /* it's a self reference */
1190 continue;
1191
1192 /*
1193 * ok, the module isn't here yet, we
1194 * are not finished
1195 */
1196 if (modlist_lookup2(modname, verinfo) == NULL)
1197 resolves = 0;
1198 }
1199 }
1200 /*
1201 * OK, if we found our modules, we can link. So, "provide"
1202 * the modules inside and add it to the end of the link order
1203 * list.
1204 */
1205 if (resolves) {
1206 if (!error) {
1207 for (mdp = start; mdp < stop; mdp++) {
1208 mp = linker_reloc_ptr(lf, *mdp);
1209 if (mp->md_type != MDT_VERSION)
1210 continue;
1211 linker_mdt_version(lf, mp,
1212 &modname, &nver);
1213 if (modlist_lookup(modname,
1214 nver) != NULL) {
1215 printf("module %s already"
1216 " present!\n", modname);
1217 linker_file_unload(lf);
1218 TAILQ_REMOVE(&loaded_files,
1219 lf, loaded);
1220 /* we changed tailq next ptr */
1221 goto restart;
1222 }
1223 modlist_newmodule(modname, nver, lf);
1224 }
1225 }
1226 TAILQ_REMOVE(&loaded_files, lf, loaded);
1227 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1228 /*
1229 * Since we provided modules, we need to restart the
1230 * sort so that the previous files that depend on us
1231 * have a chance. Also, we've busted the tailq next
1232 * pointer with the REMOVE.
1233 */
1234 goto restart;
1235 }
1236 }
1237
1238 /*
1239 * At this point, we check to see what could not be resolved..
1240 */
1241 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1242 printf("KLD file %s is missing dependencies\n", lf->filename);
1243 linker_file_unload(lf);
1244 TAILQ_REMOVE(&loaded_files, lf, loaded);
1245 }
1246
1247 /*
1248 * We made it. Finish off the linking in the order we determined.
1249 */
1250 TAILQ_FOREACH(lf, &depended_files, loaded) {
1251 if (linker_kernel_file) {
1252 linker_kernel_file->refs++;
1253 error = linker_file_add_dependency(lf,
1254 linker_kernel_file);
1255 if (error)
1256 panic("cannot add dependency");
1257 }
1258 lf->userrefs++; /* so we can (try to) kldunload it */
1259 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1260 &stop, NULL);
1261 if (!error) {
1262 for (mdp = start; mdp < stop; mdp++) {
1263 mp = linker_reloc_ptr(lf, *mdp);
1264 if (mp->md_type != MDT_DEPEND)
1265 continue;
1266 linker_mdt_depend(lf, mp, &modname, &verinfo);
1267 mod = modlist_lookup2(modname, verinfo);
1268 mod->container->refs++;
1269 error = linker_file_add_dependency(lf,
1270 mod->container);
1271 if (error)
1272 panic("cannot add dependency");
1273 }
1274 }
1275 /*
1276 * Now do relocation etc using the symbol search paths
1277 * established by the dependencies
1278 */
1279 error = LINKER_LINK_PRELOAD_FINISH(lf);
1280 if (error) {
1281 printf("KLD file %s - could not finalize loading\n",
1282 lf->filename);
1283 linker_file_unload(lf);
1284 continue;
1285 }
1286 linker_file_register_modules(lf);
1287 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1288 &si_stop, NULL) == 0)
1289 sysinit_add(si_start, si_stop);
1290 linker_file_register_sysctls(lf);
1291 lf->flags |= LINKER_FILE_LINKED;
1292 }
1293 /* woohoo! we made it! */
1294}
1295
1296SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0)
1297
1298/*
1299 * Search for a not-loaded module by name.
1300 *
1301 * Modules may be found in the following locations:
1302 *
1303 * - preloaded (result is just the module name) - on disk (result is full path
1304 * to module)
1305 *
1306 * If the module name is qualified in any way (contains path, etc.) the we
1307 * simply return a copy of it.
1308 *
1309 * The search path can be manipulated via sysctl. Note that we use the ';'
1310 * character as a separator to be consistent with the bootloader.
1311 */
1312
1313static char linker_hintfile[] = "linker.hints";
1314static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules;/modules";
1315
1316SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1317 sizeof(linker_path), "module load search path");
1318
1319TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1320
1321static char *linker_ext_list[] = {
1322 "",
1323 ".ko",
1324 NULL
1325};
1326
1327/*
1328 * Check if file actually exists either with or without extension listed in
1329 * the linker_ext_list. (probably should be generic for the rest of the
1330 * kernel)
1331 */
1332static char *
1333linker_lookup_file(const char *path, int pathlen, const char *name,
1334 int namelen, struct vattr *vap)
1335{
1336 struct nameidata nd;
1337 struct thread *td = curthread; /* XXX */
1338 char *result, **cpp, *sep;
1339 int error, len, extlen, reclen, flags;
1340 enum vtype type;
1341
1342 extlen = 0;
1343 for (cpp = linker_ext_list; *cpp; cpp++) {
1344 len = strlen(*cpp);
1345 if (len > extlen)
1346 extlen = len;
1347 }
1348 extlen++; /* trailing '\0' */
1349 sep = (path[pathlen - 1] != '/') ? "/" : "";
1350
1351 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1352 result = malloc(reclen, M_LINKER, M_WAITOK);
1353 for (cpp = linker_ext_list; *cpp; cpp++) {
1354 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1355 namelen, name, *cpp);
1356 /*
1357 * Attempt to open the file, and return the path if
1358 * we succeed and it's a regular file.
1359 */
1360 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1361 flags = FREAD;
1362 error = vn_open(&nd, &flags, 0);
1363 if (error == 0) {
1364 NDFREE(&nd, NDF_ONLY_PNBUF);
1365 type = nd.ni_vp->v_type;
1366 if (vap)
1367 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td);
1368 VOP_UNLOCK(nd.ni_vp, 0, td);
1369 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1370 if (type == VREG)
1371 return (result);
1372 }
1373 }
1374 free(result, M_LINKER);
1375 return (NULL);
1376}
1377
1378#define INT_ALIGN(base, ptr) ptr = \
1379 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1380
1381/*
1382 * Lookup KLD which contains requested module in the "linker.hints" file. If
1383 * version specification is available, then try to find the best KLD.
1384 * Otherwise just find the latest one.
1385 *
1386 * XXX: Vnode locking here is hosed; lock should be held for calls to
1387 * VOP_GETATTR() and vn_rdwr().
1388 */
1389static char *
1390linker_hints_lookup(const char *path, int pathlen, const char *modname,
1391 int modnamelen, struct mod_depend *verinfo)
1392{
1393 struct thread *td = curthread; /* XXX */
1394 struct ucred *cred = td ? td->td_ucred : NULL;
1395 struct nameidata nd;
1396 struct vattr vattr, mattr;
1397 u_char *hints = NULL;
1398 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1399 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1400
1401 result = NULL;
1402 bestver = found = 0;
1403
1404 sep = (path[pathlen - 1] != '/') ? "/" : "";
1405 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1406 strlen(sep) + 1;
1407 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1408 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1409 linker_hintfile);
1410
1411 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1412 flags = FREAD;
1413 error = vn_open(&nd, &flags, 0);
1414 if (error)
1415 goto bad;
1416 NDFREE(&nd, NDF_ONLY_PNBUF);
1417 VOP_UNLOCK(nd.ni_vp, 0, td);
1418 if (nd.ni_vp->v_type != VREG)
1419 goto bad;
1420 best = cp = NULL;
1421 error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td);
1422 if (error)
1423 goto bad;
1424 /*
1425 * XXX: we need to limit this number to some reasonable value
1426 */
1427 if (vattr.va_size > 100 * 1024) {
1428 printf("hints file too large %ld\n", (long)vattr.va_size);
1429 goto bad;
1430 }
1431 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1432 if (hints == NULL)
1433 goto bad;
1434 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1435 UIO_SYSSPACE, IO_NODELOCKED, cred, &reclen, td);
1436 if (error)
1437 goto bad;
1438 vn_close(nd.ni_vp, FREAD, cred, td);
1439 nd.ni_vp = NULL;
1440 if (reclen != 0) {
1441 printf("can't read %d\n", reclen);
1442 goto bad;
1443 }
1444 intp = (int *)hints;
1445 ival = *intp++;
1446 if (ival != LINKER_HINTS_VERSION) {
1447 printf("hints file version mismatch %d\n", ival);
1448 goto bad;
1449 }
1450 bufend = hints + vattr.va_size;
1451 recptr = (u_char *)intp;
1452 clen = blen = 0;
1453 while (recptr < bufend && !found) {
1454 intp = (int *)recptr;
1455 reclen = *intp++;
1456 ival = *intp++;
1457 cp = (char *)intp;
1458 switch (ival) {
1459 case MDT_VERSION:
1460 clen = *cp++;
1461 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1462 break;
1463 cp += clen;
1464 INT_ALIGN(hints, cp);
1465 ival = *(int *)cp;
1466 cp += sizeof(int);
1467 clen = *cp++;
1468 if (verinfo == NULL ||
1469 ival == verinfo->md_ver_preferred) {
1470 found = 1;
1471 break;
1472 }
1473 if (ival >= verinfo->md_ver_minimum &&
1474 ival <= verinfo->md_ver_maximum &&
1475 ival > bestver) {
1476 bestver = ival;
1477 best = cp;
1478 blen = clen;
1479 }
1480 break;
1481 default:
1482 break;
1483 }
1484 recptr += reclen + sizeof(int);
1485 }
1486 /*
1487 * Finally check if KLD is in the place
1488 */
1489 if (found)
1490 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1491 else if (best)
1492 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1493
1494 /*
1495 * KLD is newer than hints file. What we should do now?
1496 */
1497 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1498 printf("warning: KLD '%s' is newer than the linker.hints"
1499 " file\n", result);
1500bad:
1501 if (hints)
1502 free(hints, M_TEMP);
1503 if (nd.ni_vp != NULL)
1504 vn_close(nd.ni_vp, FREAD, cred, td);
1505 /*
1506 * If nothing found or hints is absent - fallback to the old
1507 * way by using "kldname[.ko]" as module name.
1508 */
1509 if (!found && !bestver && result == NULL)
1510 result = linker_lookup_file(path, pathlen, modname,
1511 modnamelen, NULL);
1512 return (result);
1513}
1514
1515/*
1516 * Lookup KLD which contains requested module in the all directories.
1517 */
1518static char *
1519linker_search_module(const char *modname, int modnamelen,
1520 struct mod_depend *verinfo)
1521{
1522 char *cp, *ep, *result;
1523
1524 /*
1525 * traverse the linker path
1526 */
1527 for (cp = linker_path; *cp; cp = ep + 1) {
1528 /* find the end of this component */
1529 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1530 result = linker_hints_lookup(cp, ep - cp, modname,
1531 modnamelen, verinfo);
1532 if (result != NULL)
1533 return (result);
1534 if (*ep == 0)
1535 break;
1536 }
1537 return (NULL);
1538}
1539
1540/*
1541 * Search for module in all directories listed in the linker_path.
1542 */
1543static char *
1544linker_search_kld(const char *name)
1545{
1546 char *cp, *ep, *result, **cpp;
1547 int extlen, len;
1548
1549 /* qualified at all? */
1550 if (index(name, '/'))
1551 return (linker_strdup(name));
1552
1553 extlen = 0;
1554 for (cpp = linker_ext_list; *cpp; cpp++) {
1555 len = strlen(*cpp);
1556 if (len > extlen)
1557 extlen = len;
1558 }
1559 extlen++; /* trailing '\0' */
1560
1561 /* traverse the linker path */
1562 len = strlen(name);
1563 for (ep = linker_path; *ep; ep++) {
1564 cp = ep;
1565 /* find the end of this component */
1566 for (; *ep != 0 && *ep != ';'; ep++);
1567 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1568 if (result != NULL)
1569 return (result);
1570 }
1571 return (NULL);
1572}
1573
1574static const char *
1575linker_basename(const char *path)
1576{
1577 const char *filename;
1578
1579 filename = rindex(path, '/');
1580 if (filename == NULL)
1581 return path;
1582 if (filename[1])
1583 filename++;
1584 return (filename);
1585}
1586
1587/*
1588 * Find a file which contains given module and load it, if "parent" is not
1589 * NULL, register a reference to it.
1590 */
1591static int
1592linker_load_module(const char *kldname, const char *modname,
1593 struct linker_file *parent, struct mod_depend *verinfo,
1594 struct linker_file **lfpp)
1595{
1596 linker_file_t lfdep;
1597 const char *filename;
1598 char *pathname;
1599 int error;
1600
1601 if (modname == NULL) {
1602 /*
1603 * We have to load KLD
1604 */
1605 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1606 " is not NULL"));
1607 pathname = linker_search_kld(kldname);
1608 } else {
1609 if (modlist_lookup2(modname, verinfo) != NULL)
1610 return (EEXIST);
1611 if (kldname == NULL)
1612 /*
1613 * Need to find a KLD with required module
1614 */
1615 pathname = linker_search_module(modname,
1616 strlen(modname), verinfo);
1617 else
1618 pathname = linker_strdup(kldname);
1619 }
1620 if (pathname == NULL)
1621 return (ENOENT);
1622
1623 /*
1624 * Can't load more than one file with the same basename XXX:
1625 * Actually it should be possible to have multiple KLDs with
1626 * the same basename but different path because they can
1627 * provide different versions of the same modules.
1628 */
1629 filename = linker_basename(pathname);
1630 if (linker_find_file_by_name(filename)) {
1631 error = EEXIST;
1632 goto out;
1633 }
1634 do {
1635 error = linker_load_file(pathname, &lfdep);
1636 if (error)
1637 break;
1638 if (modname && verinfo &&
1639 modlist_lookup2(modname, verinfo) == NULL) {
1640 linker_file_unload(lfdep);
1641 error = ENOENT;
1642 break;
1643 }
1644 if (parent) {
1645 error = linker_file_add_dependency(parent, lfdep);
1646 if (error)
1647 break;
1648 }
1649 if (lfpp)
1650 *lfpp = lfdep;
1651 } while (0);
1652out:
1653 if (pathname)
1654 free(pathname, M_LINKER);
1655 return (error);
1656}
1657
1658/*
1659 * This routine is responsible for finding dependencies of userland initiated
1660 * kldload(2)'s of files.
1661 */
1662int
1663linker_load_dependencies(linker_file_t lf)
1664{
1665 linker_file_t lfdep;
1666 struct mod_metadata **start, **stop, **mdp, **nmdp;
1667 struct mod_metadata *mp, *nmp;
1668 struct mod_depend *verinfo;
1669 modlist_t mod;
1670 const char *modname, *nmodname;
1678 int ver, error = 0, err, count;
1671 int ver, error = 0, count;
1679
1680 /*
1681 * All files are dependant on /kernel.
1682 */
1683 if (linker_kernel_file) {
1684 linker_kernel_file->refs++;
1685 error = linker_file_add_dependency(lf, linker_kernel_file);
1686 if (error)
1687 return (error);
1688 }
1689 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
1690 &count) != 0)
1691 return (0);
1692 for (mdp = start; mdp < stop; mdp++) {
1693 mp = linker_reloc_ptr(lf, *mdp);
1694 if (mp->md_type != MDT_VERSION)
1695 continue;
1696 linker_mdt_version(lf, mp, &modname, &ver);
1697 mod = modlist_lookup(modname, ver);
1698 if (mod != NULL) {
1699 printf("interface %s.%d already present in the KLD"
1700 " '%s'!\n", modname, ver,
1701 mod->container->filename);
1702 return (EEXIST);
1703 }
1704 }
1705
1706 for (mdp = start; mdp < stop; mdp++) {
1707 mp = linker_reloc_ptr(lf, *mdp);
1708 if (mp->md_type != MDT_DEPEND)
1709 continue;
1710 linker_mdt_depend(lf, mp, &modname, &verinfo);
1711 nmodname = NULL;
1712 for (nmdp = start; nmdp < stop; nmdp++) {
1713 nmp = linker_reloc_ptr(lf, *nmdp);
1714 if (nmp->md_type != MDT_VERSION)
1715 continue;
1716 nmodname = linker_reloc_ptr(lf, nmp->md_cval);
1672
1673 /*
1674 * All files are dependant on /kernel.
1675 */
1676 if (linker_kernel_file) {
1677 linker_kernel_file->refs++;
1678 error = linker_file_add_dependency(lf, linker_kernel_file);
1679 if (error)
1680 return (error);
1681 }
1682 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
1683 &count) != 0)
1684 return (0);
1685 for (mdp = start; mdp < stop; mdp++) {
1686 mp = linker_reloc_ptr(lf, *mdp);
1687 if (mp->md_type != MDT_VERSION)
1688 continue;
1689 linker_mdt_version(lf, mp, &modname, &ver);
1690 mod = modlist_lookup(modname, ver);
1691 if (mod != NULL) {
1692 printf("interface %s.%d already present in the KLD"
1693 " '%s'!\n", modname, ver,
1694 mod->container->filename);
1695 return (EEXIST);
1696 }
1697 }
1698
1699 for (mdp = start; mdp < stop; mdp++) {
1700 mp = linker_reloc_ptr(lf, *mdp);
1701 if (mp->md_type != MDT_DEPEND)
1702 continue;
1703 linker_mdt_depend(lf, mp, &modname, &verinfo);
1704 nmodname = NULL;
1705 for (nmdp = start; nmdp < stop; nmdp++) {
1706 nmp = linker_reloc_ptr(lf, *nmdp);
1707 if (nmp->md_type != MDT_VERSION)
1708 continue;
1709 nmodname = linker_reloc_ptr(lf, nmp->md_cval);
1717 err = strcmp(modname, nmodname);
1718 if (err == 0)
1710 if (strcmp(modname, nmodname) == 0)
1719 break;
1720 }
1721 if (nmdp < stop)/* early exit, it's a self reference */
1722 continue;
1723 mod = modlist_lookup2(modname, verinfo);
1724 if (mod) { /* woohoo, it's loaded already */
1725 lfdep = mod->container;
1726 lfdep->refs++;
1727 error = linker_file_add_dependency(lf, lfdep);
1728 if (error)
1729 break;
1730 continue;
1731 }
1732 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
1733 if (error) {
1734 printf("KLD %s: depends on %s - not available\n",
1735 lf->filename, modname);
1736 break;
1737 }
1738 }
1739
1740 if (error)
1741 return (error);
1742 linker_addmodules(lf, start, stop, 0);
1743 return (error);
1744}
1745
1746static int
1747sysctl_kern_function_list_iterate(const char *name, void *opaque)
1748{
1749 struct sysctl_req *req;
1750
1751 req = opaque;
1752 return (SYSCTL_OUT(req, name, strlen(name) + 1));
1753}
1754
1755/*
1756 * Export a nul-separated, double-nul-terminated list of all function names
1757 * in the kernel.
1758 */
1759static int
1760sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
1761{
1762 linker_file_t lf;
1763 int error;
1764
1765 TAILQ_FOREACH(lf, &linker_files, link) {
1766 error = LINKER_EACH_FUNCTION_NAME(lf,
1767 sysctl_kern_function_list_iterate, req);
1768 if (error)
1769 return (error);
1770 }
1771 return (SYSCTL_OUT(req, "", 1));
1772}
1773
1774SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
1775 NULL, 0, sysctl_kern_function_list, "", "kernel function list");
1711 break;
1712 }
1713 if (nmdp < stop)/* early exit, it's a self reference */
1714 continue;
1715 mod = modlist_lookup2(modname, verinfo);
1716 if (mod) { /* woohoo, it's loaded already */
1717 lfdep = mod->container;
1718 lfdep->refs++;
1719 error = linker_file_add_dependency(lf, lfdep);
1720 if (error)
1721 break;
1722 continue;
1723 }
1724 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
1725 if (error) {
1726 printf("KLD %s: depends on %s - not available\n",
1727 lf->filename, modname);
1728 break;
1729 }
1730 }
1731
1732 if (error)
1733 return (error);
1734 linker_addmodules(lf, start, stop, 0);
1735 return (error);
1736}
1737
1738static int
1739sysctl_kern_function_list_iterate(const char *name, void *opaque)
1740{
1741 struct sysctl_req *req;
1742
1743 req = opaque;
1744 return (SYSCTL_OUT(req, name, strlen(name) + 1));
1745}
1746
1747/*
1748 * Export a nul-separated, double-nul-terminated list of all function names
1749 * in the kernel.
1750 */
1751static int
1752sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
1753{
1754 linker_file_t lf;
1755 int error;
1756
1757 TAILQ_FOREACH(lf, &linker_files, link) {
1758 error = LINKER_EACH_FUNCTION_NAME(lf,
1759 sysctl_kern_function_list_iterate, req);
1760 if (error)
1761 return (error);
1762 }
1763 return (SYSCTL_OUT(req, "", 1));
1764}
1765
1766SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
1767 NULL, 0, sysctl_kern_function_list, "", "kernel function list");