2 * Copyright (c) 1998 Michael Smith
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/kern/subr_module.c,v 1.6 1999/10/11 15:19:10 peter Exp $
27 * $DragonFly: src/sys/kern/subr_module.c,v 1.4 2004/05/26 08:32:41 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/linker.h>
35 * Preloaded module support
38 caddr_t preload_metadata
;
41 * Search for the preloaded module (name)
44 preload_search_by_name(const char *name
)
52 if (preload_metadata
== NULL
)
55 curp
= preload_metadata
;
57 hdr
= (u_int32_t
*)curp
;
58 if (hdr
[0] == 0 && hdr
[1] == 0)
62 * Search for a MODINFO_NAME field. the boot loader really
63 * ought to strip the path names
65 if (hdr
[0] == MODINFO_NAME
) {
66 scanname
= curp
+ sizeof(u_int32_t
) * 2;
68 while (i
> 0 && scanname
[i
-1] != '/')
70 if (strcmp(name
, scanname
) == 0)
72 if (strcmp(name
, scanname
+ i
) == 0)
75 /* skip to next field */
76 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
77 next
= roundup(next
, sizeof(u_long
));
84 * Search for the first preloaded module of (type)
87 preload_search_by_type(const char *type
)
93 if (preload_metadata
!= NULL
) {
95 curp
= preload_metadata
;
98 hdr
= (u_int32_t
*)curp
;
99 if (hdr
[0] == 0 && hdr
[1] == 0)
102 /* remember the start of each record */
103 if (hdr
[0] == MODINFO_NAME
)
106 /* Search for a MODINFO_TYPE field */
107 if ((hdr
[0] == MODINFO_TYPE
) &&
108 !strcmp(type
, curp
+ sizeof(u_int32_t
) * 2))
111 /* skip to next field */
112 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
113 next
= roundup(next
, sizeof(u_long
));
121 * Walk through the preloaded module list
124 preload_search_next_name(caddr_t base
)
130 if (preload_metadata
!= NULL
) {
132 /* Pick up where we left off last time */
134 /* skip to next field */
136 hdr
= (u_int32_t
*)curp
;
137 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
138 next
= roundup(next
, sizeof(u_long
));
141 curp
= preload_metadata
;
144 hdr
= (u_int32_t
*)curp
;
145 if (hdr
[0] == 0 && hdr
[1] == 0)
148 /* Found a new record? */
149 if (hdr
[0] == MODINFO_NAME
)
152 /* skip to next field */
153 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
154 next
= roundup(next
, sizeof(u_long
));
162 * Given a preloaded module handle (mod), return a pointer
163 * to the data for the attribute (inf).
166 preload_search_info(caddr_t mod
, int inf
)
175 hdr
= (u_int32_t
*)curp
;
176 /* end of module data? */
177 if (hdr
[0] == 0 && hdr
[1] == 0)
180 * We give up once we've looped back to what we were looking at
181 * first - this should normally be a MODINFO_NAME field.
191 * Attribute match? Return pointer to data.
192 * Consumer may safely assume that size value preceeds
196 return(curp
+ (sizeof(u_int32_t
) * 2));
198 /* skip to next field */
199 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
200 next
= roundup(next
, sizeof(u_long
));
207 * Delete a preload record by name.
209 * XXX we should really pass the base of the preloaded module here and not
210 * require rematching of the name. If the wrong module (or no module) is
211 * deleted, the original preloaded module might be loaded again, causing it's
212 * data to be relocated twice.
215 preload_delete_name(const char *name
)
224 if (preload_metadata
!= NULL
) {
226 curp
= preload_metadata
;
228 hdr
= (u_int32_t
*)curp
;
229 if (hdr
[0] == 0 && hdr
[1] == 0)
232 /* Search for a MODINFO_NAME field */
233 if (hdr
[0] == MODINFO_NAME
) {
234 scanname
= curp
+ sizeof(u_int32_t
) * 2;
235 i
= strlen(scanname
);
236 while (i
> 0 && scanname
[i
-1] != '/')
238 if (strcmp(name
, scanname
) == 0)
240 else if (strcmp(name
, scanname
+ i
) == 0)
243 clearing
= 0; /* at next module now, stop clearing */
246 hdr
[0] = MODINFO_EMPTY
;
248 /* skip to next field */
249 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
250 next
= roundup(next
, sizeof(u_long
));
256 /* Called from locore on i386. Convert physical pointers to kvm. Sigh. */
258 preload_bootstrap_relocate(vm_offset_t offset
)
265 if (preload_metadata
!= NULL
) {
267 curp
= preload_metadata
;
269 hdr
= (u_int32_t
*)curp
;
270 if (hdr
[0] == 0 && hdr
[1] == 0)
273 /* Deal with the ones that we know we have to fix */
276 case MODINFO_METADATA
|MODINFOMD_SSYM
:
277 case MODINFO_METADATA
|MODINFOMD_ESYM
:
278 ptr
= (vm_offset_t
*)(curp
+ (sizeof(u_int32_t
) * 2));
282 /* The rest is beyond us for now */
284 /* skip to next field */
285 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
286 next
= roundup(next
, sizeof(u_long
));