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