]>
git.saurik.com Git - apple/xnu.git/blob - libkern/kernel_mach_header.c
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * File: libkern/kernel_mach_header.c
31 * Functions for accessing mach-o headers.
33 * NOTE: This file supports only kernel mach headers at the present
34 * time; it's primary use is by kld, and all externally
35 * referenced routines at the present time operate against
36 * the kernel mach header _mh_execute_header, which is the
37 * header for the currently executing kernel.
41 #include <vm/vm_map.h>
42 #include <vm/vm_kern.h>
43 #include <libkern/kernel_mach_header.h>
44 #include <string.h> // from libsa
47 * return the last address (first avail)
49 * This routine operates against the currently executing kernel only
54 kernel_segment_command_t
*sgp
;
55 vm_offset_t last_addr
= 0;
56 kernel_mach_header_t
*header
= &_mh_execute_header
;
59 sgp
= (kernel_segment_command_t
*)
60 ((uintptr_t)header
+ sizeof(kernel_mach_header_t
));
61 for (i
= 0; i
< header
->ncmds
; i
++){
62 if ( sgp
->cmd
== LC_SEGMENT_KERNEL
) {
63 if (sgp
->vmaddr
+ sgp
->vmsize
> last_addr
)
64 last_addr
= sgp
->vmaddr
+ sgp
->vmsize
;
66 sgp
= (kernel_segment_command_t
*)((uintptr_t)sgp
+ sgp
->cmdsize
);
72 * This routine returns the a pointer to the data for the named section in the
73 * named segment if it exist in the mach header passed to it. Also it returns
74 * the size of the section data indirectly through the pointer size. Otherwise
75 * it returns zero for the pointer and the size.
77 * This routine can operate against any kernel mach header.
80 getsectdatafromheader(
81 kernel_mach_header_t
*mhp
,
86 const kernel_section_t
*sp
;
89 sp
= getsectbynamefromheader(mhp
, segname
, sectname
);
90 if(sp
== (kernel_section_t
*)0){
95 result
= (void *)sp
->addr
;
100 * This routine returns the a pointer to the data for the named segment
101 * if it exist in the mach header passed to it. Also it returns
102 * the size of the segment data indirectly through the pointer size.
103 * Otherwise it returns zero for the pointer and the size.
106 getsegdatafromheader(
107 kernel_mach_header_t
*mhp
,
111 const kernel_segment_command_t
*sc
;
114 sc
= getsegbynamefromheader(mhp
, segname
);
115 if(sc
== (kernel_segment_command_t
*)0){
120 result
= (void *)sc
->vmaddr
;
125 * This routine returns the section structure for the named section in the
126 * named segment for the mach_header pointer passed to it if it exist.
127 * Otherwise it returns zero.
129 * This routine can operate against any kernel mach header.
132 getsectbynamefromheader(
133 kernel_mach_header_t
*mhp
,
135 const char *sectname
)
137 kernel_segment_command_t
*sgp
;
138 kernel_section_t
*sp
;
141 sgp
= (kernel_segment_command_t
*)
142 ((uintptr_t)mhp
+ sizeof(kernel_mach_header_t
));
143 for(i
= 0; i
< mhp
->ncmds
; i
++){
144 if(sgp
->cmd
== LC_SEGMENT_KERNEL
)
145 if(strncmp(sgp
->segname
, segname
, sizeof(sgp
->segname
)) == 0 ||
146 mhp
->filetype
== MH_OBJECT
){
147 sp
= (kernel_section_t
*)((uintptr_t)sgp
+
148 sizeof(kernel_segment_command_t
));
149 for(j
= 0; j
< sgp
->nsects
; j
++){
150 if(strncmp(sp
->sectname
, sectname
,
151 sizeof(sp
->sectname
)) == 0 &&
152 strncmp(sp
->segname
, segname
,
153 sizeof(sp
->segname
)) == 0)
155 sp
= (kernel_section_t
*)((uintptr_t)sp
+
156 sizeof(kernel_section_t
));
159 sgp
= (kernel_segment_command_t
*)((uintptr_t)sgp
+ sgp
->cmdsize
);
161 return((kernel_section_t
*)NULL
);
165 * This routine can operate against any kernel mach header.
167 kernel_segment_command_t
*
168 getsegbynamefromheader(
169 kernel_mach_header_t
*header
,
170 const char *seg_name
)
172 kernel_segment_command_t
*sgp
;
175 sgp
= (kernel_segment_command_t
*)
176 ((uintptr_t)header
+ sizeof(kernel_mach_header_t
));
177 for (i
= 0; i
< header
->ncmds
; i
++){
178 if ( sgp
->cmd
== LC_SEGMENT_KERNEL
179 && !strncmp(sgp
->segname
, seg_name
, sizeof(sgp
->segname
)))
181 sgp
= (kernel_segment_command_t
*)((uintptr_t)sgp
+ sgp
->cmdsize
);
183 return (kernel_segment_command_t
*)NULL
;
187 * Return the first segment_command in the header.
189 kernel_segment_command_t
*
192 return firstsegfromheader(&_mh_execute_header
);
195 kernel_segment_command_t
*
196 firstsegfromheader(kernel_mach_header_t
*header
)
199 kernel_segment_command_t
*sgp
= (kernel_segment_command_t
*)
200 ((uintptr_t)header
+ sizeof(*header
));
202 for (i
= 0; i
< header
->ncmds
; i
++){
203 if (sgp
->cmd
== LC_SEGMENT_KERNEL
)
205 sgp
= (kernel_segment_command_t
*)((uintptr_t)sgp
+ sgp
->cmdsize
);
207 return (kernel_segment_command_t
*)NULL
;
211 * This routine operates against any kernel mach segment_command structure
212 * pointer and the provided kernel header, to obtain the sequentially next
213 * segment_command structure in that header.
215 kernel_segment_command_t
*
217 kernel_mach_header_t
*header
,
218 kernel_segment_command_t
*seg
)
221 kernel_segment_command_t
*sgp
= (kernel_segment_command_t
*)
222 ((uintptr_t)header
+ sizeof(*header
));
224 /* Find the index of the passed-in segment */
225 for (i
= 0; sgp
!= seg
&& i
< header
->ncmds
; i
++) {
226 sgp
= (kernel_segment_command_t
*)((uintptr_t)sgp
+ sgp
->cmdsize
);
229 /* Increment to the next load command */
231 sgp
= (kernel_segment_command_t
*)((uintptr_t)sgp
+ sgp
->cmdsize
);
233 /* Return the next segment command, if any */
234 for (; i
< header
->ncmds
; i
++) {
235 if (sgp
->cmd
== LC_SEGMENT_KERNEL
) return sgp
;
237 sgp
= (kernel_segment_command_t
*)((uintptr_t)sgp
+ sgp
->cmdsize
);
240 return (kernel_segment_command_t
*)NULL
;
245 * Return the address of the named Mach-O segment from the currently
246 * executing kernel kernel, or NULL.
248 kernel_segment_command_t
*
249 getsegbyname(const char *seg_name
)
251 return(getsegbynamefromheader(&_mh_execute_header
, seg_name
));
255 * This routine returns the a pointer the section structure of the named
256 * section in the named segment if it exists in the currently executing
257 * kernel, which it is presumed to be linked into. Otherwise it returns NULL.
262 const char *sectname
)
264 return(getsectbynamefromheader(
265 (kernel_mach_header_t
*)&_mh_execute_header
, segname
, sectname
));
269 * This routine can operate against any kernel segment_command structure to
270 * return the first kernel section immediately following that structure. If
271 * there are no sections associated with the segment_command structure, it
275 firstsect(kernel_segment_command_t
*sgp
)
277 if (!sgp
|| sgp
->nsects
== 0)
278 return (kernel_section_t
*)NULL
;
280 return (kernel_section_t
*)(sgp
+1);
284 * This routine can operate against any kernel segment_command structure and
285 * kernel section to return the next consecutive kernel section immediately
286 * following the kernel section provided. If there are no sections following
287 * the provided section, it returns NULL.
290 nextsect(kernel_segment_command_t
*sgp
, kernel_section_t
*sp
)
292 kernel_section_t
*fsp
= firstsect(sgp
);
294 if (((uintptr_t)(sp
- fsp
) + 1) >= sgp
->nsects
)
295 return (kernel_section_t
*)NULL
;
302 * This routine returns the section command for the symbol table in the
303 * named segment for the mach_header pointer passed to it if it exist.
304 * Otherwise it returns zero.
306 static struct symtab_command
*
307 getsectcmdsymtabfromheader(
308 kernel_mach_header_t
*mhp
)
310 kernel_segment_command_t
*sgp
;
313 sgp
= (kernel_segment_command_t
*)
314 ((uintptr_t)mhp
+ sizeof(kernel_mach_header_t
));
315 for(i
= 0; i
< mhp
->ncmds
; i
++){
316 if(sgp
->cmd
== LC_SYMTAB
)
317 return((struct symtab_command
*)sgp
);
318 sgp
= (kernel_segment_command_t
*)((uintptr_t)sgp
+ sgp
->cmdsize
);
320 return((struct symtab_command
*)NULL
);
323 boolean_t
getsymtab(kernel_mach_header_t
*header
,
327 vm_size_t
*strtabsize
)
329 kernel_segment_command_t
*seglink_cmd
;
330 struct symtab_command
*symtab_cmd
;
334 if((header
->magic
!= MH_MAGIC
)
335 && (header
->magic
!= MH_MAGIC_64
)) { /* Check if this is a valid header format */
336 return (FALSE
); /* Bye y'all... */
339 seglink_cmd
= getsegbynamefromheader(header
,"__LINKEDIT");
340 if (seglink_cmd
== NULL
) {
345 symtab_cmd
= getsectcmdsymtabfromheader(header
);
346 if (symtab_cmd
== NULL
)
349 *nsyms
= symtab_cmd
->nsyms
;
350 if(symtab_cmd
->nsyms
== 0) return (FALSE
); /* No symbols */
352 *strtabsize
= symtab_cmd
->strsize
;
353 if(symtab_cmd
->strsize
== 0) return (FALSE
); /* Symbol length is 0 */
355 *symtab
= seglink_cmd
->vmaddr
+ symtab_cmd
->symoff
-
356 seglink_cmd
->fileoff
;
358 *strtab
= seglink_cmd
->vmaddr
+ symtab_cmd
->stroff
-
359 seglink_cmd
->fileoff
;