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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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
)
50 if (preload_metadata
!= NULL
) {
52 curp
= preload_metadata
;
54 hdr
= (u_int32_t
*)curp
;
55 if (hdr
[0] == 0 && hdr
[1] == 0)
58 /* Search for a MODINFO_NAME field */
59 if ((hdr
[0] == MODINFO_NAME
) &&
60 !strcmp(name
, curp
+ sizeof(u_int32_t
) * 2))
63 /* skip to next field */
64 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
65 next
= roundup(next
, sizeof(u_long
));
73 * Search for the first preloaded module of (type)
76 preload_search_by_type(const char *type
)
82 if (preload_metadata
!= NULL
) {
84 curp
= preload_metadata
;
87 hdr
= (u_int32_t
*)curp
;
88 if (hdr
[0] == 0 && hdr
[1] == 0)
91 /* remember the start of each record */
92 if (hdr
[0] == MODINFO_NAME
)
95 /* Search for a MODINFO_TYPE field */
96 if ((hdr
[0] == MODINFO_TYPE
) &&
97 !strcmp(type
, curp
+ sizeof(u_int32_t
) * 2))
100 /* skip to next field */
101 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
102 next
= roundup(next
, sizeof(u_long
));
110 * Walk through the preloaded module list
113 preload_search_next_name(caddr_t base
)
119 if (preload_metadata
!= NULL
) {
121 /* Pick up where we left off last time */
123 /* skip to next field */
125 hdr
= (u_int32_t
*)curp
;
126 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
127 next
= roundup(next
, sizeof(u_long
));
130 curp
= preload_metadata
;
133 hdr
= (u_int32_t
*)curp
;
134 if (hdr
[0] == 0 && hdr
[1] == 0)
137 /* Found a new record? */
138 if (hdr
[0] == MODINFO_NAME
)
141 /* skip to next field */
142 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
143 next
= roundup(next
, sizeof(u_long
));
151 * Given a preloaded module handle (mod), return a pointer
152 * to the data for the attribute (inf).
155 preload_search_info(caddr_t mod
, int inf
)
164 hdr
= (u_int32_t
*)curp
;
165 /* end of module data? */
166 if (hdr
[0] == 0 && hdr
[1] == 0)
169 * We give up once we've looped back to what we were looking at
170 * first - this should normally be a MODINFO_NAME field.
180 * Attribute match? Return pointer to data.
181 * Consumer may safely assume that size value precedes
185 return(curp
+ (sizeof(u_int32_t
) * 2));
187 /* skip to next field */
188 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
189 next
= roundup(next
, sizeof(u_long
));
196 * Delete a preload record by name.
199 preload_delete_name(const char *name
)
206 if (preload_metadata
!= NULL
) {
209 curp
= preload_metadata
;
211 hdr
= (u_int32_t
*)curp
;
212 if (hdr
[0] == 0 && hdr
[1] == 0)
215 /* Search for a MODINFO_NAME field */
216 if (hdr
[0] == MODINFO_NAME
) {
217 if (!strcmp(name
, curp
+ sizeof(u_int32_t
) * 2))
218 clearing
= 1; /* got it, start clearing */
220 clearing
= 0; /* at next one now.. better stop */
223 hdr
[0] = MODINFO_EMPTY
;
225 /* skip to next field */
226 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
227 next
= roundup(next
, sizeof(u_long
));
233 /* Called from locore on i386. Convert physical pointers to kvm. Sigh. */
235 preload_bootstrap_relocate(vm_offset_t offset
)
242 if (preload_metadata
!= NULL
) {
244 curp
= preload_metadata
;
246 hdr
= (u_int32_t
*)curp
;
247 if (hdr
[0] == 0 && hdr
[1] == 0)
250 /* Deal with the ones that we know we have to fix */
253 case MODINFO_METADATA
|MODINFOMD_SSYM
:
254 case MODINFO_METADATA
|MODINFOMD_ESYM
:
255 ptr
= (vm_offset_t
*)(curp
+ (sizeof(u_int32_t
) * 2));
259 /* The rest is beyond us for now */
261 /* skip to next field */
262 next
= sizeof(u_int32_t
) * 2 + hdr
[1];
263 next
= roundup(next
, sizeof(u_long
));