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