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