xref: /xnu-8020.121.3/libkern/kernel_mach_header.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions /*
2*fdd8201dSApple OSS Distributions  * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
3*fdd8201dSApple OSS Distributions  *
4*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*fdd8201dSApple OSS Distributions  *
6*fdd8201dSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*fdd8201dSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*fdd8201dSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*fdd8201dSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*fdd8201dSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*fdd8201dSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*fdd8201dSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*fdd8201dSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*fdd8201dSApple OSS Distributions  *
15*fdd8201dSApple OSS Distributions  * Please obtain a copy of the License at
16*fdd8201dSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*fdd8201dSApple OSS Distributions  *
18*fdd8201dSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*fdd8201dSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*fdd8201dSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*fdd8201dSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*fdd8201dSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*fdd8201dSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*fdd8201dSApple OSS Distributions  * limitations under the License.
25*fdd8201dSApple OSS Distributions  *
26*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*fdd8201dSApple OSS Distributions  */
28*fdd8201dSApple OSS Distributions /*
29*fdd8201dSApple OSS Distributions  *	File: libkern/kernel_mach_header.c
30*fdd8201dSApple OSS Distributions  *
31*fdd8201dSApple OSS Distributions  *	Functions for accessing mach-o headers.
32*fdd8201dSApple OSS Distributions  *
33*fdd8201dSApple OSS Distributions  * NOTE:	This file supports only kernel mach headers at the present
34*fdd8201dSApple OSS Distributions  *		time; it's primary use is by kld, and all externally
35*fdd8201dSApple OSS Distributions  *		referenced routines at the present time operate against
36*fdd8201dSApple OSS Distributions  *		the kernel mach header _mh_execute_header, which is the
37*fdd8201dSApple OSS Distributions  *		header for the currently executing kernel.
38*fdd8201dSApple OSS Distributions  *
39*fdd8201dSApple OSS Distributions  */
40*fdd8201dSApple OSS Distributions 
41*fdd8201dSApple OSS Distributions #include <vm/vm_map.h>
42*fdd8201dSApple OSS Distributions #include <vm/vm_kern.h>
43*fdd8201dSApple OSS Distributions #include <libkern/kernel_mach_header.h>
44*fdd8201dSApple OSS Distributions #include <string.h>             // from libsa
45*fdd8201dSApple OSS Distributions 
46*fdd8201dSApple OSS Distributions /*
47*fdd8201dSApple OSS Distributions  * return the last address (first avail)
48*fdd8201dSApple OSS Distributions  *
49*fdd8201dSApple OSS Distributions  * This routine operates against the currently executing kernel only
50*fdd8201dSApple OSS Distributions  */
51*fdd8201dSApple OSS Distributions vm_offset_t
getlastaddr(void)52*fdd8201dSApple OSS Distributions getlastaddr(void)
53*fdd8201dSApple OSS Distributions {
54*fdd8201dSApple OSS Distributions 	kernel_segment_command_t        *sgp;
55*fdd8201dSApple OSS Distributions 	vm_offset_t             last_addr = 0;
56*fdd8201dSApple OSS Distributions 	kernel_mach_header_t *header = &_mh_execute_header;
57*fdd8201dSApple OSS Distributions 	unsigned long i;
58*fdd8201dSApple OSS Distributions 
59*fdd8201dSApple OSS Distributions 	sgp = (kernel_segment_command_t *)
60*fdd8201dSApple OSS Distributions 	    ((uintptr_t)header + sizeof(kernel_mach_header_t));
61*fdd8201dSApple OSS Distributions 	for (i = 0; i < header->ncmds; i++) {
62*fdd8201dSApple OSS Distributions 		if (sgp->cmd == LC_SEGMENT_KERNEL) {
63*fdd8201dSApple OSS Distributions 			if (sgp->vmaddr + sgp->vmsize > last_addr) {
64*fdd8201dSApple OSS Distributions 				last_addr = sgp->vmaddr + sgp->vmsize;
65*fdd8201dSApple OSS Distributions 			}
66*fdd8201dSApple OSS Distributions 		}
67*fdd8201dSApple OSS Distributions 		sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
68*fdd8201dSApple OSS Distributions 	}
69*fdd8201dSApple OSS Distributions 	return last_addr;
70*fdd8201dSApple OSS Distributions }
71*fdd8201dSApple OSS Distributions 
72*fdd8201dSApple OSS Distributions /*
73*fdd8201dSApple OSS Distributions  * Find the specified load command in the Mach-O headers, and return
74*fdd8201dSApple OSS Distributions  * the command. If there is no such load command, NULL is returned.
75*fdd8201dSApple OSS Distributions  */
76*fdd8201dSApple OSS Distributions void *
getcommandfromheader(kernel_mach_header_t * mhp,uint32_t cmd)77*fdd8201dSApple OSS Distributions getcommandfromheader(kernel_mach_header_t *mhp, uint32_t cmd)
78*fdd8201dSApple OSS Distributions {
79*fdd8201dSApple OSS Distributions 	struct load_command *lcp;
80*fdd8201dSApple OSS Distributions 	unsigned long i;
81*fdd8201dSApple OSS Distributions 
82*fdd8201dSApple OSS Distributions 	lcp = (struct load_command *) (mhp + 1);
83*fdd8201dSApple OSS Distributions 	for (i = 0; i < mhp->ncmds; i++) {
84*fdd8201dSApple OSS Distributions 		if (lcp->cmd == cmd) {
85*fdd8201dSApple OSS Distributions 			return (void *)lcp;
86*fdd8201dSApple OSS Distributions 		}
87*fdd8201dSApple OSS Distributions 
88*fdd8201dSApple OSS Distributions 		lcp = (struct load_command *)((uintptr_t)lcp + lcp->cmdsize);
89*fdd8201dSApple OSS Distributions 	}
90*fdd8201dSApple OSS Distributions 
91*fdd8201dSApple OSS Distributions 	return NULL;
92*fdd8201dSApple OSS Distributions }
93*fdd8201dSApple OSS Distributions 
94*fdd8201dSApple OSS Distributions /*
95*fdd8201dSApple OSS Distributions  * Find the UUID load command in the Mach-O headers, and return
96*fdd8201dSApple OSS Distributions  * the address of the UUID blob and size in "*size". If the
97*fdd8201dSApple OSS Distributions  * Mach-O image is missing a UUID, NULL is returned.
98*fdd8201dSApple OSS Distributions  */
99*fdd8201dSApple OSS Distributions void *
getuuidfromheader(kernel_mach_header_t * mhp,unsigned long * size)100*fdd8201dSApple OSS Distributions getuuidfromheader(kernel_mach_header_t *mhp, unsigned long *size)
101*fdd8201dSApple OSS Distributions {
102*fdd8201dSApple OSS Distributions 	struct uuid_command *cmd = (struct uuid_command *)
103*fdd8201dSApple OSS Distributions 	    getcommandfromheader(mhp, LC_UUID);
104*fdd8201dSApple OSS Distributions 
105*fdd8201dSApple OSS Distributions 	if (cmd != NULL) {
106*fdd8201dSApple OSS Distributions 		if (size) {
107*fdd8201dSApple OSS Distributions 			*size = sizeof(cmd->uuid);
108*fdd8201dSApple OSS Distributions 		}
109*fdd8201dSApple OSS Distributions 		return cmd->uuid;
110*fdd8201dSApple OSS Distributions 	}
111*fdd8201dSApple OSS Distributions 
112*fdd8201dSApple OSS Distributions 	return NULL;
113*fdd8201dSApple OSS Distributions }
114*fdd8201dSApple OSS Distributions 
115*fdd8201dSApple OSS Distributions /*
116*fdd8201dSApple OSS Distributions  * This routine returns the a pointer to the data for the named section in the
117*fdd8201dSApple OSS Distributions  * named segment if it exist in the mach header passed to it.  Also it returns
118*fdd8201dSApple OSS Distributions  * the size of the section data indirectly through the pointer size.  Otherwise
119*fdd8201dSApple OSS Distributions  *  it returns zero for the pointer and the size.
120*fdd8201dSApple OSS Distributions  *
121*fdd8201dSApple OSS Distributions  * This routine can operate against any kernel mach header.
122*fdd8201dSApple OSS Distributions  */
123*fdd8201dSApple OSS Distributions void *
getsectdatafromheader(kernel_mach_header_t * mhp,const char * segname,const char * sectname,unsigned long * size)124*fdd8201dSApple OSS Distributions getsectdatafromheader(
125*fdd8201dSApple OSS Distributions 	kernel_mach_header_t *mhp,
126*fdd8201dSApple OSS Distributions 	const char *segname,
127*fdd8201dSApple OSS Distributions 	const char *sectname,
128*fdd8201dSApple OSS Distributions 	unsigned long *size)
129*fdd8201dSApple OSS Distributions {
130*fdd8201dSApple OSS Distributions 	const kernel_section_t *sp;
131*fdd8201dSApple OSS Distributions 	void *result;
132*fdd8201dSApple OSS Distributions 
133*fdd8201dSApple OSS Distributions 	sp = getsectbynamefromheader(mhp, segname, sectname);
134*fdd8201dSApple OSS Distributions 	if (sp == (kernel_section_t *)0) {
135*fdd8201dSApple OSS Distributions 		*size = 0;
136*fdd8201dSApple OSS Distributions 		return (char *)0;
137*fdd8201dSApple OSS Distributions 	}
138*fdd8201dSApple OSS Distributions 	*size = sp->size;
139*fdd8201dSApple OSS Distributions 	result = (void *)sp->addr;
140*fdd8201dSApple OSS Distributions 	return result;
141*fdd8201dSApple OSS Distributions }
142*fdd8201dSApple OSS Distributions 
143*fdd8201dSApple OSS Distributions /*
144*fdd8201dSApple OSS Distributions  * This routine returns the offset for the named section in the
145*fdd8201dSApple OSS Distributions  * named segment if it exist in the mach header passed to it. Otherwise
146*fdd8201dSApple OSS Distributions  *  it returns zero.
147*fdd8201dSApple OSS Distributions  *
148*fdd8201dSApple OSS Distributions  * This routine can operate against any kernel mach header.
149*fdd8201dSApple OSS Distributions  */
150*fdd8201dSApple OSS Distributions uint32_t
getsectoffsetfromheader(kernel_mach_header_t * mhp,const char * segname,const char * sectname)151*fdd8201dSApple OSS Distributions getsectoffsetfromheader(
152*fdd8201dSApple OSS Distributions 	kernel_mach_header_t *mhp,
153*fdd8201dSApple OSS Distributions 	const char *segname,
154*fdd8201dSApple OSS Distributions 	const char *sectname)
155*fdd8201dSApple OSS Distributions {
156*fdd8201dSApple OSS Distributions 	const kernel_section_t *sp;
157*fdd8201dSApple OSS Distributions 
158*fdd8201dSApple OSS Distributions 	sp = getsectbynamefromheader(mhp, segname, sectname);
159*fdd8201dSApple OSS Distributions 	if (sp == (kernel_section_t *)0) {
160*fdd8201dSApple OSS Distributions 		return 0;
161*fdd8201dSApple OSS Distributions 	}
162*fdd8201dSApple OSS Distributions 
163*fdd8201dSApple OSS Distributions 	return sp->offset;
164*fdd8201dSApple OSS Distributions }
165*fdd8201dSApple OSS Distributions 
166*fdd8201dSApple OSS Distributions /*
167*fdd8201dSApple OSS Distributions  * This routine returns the a pointer to the data for the named segment
168*fdd8201dSApple OSS Distributions  * if it exist in the mach header passed to it.  Also it returns
169*fdd8201dSApple OSS Distributions  * the size of the segment data indirectly through the pointer size.
170*fdd8201dSApple OSS Distributions  * Otherwise it returns zero for the pointer and the size.
171*fdd8201dSApple OSS Distributions  */
172*fdd8201dSApple OSS Distributions void *
getsegdatafromheader(kernel_mach_header_t * mhp,const char * segname,unsigned long * size)173*fdd8201dSApple OSS Distributions getsegdatafromheader(
174*fdd8201dSApple OSS Distributions 	kernel_mach_header_t *mhp,
175*fdd8201dSApple OSS Distributions 	const char *segname,
176*fdd8201dSApple OSS Distributions 	unsigned long *size)
177*fdd8201dSApple OSS Distributions {
178*fdd8201dSApple OSS Distributions 	const kernel_segment_command_t *sc;
179*fdd8201dSApple OSS Distributions 	void *result;
180*fdd8201dSApple OSS Distributions 
181*fdd8201dSApple OSS Distributions 	sc = getsegbynamefromheader(mhp, segname);
182*fdd8201dSApple OSS Distributions 	if (sc == (kernel_segment_command_t *)0) {
183*fdd8201dSApple OSS Distributions 		*size = 0;
184*fdd8201dSApple OSS Distributions 		return (char *)0;
185*fdd8201dSApple OSS Distributions 	}
186*fdd8201dSApple OSS Distributions 	*size = sc->vmsize;
187*fdd8201dSApple OSS Distributions 	result = (void *)sc->vmaddr;
188*fdd8201dSApple OSS Distributions 	return result;
189*fdd8201dSApple OSS Distributions }
190*fdd8201dSApple OSS Distributions 
191*fdd8201dSApple OSS Distributions /*
192*fdd8201dSApple OSS Distributions  * This routine iterates through the sections in a particular segment
193*fdd8201dSApple OSS Distributions  * and returns pointer to the requested section, if it is present.
194*fdd8201dSApple OSS Distributions  * Otherwise it returns zero.
195*fdd8201dSApple OSS Distributions  */
196*fdd8201dSApple OSS Distributions kernel_section_t *
getsectbynamefromseg(kernel_segment_command_t * sgp,const char * segname,const char * sectname)197*fdd8201dSApple OSS Distributions getsectbynamefromseg(
198*fdd8201dSApple OSS Distributions 	kernel_segment_command_t *sgp,
199*fdd8201dSApple OSS Distributions 	const char *segname,
200*fdd8201dSApple OSS Distributions 	const char *sectname)
201*fdd8201dSApple OSS Distributions {
202*fdd8201dSApple OSS Distributions 	unsigned long j;
203*fdd8201dSApple OSS Distributions 	kernel_section_t *sp = (kernel_section_t *)((uintptr_t)sgp +
204*fdd8201dSApple OSS Distributions 	    sizeof(kernel_segment_command_t));
205*fdd8201dSApple OSS Distributions 	for (j = 0; j < sgp->nsects; j++) {
206*fdd8201dSApple OSS Distributions 		if (strncmp(sp->sectname, sectname,
207*fdd8201dSApple OSS Distributions 		    sizeof(sp->sectname)) == 0 &&
208*fdd8201dSApple OSS Distributions 		    strncmp(sp->segname, segname,
209*fdd8201dSApple OSS Distributions 		    sizeof(sp->segname)) == 0) {
210*fdd8201dSApple OSS Distributions 			return sp;
211*fdd8201dSApple OSS Distributions 		}
212*fdd8201dSApple OSS Distributions 		sp = (kernel_section_t *)((uintptr_t)sp +
213*fdd8201dSApple OSS Distributions 		    sizeof(kernel_section_t));
214*fdd8201dSApple OSS Distributions 	}
215*fdd8201dSApple OSS Distributions 	return (kernel_section_t *)NULL;
216*fdd8201dSApple OSS Distributions }
217*fdd8201dSApple OSS Distributions 
218*fdd8201dSApple OSS Distributions 
219*fdd8201dSApple OSS Distributions /*
220*fdd8201dSApple OSS Distributions  * This routine returns the section structure for the named section in the
221*fdd8201dSApple OSS Distributions  * named segment for the mach_header pointer passed to it if it exist.
222*fdd8201dSApple OSS Distributions  * Otherwise it returns zero.
223*fdd8201dSApple OSS Distributions  *
224*fdd8201dSApple OSS Distributions  * This routine can operate against any kernel mach header.
225*fdd8201dSApple OSS Distributions  */
226*fdd8201dSApple OSS Distributions kernel_section_t *
getsectbynamefromheader(kernel_mach_header_t * mhp,const char * segname,const char * sectname)227*fdd8201dSApple OSS Distributions getsectbynamefromheader(
228*fdd8201dSApple OSS Distributions 	kernel_mach_header_t *mhp,
229*fdd8201dSApple OSS Distributions 	const char *segname,
230*fdd8201dSApple OSS Distributions 	const char *sectname)
231*fdd8201dSApple OSS Distributions {
232*fdd8201dSApple OSS Distributions 	kernel_segment_command_t *sgp;
233*fdd8201dSApple OSS Distributions 	kernel_section_t *sp;
234*fdd8201dSApple OSS Distributions 	unsigned long i;
235*fdd8201dSApple OSS Distributions 
236*fdd8201dSApple OSS Distributions 	sgp = (kernel_segment_command_t *)
237*fdd8201dSApple OSS Distributions 	    ((uintptr_t)mhp + sizeof(kernel_mach_header_t));
238*fdd8201dSApple OSS Distributions 	for (i = 0; i < mhp->ncmds; i++) {
239*fdd8201dSApple OSS Distributions 		if (sgp->cmd == LC_SEGMENT_KERNEL) {
240*fdd8201dSApple OSS Distributions 			if (strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
241*fdd8201dSApple OSS Distributions 			    mhp->filetype == MH_OBJECT) {
242*fdd8201dSApple OSS Distributions 				sp = getsectbynamefromseg(sgp, segname, sectname);
243*fdd8201dSApple OSS Distributions 				if (sp) {
244*fdd8201dSApple OSS Distributions 					return sp;
245*fdd8201dSApple OSS Distributions 				}
246*fdd8201dSApple OSS Distributions 			}
247*fdd8201dSApple OSS Distributions 		}
248*fdd8201dSApple OSS Distributions 		sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
249*fdd8201dSApple OSS Distributions 	}
250*fdd8201dSApple OSS Distributions 	return (kernel_section_t *)NULL;
251*fdd8201dSApple OSS Distributions }
252*fdd8201dSApple OSS Distributions 
253*fdd8201dSApple OSS Distributions /*
254*fdd8201dSApple OSS Distributions  * This routine can operate against any kernel mach header.
255*fdd8201dSApple OSS Distributions  */
256*fdd8201dSApple OSS Distributions kernel_segment_command_t *
getsegbynamefromheader(kernel_mach_header_t * header,const char * seg_name)257*fdd8201dSApple OSS Distributions getsegbynamefromheader(
258*fdd8201dSApple OSS Distributions 	kernel_mach_header_t    *header,
259*fdd8201dSApple OSS Distributions 	const char              *seg_name)
260*fdd8201dSApple OSS Distributions {
261*fdd8201dSApple OSS Distributions 	kernel_segment_command_t *sgp;
262*fdd8201dSApple OSS Distributions 	unsigned long i;
263*fdd8201dSApple OSS Distributions 
264*fdd8201dSApple OSS Distributions 	sgp = (kernel_segment_command_t *)
265*fdd8201dSApple OSS Distributions 	    ((uintptr_t)header + sizeof(kernel_mach_header_t));
266*fdd8201dSApple OSS Distributions 	for (i = 0; i < header->ncmds; i++) {
267*fdd8201dSApple OSS Distributions 		if (sgp->cmd == LC_SEGMENT_KERNEL
268*fdd8201dSApple OSS Distributions 		    && !strncmp(sgp->segname, seg_name, sizeof(sgp->segname))) {
269*fdd8201dSApple OSS Distributions 			return sgp;
270*fdd8201dSApple OSS Distributions 		}
271*fdd8201dSApple OSS Distributions 		sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
272*fdd8201dSApple OSS Distributions 	}
273*fdd8201dSApple OSS Distributions 	return (kernel_segment_command_t *)NULL;
274*fdd8201dSApple OSS Distributions }
275*fdd8201dSApple OSS Distributions 
276*fdd8201dSApple OSS Distributions /*
277*fdd8201dSApple OSS Distributions  * Return the first segment_command in the header.
278*fdd8201dSApple OSS Distributions  */
279*fdd8201dSApple OSS Distributions kernel_segment_command_t *
firstseg(void)280*fdd8201dSApple OSS Distributions firstseg(void)
281*fdd8201dSApple OSS Distributions {
282*fdd8201dSApple OSS Distributions 	return firstsegfromheader(&_mh_execute_header);
283*fdd8201dSApple OSS Distributions }
284*fdd8201dSApple OSS Distributions 
285*fdd8201dSApple OSS Distributions kernel_segment_command_t *
firstsegfromheader(kernel_mach_header_t * header)286*fdd8201dSApple OSS Distributions firstsegfromheader(kernel_mach_header_t *header)
287*fdd8201dSApple OSS Distributions {
288*fdd8201dSApple OSS Distributions 	u_int i = 0;
289*fdd8201dSApple OSS Distributions 	kernel_segment_command_t *sgp = (kernel_segment_command_t *)
290*fdd8201dSApple OSS Distributions 	    ((uintptr_t)header + sizeof(*header));
291*fdd8201dSApple OSS Distributions 
292*fdd8201dSApple OSS Distributions 	for (i = 0; i < header->ncmds; i++) {
293*fdd8201dSApple OSS Distributions 		if (sgp->cmd == LC_SEGMENT_KERNEL) {
294*fdd8201dSApple OSS Distributions 			return sgp;
295*fdd8201dSApple OSS Distributions 		}
296*fdd8201dSApple OSS Distributions 		sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
297*fdd8201dSApple OSS Distributions 	}
298*fdd8201dSApple OSS Distributions 	return (kernel_segment_command_t *)NULL;
299*fdd8201dSApple OSS Distributions }
300*fdd8201dSApple OSS Distributions 
301*fdd8201dSApple OSS Distributions /*
302*fdd8201dSApple OSS Distributions  * This routine operates against any kernel mach segment_command structure
303*fdd8201dSApple OSS Distributions  * pointer and the provided kernel header, to obtain the sequentially next
304*fdd8201dSApple OSS Distributions  * segment_command structure in that header.
305*fdd8201dSApple OSS Distributions  */
306*fdd8201dSApple OSS Distributions kernel_segment_command_t *
nextsegfromheader(kernel_mach_header_t * header,kernel_segment_command_t * seg)307*fdd8201dSApple OSS Distributions nextsegfromheader(
308*fdd8201dSApple OSS Distributions 	kernel_mach_header_t    *header,
309*fdd8201dSApple OSS Distributions 	kernel_segment_command_t        *seg)
310*fdd8201dSApple OSS Distributions {
311*fdd8201dSApple OSS Distributions 	u_int i = 0;
312*fdd8201dSApple OSS Distributions 	kernel_segment_command_t *sgp = (kernel_segment_command_t *)
313*fdd8201dSApple OSS Distributions 	    ((uintptr_t)header + sizeof(*header));
314*fdd8201dSApple OSS Distributions 
315*fdd8201dSApple OSS Distributions 	/* Find the index of the passed-in segment */
316*fdd8201dSApple OSS Distributions 	for (i = 0; sgp != seg && i < header->ncmds; i++) {
317*fdd8201dSApple OSS Distributions 		sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
318*fdd8201dSApple OSS Distributions 	}
319*fdd8201dSApple OSS Distributions 
320*fdd8201dSApple OSS Distributions 	/* Increment to the next load command */
321*fdd8201dSApple OSS Distributions 	i++;
322*fdd8201dSApple OSS Distributions 	sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
323*fdd8201dSApple OSS Distributions 
324*fdd8201dSApple OSS Distributions 	/* Return the next segment command, if any */
325*fdd8201dSApple OSS Distributions 	for (; i < header->ncmds; i++) {
326*fdd8201dSApple OSS Distributions 		if (sgp->cmd == LC_SEGMENT_KERNEL) {
327*fdd8201dSApple OSS Distributions 			return sgp;
328*fdd8201dSApple OSS Distributions 		}
329*fdd8201dSApple OSS Distributions 
330*fdd8201dSApple OSS Distributions 		sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
331*fdd8201dSApple OSS Distributions 	}
332*fdd8201dSApple OSS Distributions 
333*fdd8201dSApple OSS Distributions 	return (kernel_segment_command_t *)NULL;
334*fdd8201dSApple OSS Distributions }
335*fdd8201dSApple OSS Distributions 
336*fdd8201dSApple OSS Distributions 
337*fdd8201dSApple OSS Distributions /*
338*fdd8201dSApple OSS Distributions  * Return the address of the named Mach-O segment from the currently
339*fdd8201dSApple OSS Distributions  * executing kernel kernel, or NULL.
340*fdd8201dSApple OSS Distributions  */
341*fdd8201dSApple OSS Distributions kernel_segment_command_t *
getsegbyname(const char * seg_name)342*fdd8201dSApple OSS Distributions getsegbyname(const char *seg_name)
343*fdd8201dSApple OSS Distributions {
344*fdd8201dSApple OSS Distributions 	return getsegbynamefromheader(&_mh_execute_header, seg_name);
345*fdd8201dSApple OSS Distributions }
346*fdd8201dSApple OSS Distributions 
347*fdd8201dSApple OSS Distributions /*
348*fdd8201dSApple OSS Distributions  * This routine returns the a pointer the section structure of the named
349*fdd8201dSApple OSS Distributions  * section in the named segment if it exists in the currently executing
350*fdd8201dSApple OSS Distributions  * kernel, which it is presumed to be linked into.  Otherwise it returns NULL.
351*fdd8201dSApple OSS Distributions  */
352*fdd8201dSApple OSS Distributions kernel_section_t *
getsectbyname(const char * segname,const char * sectname)353*fdd8201dSApple OSS Distributions getsectbyname(
354*fdd8201dSApple OSS Distributions 	const char *segname,
355*fdd8201dSApple OSS Distributions 	const char *sectname)
356*fdd8201dSApple OSS Distributions {
357*fdd8201dSApple OSS Distributions 	return getsectbynamefromheader(
358*fdd8201dSApple OSS Distributions 		(kernel_mach_header_t *)&_mh_execute_header, segname, sectname);
359*fdd8201dSApple OSS Distributions }
360*fdd8201dSApple OSS Distributions 
361*fdd8201dSApple OSS Distributions /*
362*fdd8201dSApple OSS Distributions  * This routine can operate against any kernel segment_command structure to
363*fdd8201dSApple OSS Distributions  * return the first kernel section immediately following that structure.  If
364*fdd8201dSApple OSS Distributions  * there are no sections associated with the segment_command structure, it
365*fdd8201dSApple OSS Distributions  * returns NULL.
366*fdd8201dSApple OSS Distributions  */
367*fdd8201dSApple OSS Distributions kernel_section_t *
firstsect(kernel_segment_command_t * sgp)368*fdd8201dSApple OSS Distributions firstsect(kernel_segment_command_t *sgp)
369*fdd8201dSApple OSS Distributions {
370*fdd8201dSApple OSS Distributions 	if (!sgp || sgp->nsects == 0) {
371*fdd8201dSApple OSS Distributions 		return (kernel_section_t *)NULL;
372*fdd8201dSApple OSS Distributions 	}
373*fdd8201dSApple OSS Distributions 
374*fdd8201dSApple OSS Distributions 	return (kernel_section_t *)(sgp + 1);
375*fdd8201dSApple OSS Distributions }
376*fdd8201dSApple OSS Distributions 
377*fdd8201dSApple OSS Distributions /*
378*fdd8201dSApple OSS Distributions  * This routine can operate against any kernel segment_command structure and
379*fdd8201dSApple OSS Distributions  * kernel section to return the next consecutive  kernel section immediately
380*fdd8201dSApple OSS Distributions  * following the kernel section provided.  If there are no sections following
381*fdd8201dSApple OSS Distributions  * the provided section, it returns NULL.
382*fdd8201dSApple OSS Distributions  */
383*fdd8201dSApple OSS Distributions kernel_section_t *
nextsect(kernel_segment_command_t * sgp,kernel_section_t * sp)384*fdd8201dSApple OSS Distributions nextsect(kernel_segment_command_t *sgp, kernel_section_t *sp)
385*fdd8201dSApple OSS Distributions {
386*fdd8201dSApple OSS Distributions 	kernel_section_t *fsp = firstsect(sgp);
387*fdd8201dSApple OSS Distributions 
388*fdd8201dSApple OSS Distributions 	if (((uintptr_t)(sp - fsp) + 1) >= sgp->nsects) {
389*fdd8201dSApple OSS Distributions 		return (kernel_section_t *)NULL;
390*fdd8201dSApple OSS Distributions 	}
391*fdd8201dSApple OSS Distributions 
392*fdd8201dSApple OSS Distributions 	return sp + 1;
393*fdd8201dSApple OSS Distributions }
394