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