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 $
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/linker.h>
34 * Preloaded module support
37 caddr_t preload_metadata;
40 * Search for the preloaded module (name)
43 preload_search_by_name(const char *name)
49 if (preload_metadata != NULL) {
51 curp = preload_metadata;
53 hdr = (u_int32_t *)curp;
54 if (hdr[0] == 0 && hdr[1] == 0)
57 /* Search for a MODINFO_NAME field */
58 if ((hdr[0] == MODINFO_NAME) &&
59 !strcmp(name, curp + sizeof(u_int32_t) * 2))
62 /* skip to next field */
63 next = sizeof(u_int32_t) * 2 + hdr[1];
64 next = roundup(next, sizeof(u_long));
72 * Search for the first preloaded module of (type)
75 preload_search_by_type(const char *type)
81 if (preload_metadata != NULL) {
83 curp = preload_metadata;
86 hdr = (u_int32_t *)curp;
87 if (hdr[0] == 0 && hdr[1] == 0)
90 /* remember the start of each record */
91 if (hdr[0] == MODINFO_NAME)
94 /* Search for a MODINFO_TYPE field */
95 if ((hdr[0] == MODINFO_TYPE) &&
96 !strcmp(type, curp + sizeof(u_int32_t) * 2))
99 /* skip to next field */
100 next = sizeof(u_int32_t) * 2 + hdr[1];
101 next = roundup(next, sizeof(u_long));
109 * Walk through the preloaded module list
112 preload_search_next_name(caddr_t base)
118 if (preload_metadata != NULL) {
120 /* Pick up where we left off last time */
122 /* skip to next field */
124 hdr = (u_int32_t *)curp;
125 next = sizeof(u_int32_t) * 2 + hdr[1];
126 next = roundup(next, sizeof(u_long));
129 curp = preload_metadata;
132 hdr = (u_int32_t *)curp;
133 if (hdr[0] == 0 && hdr[1] == 0)
136 /* Found a new record? */
137 if (hdr[0] == MODINFO_NAME)
140 /* skip to next field */
141 next = sizeof(u_int32_t) * 2 + hdr[1];
142 next = roundup(next, sizeof(u_long));
150 * Given a preloaded module handle (mod), return a pointer
151 * to the data for the attribute (inf).
154 preload_search_info(caddr_t mod, int inf)
163 hdr = (u_int32_t *)curp;
164 /* end of module data? */
165 if (hdr[0] == 0 && hdr[1] == 0)
168 * We give up once we've looped back to what we were looking at
169 * first - this should normally be a MODINFO_NAME field.
179 * Attribute match? Return pointer to data.
180 * Consumer may safely assume that size value preceeds
184 return(curp + (sizeof(u_int32_t) * 2));
186 /* skip to next field */
187 next = sizeof(u_int32_t) * 2 + hdr[1];
188 next = roundup(next, sizeof(u_long));
195 * Delete a preload record by name.
198 preload_delete_name(const char *name)
205 if (preload_metadata != NULL) {
208 curp = preload_metadata;
210 hdr = (u_int32_t *)curp;
211 if (hdr[0] == 0 && hdr[1] == 0)
214 /* Search for a MODINFO_NAME field */
215 if (hdr[0] == MODINFO_NAME) {
216 if (!strcmp(name, curp + sizeof(u_int32_t) * 2))
217 clearing = 1; /* got it, start clearing */
219 clearing = 0; /* at next one now.. better stop */
222 hdr[0] = MODINFO_EMPTY;
224 /* skip to next field */
225 next = sizeof(u_int32_t) * 2 + hdr[1];
226 next = roundup(next, sizeof(u_long));
232 /* Called from locore on i386. Convert physical pointers to kvm. Sigh. */
234 preload_bootstrap_relocate(vm_offset_t offset)
241 if (preload_metadata != NULL) {
243 curp = preload_metadata;
245 hdr = (u_int32_t *)curp;
246 if (hdr[0] == 0 && hdr[1] == 0)
249 /* Deal with the ones that we know we have to fix */
252 case MODINFO_METADATA|MODINFOMD_SSYM:
253 case MODINFO_METADATA|MODINFOMD_ESYM:
254 ptr = (vm_offset_t *)(curp + (sizeof(u_int32_t) * 2));
258 /* The rest is beyond us for now */
260 /* skip to next field */
261 next = sizeof(u_int32_t) * 2 + hdr[1];
262 next = roundup(next, sizeof(u_long));