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