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