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