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