1/*
2 * Copyright (c) 2007-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#ifndef _KXLD_H
29#define _KXLD_H
30
31#include <sys/types.h>
32#include <mach/boolean.h>       // boolean_t
33#include <mach/kern_return.h>   // kern_return_t
34#include <mach/machine.h>       // cpu_type_t and cpu_subtype_t
35#include <mach/vm_types.h>
36
37#include "kxld_types.h"
38
39/*******************************************************************************
40* API
41*******************************************************************************/
42
43/*******************************************************************************
44* Creates a state object for the linker.  A context must be created for each
45* link thread and destroyed at the end of the thread's life.  A context should
46* be reused for all links occuring in that link thread.
47*   context             Returns a pointer to the new context object
48*   allocate callback   Callback to allocate memory for the linked kext
49*   log_callback        Callback for all kxld logging output
50*   flags               Flags to control the behavior of kxld
51*   cputype             The target arch's CPU type (0 for host arch)
52*   cpusubtype          The target arch's CPU subtype (0 for host subtype)
53*******************************************************************************/
54kern_return_t kxld_create_context(
55    KXLDContext **context,
56    KXLDAllocateCallback allocate_callback,
57    KXLDLoggingCallback log_callback,
58    KXLDFlags flags,
59    cpu_type_t cputype,
60    cpu_subtype_t cpusubtype)
61    __attribute__((nonnull(1,2),visibility("default")));
62
63/*******************************************************************************
64* Destroys a link context and frees all associated memory.  Should be called at
65* the end of a link thread's life.
66*******************************************************************************/
67void kxld_destroy_context(
68    KXLDContext *context)
69    __attribute__((nonnull,visibility("default")));
70
71/*******************************************************************************
72* Links a kext against its dependencies, using a callback to allocate the memory
73* at which it will be located.
74* NOTE: The object data itself must be mmapped with PROT_WRITE and MAP_PRIVATE
75*   context             The KXLDContext object for the current link thread.
76*   file                The kext object file read into memory.
77*                       Supported formats: Mach-O, Mach-O64, Fat.
78*   size                The size of the kext in memory.  Must be nonzero.
79*   name                The name, usually the bundle identifier, of the kext
80*   callback_data       Data that is to be passed to the callback functions.
81*   dependencies        An array of pointers to the kexts upon which this kext
82*                       is dependent.
83*   num_dependencies    Number of entries in the 'dependencies' array.
84*   linked_object       This will be set to the address of the linked kext
85*                       object. If the address provided by the
86*                       kxld_alloc_callback is considered writable, this
87*                       pointer will be set to that address.  Otherwise, the
88*                       linked object will be written to a temporary buffer
89*                       that should be freed by the caller.
90*   kmod_info_kern      Kernel address of the kmod_info_t structure.
91******************************************************************************/
92kern_return_t kxld_link_file(
93    KXLDContext *context,
94    u_char *file,
95    u_long size,
96    const char *name,
97    void *callback_data,
98    KXLDDependency *dependencies,
99    u_int num_dependencies,
100    u_char **linked_object,
101    kxld_addr_t *kmod_info_kern)
102    __attribute__((nonnull(1,2,4,6,8,9), visibility("default")));
103
104/*******************************************************************************
105*******************************************************************************/
106boolean_t kxld_validate_copyright_string(const char *str)
107    __attribute__((pure, nonnull, visibility("default")));
108
109#endif // _KXLD_H_
110
111