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