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.h
30 *
31 * Definitions for accessing mach-o headers.
32 *
33 * NOTE: These functions work on Mach-O headers compatible with
34 * the currently running kernel, and cannot be used against mach
35 * headers other than that of the currently running kernel.
36 *
37 */
38
39#ifndef _KERNEL_MACH_HEADER_
40#define _KERNEL_MACH_HEADER_
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46#include <mach/mach_types.h>
47#include <mach-o/loader.h>
48#include <mach-o/nlist.h>
49#include <mach-o/reloc.h>
50
51#if !KERNEL
52#error this header for kernel use only
53#endif
54
55#if defined(__LP64__)
56
57typedef struct mach_header_64 kernel_mach_header_t;
58typedef struct segment_command_64 kernel_segment_command_t;
59typedef struct section_64 kernel_section_t;
60typedef struct nlist_64 kernel_nlist_t;
61
62#define MH_MAGIC_KERNEL MH_MAGIC_64
63#define LC_SEGMENT_KERNEL LC_SEGMENT_64
64
65#else
66
67typedef struct mach_header kernel_mach_header_t;
68typedef struct segment_command kernel_segment_command_t;
69typedef struct section kernel_section_t;
70typedef struct nlist kernel_nlist_t;
71
72#define MH_MAGIC_KERNEL MH_MAGIC
73#define LC_SEGMENT_KERNEL LC_SEGMENT
74#define SECT_CONSTRUCTOR "__constructor"
75#define SECT_DESTRUCTOR "__destructor"
76
77#endif
78
79#define SECT_MODINITFUNC "__mod_init_func"
80#define SECT_MODTERMFUNC "__mod_term_func"
81
82extern kernel_mach_header_t _mh_execute_header;
83
84/*
85 * If the 'MH_DYLIB_IN_CACHE' bit is set in a kernel or kext mach-o header flag,
86 * then that mach-o has been linked by the new KernelCollectionBuilder into
87 * an MH_FILESET kernel collection. This bit is typically reserved for dylibs
88 * that are part of the dyld-shared-cache, but when applied to constituents of
89 * a kernel collection, it has this special meaning.
90 */
91#define kernel_mach_header_is_in_fileset(_mh) ((_mh)->flags & MH_DYLIB_IN_CACHE)
92
93vm_offset_t getlastaddr(kernel_mach_header_t *header);
94vm_offset_t getlastkerneladdr(void);
95
96kernel_segment_command_t *firstseg(void);
97kernel_segment_command_t *firstsegfromheader(kernel_mach_header_t *header);
98kernel_segment_command_t *nextsegfromheader(
99 kernel_mach_header_t *header,
100 kernel_segment_command_t *seg);
101kernel_segment_command_t *getsegbyname(const char *seg_name);
102kernel_segment_command_t *getsegbynamefromheader(
103 kernel_mach_header_t *header,
104 const char *seg_name);
105void *getsegdatafromheader(kernel_mach_header_t *, const char *, unsigned long *);
106kernel_section_t *getsectbyname(const char *seg_name, const char *sect_name);
107kernel_section_t *getsectbynamefromheader(
108 kernel_mach_header_t *header,
109 const char *seg_name,
110 const char *sect_name);
111kernel_section_t *getsectbynamefromseg(
112 kernel_segment_command_t *sgp,
113 const char *segname,
114 const char *sectname);
115uint32_t getsectoffsetfromheader(
116 kernel_mach_header_t *mhp,
117 const char *segname,
118 const char *sectname);
119void *getsectdatafromheader(kernel_mach_header_t *, const char *, const char *, unsigned long *);
120kernel_section_t *firstsect(kernel_segment_command_t *sgp);
121kernel_section_t *nextsect(kernel_segment_command_t *sgp, kernel_section_t *sp);
122void *getcommandfromheader(kernel_mach_header_t *, uint32_t);
123void *getuuidfromheader(kernel_mach_header_t *, unsigned long *);
124
125bool kernel_text_contains(vm_offset_t);
126
127#ifdef __cplusplus
128}
129#endif
130
131#endif /* _KERNEL_MACH_HEADER_ */
132