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