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