]> git.saurik.com Git - apple/xnu.git/blame - libkern/kxld/kxld.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / libkern / kxld / kxld.c
CommitLineData
b0d623f7 1/*
316670eb 2 * Copyright (c) 2007-2008, 2012 Apple Inc. All rights reserved.
b0d623f7
A
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#include <string.h>
29#include <sys/types.h>
30#include <mach/vm_param.h> /* For PAGE_SIZE */
31
32#define DEBUG_ASSERT_COMPONENT_NAME_STRING "kxld"
33#include <AssertMacros.h>
34
3e170ce0
A
35#if KERNEL
36#define __KXLD_KERNEL_UNUSED __unused
37#else
38#define __KXLD_KERNEL_UNUSED
39#endif
40
b0d623f7
A
41#if !KERNEL
42 #include "kxld.h"
43 #include "kxld_types.h"
39037602 44#else
b0d623f7
A
45 #include <libkern/kxld.h>
46 #include <libkern/kxld_types.h>
47#endif /* KERNEL */
48
49#include "kxld_array.h"
50#include "kxld_dict.h"
51#include "kxld_kext.h"
6d2010ae 52#include "kxld_object.h"
b0d623f7
A
53#include "kxld_sym.h"
54#include "kxld_symtab.h"
55#include "kxld_util.h"
56#include "kxld_vtable.h"
57
58struct kxld_vtable;
59
60struct kxld_context {
61 KXLDKext *kext;
62 KXLDArray *section_order;
6d2010ae
A
63 KXLDArray objects;
64 KXLDArray dependencies;
65 KXLDDict defined_symbols_by_name;
66 KXLDDict defined_cxx_symbols_by_value;
67 KXLDDict obsolete_symbols_by_name;
68 KXLDDict vtables_by_name;
b0d623f7
A
69 KXLDFlags flags;
70 KXLDAllocateCallback allocate_callback;
71 cpu_type_t cputype;
72 cpu_subtype_t cpusubtype;
73};
74
39037602
A
75// set to TRUE if the kext has a vmaddr_TEXT_EXEC != 0
76boolean_t isSplitKext = FALSE;
77
78// set to TRUE is we come in via kxld_link_file
79boolean_t isOldInterface = FALSE;
80uint32_t kaslr_offsets_count = 0;
81uint32_t *kaslr_offsets = NULL;
82uint32_t kaslr_offsets_index = 0;
83
b0d623f7
A
84/*******************************************************************************
85* Globals
86*******************************************************************************/
87
88/* Certain architectures alter the order of a kext's sections from its input
89 * binary, so we track that order in a dictionary of arrays, with one array for
90 * each architecture. Since the kernel only has one architecture, we can
91 * eliminate the dictionary and use a simple array.
92 * XXX: If we ever use the linker in a multithreaded environment, we will need
93 * locks around these global structures.
94 */
95#if KXLD_USER_OR_OBJECT
96#if KERNEL
97static KXLDArray *s_section_order;
98#else
99static KXLDDict *s_order_dict;
100#endif
101#endif
102
103/*******************************************************************************
104* Prototypes
105*******************************************************************************/
106
6d2010ae 107static kern_return_t init_context(KXLDContext *context, u_int ndependencies);
39037602 108static KXLDObject * get_object_for_file(KXLDContext *context,
6d2010ae 109 u_char *file, u_long size, const char *name);
39037602 110static kern_return_t allocate_split_kext(KXLDContext *context, splitKextLinkInfo * link_info);
6d2010ae 111static u_char * allocate_kext(KXLDContext *context, void *callback_data,
39037602
A
112 kxld_addr_t *vmaddr, u_long *vmsize, u_char **linked_object_alloc_out);
113static kern_return_t init_kext_objects(KXLDContext *context, u_char *file,
114 u_long size, const char *name, KXLDDependency *dependencies,
115 u_int ndependencies);
b0d623f7
A
116static void clear_context(KXLDContext *context);
117
118/*******************************************************************************
119*******************************************************************************/
120kern_return_t
121kxld_create_context(KXLDContext **_context,
122 KXLDAllocateCallback allocate_callback, KXLDLoggingCallback logging_callback,
3e170ce0
A
123 KXLDFlags flags, cpu_type_t cputype, cpu_subtype_t cpusubtype,
124 vm_size_t pagesize __KXLD_KERNEL_UNUSED)
b0d623f7
A
125{
126 kern_return_t rval = KERN_FAILURE;
6d2010ae
A
127 KXLDContext * context = NULL;
128 KXLDArray * section_order = NULL;
b0d623f7 129#if !KERNEL
6d2010ae 130 cpu_type_t * cputype_p = NULL;
b0d623f7
A
131#endif
132
133 check(_context);
39037602
A
134 if (isOldInterface) {
135 check(allocate_callback);
136 }
b0d623f7
A
137 check(logging_callback);
138 *_context = NULL;
139
140 context = kxld_alloc(sizeof(*context));
141 require_action(context, finish, rval=KERN_RESOURCE_SHORTAGE);
142 bzero(context, sizeof(*context));
143
144 context->flags = flags;
145 context->allocate_callback = allocate_callback;
146 context->cputype = cputype;
147 context->cpusubtype = cpusubtype;
148
3e170ce0
A
149#if !KERNEL
150 if (pagesize) {
151 kxld_set_cross_link_page_size(pagesize);
152 }
153#endif /* !KERNEL */
154
b0d623f7
A
155 kxld_set_logging_callback(logging_callback);
156
157 context->kext = kxld_alloc(kxld_kext_sizeof());
158 require_action(context->kext, finish, rval=KERN_RESOURCE_SHORTAGE);
159 bzero(context->kext, kxld_kext_sizeof());
160
161 /* Check if we already have an order array for this arch */
162
163#if KXLD_USER_OR_OBJECT
164#if KERNEL
165 context->section_order = s_section_order;
166#else
167 /* In userspace, create the dictionary if it doesn't already exist */
168 if (!s_order_dict) {
169 s_order_dict = kxld_alloc(sizeof(*s_order_dict));
170 require_action(s_order_dict, finish, rval=KERN_RESOURCE_SHORTAGE);
171 bzero(s_order_dict, sizeof(*s_order_dict));
172
173 rval = kxld_dict_init(s_order_dict, kxld_dict_uint32_hash,
174 kxld_dict_uint32_cmp, 0);
175 require_noerr(rval, finish);
176 }
177
178 context->section_order = kxld_dict_find(s_order_dict, &cputype);
179#endif /* KERNEL */
180
181 /* Create an order array for this arch if needed */
182
183 if (!context->section_order) {
184
185 section_order = kxld_alloc(sizeof(*section_order));
186 require_action(section_order, finish, rval=KERN_RESOURCE_SHORTAGE);
187 bzero(section_order, sizeof(*section_order));
188
189#if KERNEL
190 s_section_order = section_order;
191#else
192 /* In userspace, add the new array to the order dictionary */
193 cputype_p = kxld_alloc(sizeof(*cputype_p));
194 require_action(cputype_p, finish, rval=KERN_RESOURCE_SHORTAGE);
195 *cputype_p = cputype;
196
197 rval = kxld_dict_insert(s_order_dict, cputype_p, section_order);
198 require_noerr(rval, finish);
199
200 cputype_p = NULL;
201#endif /* KERNEL */
202
203 context->section_order = section_order;
204
205 section_order = NULL;
206 }
207#endif /* KXLD_USER_OR_OBJECT */
208
209 rval = KERN_SUCCESS;
210 *_context = context;
211 context = NULL;
212
213finish:
6d2010ae 214 if (context) kxld_destroy_context(context);
b0d623f7
A
215 if (section_order) kxld_free(section_order, sizeof(*section_order));
216#if !KERNEL
217 if (cputype_p) kxld_free(cputype_p, sizeof(*cputype_p));
218#endif
219
220 return rval;
221}
222
223/*******************************************************************************
224*******************************************************************************/
225void
226kxld_destroy_context(KXLDContext *context)
227{
6d2010ae
A
228 KXLDObject *object = NULL;
229 KXLDKext *dep = NULL;
b0d623f7
A
230 u_int i = 0;
231
232 check(context);
233
234 kxld_kext_deinit(context->kext);
235
6d2010ae
A
236 for (i = 0; i < context->objects.maxitems; ++i) {
237 object = kxld_array_get_slot(&context->objects, i);
238 kxld_object_deinit(object);
b0d623f7 239 }
6d2010ae 240 kxld_array_deinit(&context->objects);
b0d623f7 241
6d2010ae
A
242 for (i = 0; i < context->dependencies.maxitems; ++i) {
243 dep = kxld_array_get_slot(&context->dependencies, i);
244 kxld_kext_deinit(dep);
245 }
246 kxld_array_deinit(&context->dependencies);
b0d623f7 247
6d2010ae
A
248 kxld_dict_deinit(&context->defined_symbols_by_name);
249 kxld_dict_deinit(&context->defined_cxx_symbols_by_value);
250 kxld_dict_deinit(&context->obsolete_symbols_by_name);
251 kxld_dict_deinit(&context->vtables_by_name);
b0d623f7
A
252
253 kxld_free(context->kext, kxld_kext_sizeof());
254 kxld_free(context, sizeof(*context));
255
256 kxld_print_memory_report();
257}
258
259/*******************************************************************************
39037602 260 *******************************************************************************/
b0d623f7 261kern_return_t
39037602 262kxld_link_split_file(
6d2010ae 263 KXLDContext * context,
39037602 264 splitKextLinkInfo *link_info,
6d2010ae
A
265 const char * name,
266 void * callback_data,
267 KXLDDependency * dependencies,
268 u_int ndependencies,
6d2010ae 269 kxld_addr_t * kmod_info_kern)
39037602
A
270{
271 kern_return_t rval = KERN_FAILURE;
272 KXLDObject * kext_object = NULL;
273 splitKextLinkInfo * my_link_info = NULL;
274
275 isSplitKext = (link_info->vmaddr_TEXT_EXEC != 0);
276 isOldInterface = FALSE;
277
278 kxld_set_logging_callback_data(name, callback_data);
279
280 kxld_log(kKxldLogLinking, kKxldLogBasic, "Linking kext %s", name);
281
282 kaslr_offsets_count = 0;
283 kaslr_offsets_index = 0;
284 kaslr_offsets = NULL;
285
286 require_action(context, finish, rval=KERN_INVALID_ARGUMENT);
287 require_action(link_info, finish, rval=KERN_INVALID_ARGUMENT);
288 require_action(dependencies, finish, rval=KERN_INVALID_ARGUMENT);
289 require_action(ndependencies, finish, rval=KERN_INVALID_ARGUMENT);
290 require_action(kmod_info_kern, finish, rval=KERN_INVALID_ARGUMENT);
291
292 rval = init_context(context, ndependencies);
293 require_noerr(rval, finish);
294
295 rval = init_kext_objects(context,
296 link_info->kextExecutable,
297 link_info->kextSize,
298 name,
299 dependencies, ndependencies);
300 require_noerr(rval, finish);
301
302 kext_object = get_object_for_file(context,
303 link_info->kextExecutable,
304 link_info->kextSize,
305 name);
306 require_action(kext_object, finish, rval=KERN_FAILURE);
307
308 // copy vmaddrs and fileoffsets for split segments into kext_object
309 kxld_object_set_link_info(kext_object, link_info);
310
311 my_link_info = kxld_object_get_link_info(kext_object);
312
313 rval = allocate_split_kext(context, my_link_info);
314 require_noerr(rval, finish);
315
316#if SPLIT_KEXTS_DEBUG
317 kxld_log(kKxldLogLinking, kKxldLogErr, "Linking kext %s", name);
318 kxld_show_split_info(link_info);
319#endif // SPLIT_KEXTS_DEBUG
320
321 rval = kxld_kext_relocate(context->kext,
322 (kxld_addr_t)my_link_info,
323 &context->vtables_by_name,
324 &context->defined_symbols_by_name,
325 &context->obsolete_symbols_by_name,
326 &context->defined_cxx_symbols_by_value);
327 require_noerr(rval, finish);
328
329 rval = kxld_kext_export_linked_object(context->kext,
330 (void *) my_link_info,
331 kmod_info_kern);
332 require_noerr(rval, finish);
333
334 // pass back info about linked kext
335 link_info->kaslr_offsets_count = kaslr_offsets_count;
336 link_info->kaslr_offsets = kaslr_offsets;
337 link_info->linkedKext = my_link_info->linkedKext;
338 link_info->linkedKextSize = my_link_info->linkedKextSize;
339
340 if (kaslr_offsets_count != kaslr_offsets_index) {
341 kxld_log(kKxldLogLinking, kKxldLogErr, "[ERROR] %s: KASLR pointers: count=%d, but only populated %d!", name, kaslr_offsets_count, kaslr_offsets_index);
342 rval = KERN_FAILURE;
343 goto finish;
344 }
345
346 // the values are now the responsibility of the caller
347 kaslr_offsets_count = 0;
348 kaslr_offsets_index = 0;
349 kaslr_offsets = NULL;
350
351 rval = KERN_SUCCESS;
352finish:
353 clear_context(context);
354 kxld_set_logging_callback_data(NULL, NULL);
355
356 return rval;
357}
358
359/*******************************************************************************
360 *******************************************************************************/
361kern_return_t
362kxld_link_file(
363 KXLDContext * context,
364 u_char * file,
365 u_long size,
366 const char * name,
367 void * callback_data,
368 KXLDDependency * dependencies,
369 u_int ndependencies,
370 u_char ** linked_object_out,
371 kxld_addr_t * kmod_info_kern)
b0d623f7 372{
6d2010ae
A
373 kern_return_t rval = KERN_FAILURE;
374 kxld_addr_t vmaddr = 0;
375 u_long vmsize = 0;
376 u_char * linked_object = NULL;
377 u_char * linked_object_alloc = NULL;
39037602
A
378
379 kaslr_offsets_count = 0;
380 kaslr_offsets_index = 0;
381 kaslr_offsets = NULL;
b0d623f7
A
382
383 kxld_set_logging_callback_data(name, callback_data);
39037602 384
6d2010ae 385 kxld_log(kKxldLogLinking, kKxldLogBasic, "Linking kext %s", name);
39037602 386
b0d623f7 387 require_action(context, finish, rval=KERN_INVALID_ARGUMENT);
6d2010ae
A
388 require_action(dependencies, finish, rval=KERN_INVALID_ARGUMENT);
389 require_action(ndependencies, finish, rval=KERN_INVALID_ARGUMENT);
39037602
A
390 require_action(file, finish, rval=KERN_INVALID_ARGUMENT);
391 require_action(size, finish, rval=KERN_INVALID_ARGUMENT);
6d2010ae
A
392 require_action(linked_object_out, finish, rval=KERN_INVALID_ARGUMENT);
393 require_action(kmod_info_kern, finish, rval=KERN_INVALID_ARGUMENT);
39037602
A
394
395 isSplitKext = FALSE;
396 isOldInterface = TRUE;
b0d623f7 397
6d2010ae 398 rval = init_context(context, ndependencies);
b0d623f7 399 require_noerr(rval, finish);
39037602
A
400
401 rval = init_kext_objects(context, file, size, name,
402 dependencies, ndependencies);
b0d623f7 403 require_noerr(rval, finish);
39037602
A
404
405 linked_object = allocate_kext(context, callback_data,
406 &vmaddr, &vmsize, &linked_object_alloc);
6d2010ae 407 require_action(linked_object, finish, rval=KERN_RESOURCE_SHORTAGE);
39037602
A
408
409
410 rval = kxld_kext_relocate(context->kext,
411 vmaddr,
412 &context->vtables_by_name,
413 &context->defined_symbols_by_name,
414 &context->obsolete_symbols_by_name,
415 &context->defined_cxx_symbols_by_value);
6d2010ae 416 require_noerr(rval, finish);
39037602
A
417
418 rval = kxld_kext_export_linked_object(context->kext,
419 (void *) linked_object,
420 kmod_info_kern);
6d2010ae 421 require_noerr(rval, finish);
6d2010ae 422 *linked_object_out = linked_object;
39037602 423
6d2010ae 424 linked_object_alloc = NULL;
39037602 425
6d2010ae
A
426 rval = KERN_SUCCESS;
427finish:
428 if (linked_object_alloc) {
429 kxld_page_free_untracked(linked_object_alloc, vmsize);
430 }
39037602 431
6d2010ae
A
432 clear_context(context);
433 kxld_set_logging_callback_data(NULL, NULL);
39037602 434
6d2010ae
A
435 return rval;
436}
b0d623f7 437
39037602 438
6d2010ae
A
439/*******************************************************************************
440*******************************************************************************/
441static kern_return_t
442init_context(KXLDContext *context, u_int ndependencies)
443{
444 kern_return_t rval = KERN_FAILURE;
b0d623f7 445
6d2010ae
A
446 /* Create an array of objects large enough to hold an object
447 * for every dependency, an interface for each dependency, and a kext. */
448 rval = kxld_array_init(&context->objects,
449 kxld_object_sizeof(), 2 * ndependencies + 1);
450 require_noerr(rval, finish);
b0d623f7 451
6d2010ae
A
452 rval = kxld_array_init(&context->dependencies,
453 kxld_kext_sizeof(), ndependencies);
454 require_noerr(rval, finish);
b0d623f7 455
6d2010ae
A
456 rval = kxld_dict_init(&context->defined_symbols_by_name,
457 kxld_dict_string_hash, kxld_dict_string_cmp, 0);
458 require_noerr(rval, finish);
b0d623f7 459
6d2010ae
A
460 rval = kxld_dict_init(&context->defined_cxx_symbols_by_value,
461 kxld_dict_kxldaddr_hash, kxld_dict_kxldaddr_cmp, 0);
462 require_noerr(rval, finish);
b0d623f7 463
6d2010ae
A
464 rval = kxld_dict_init(&context->obsolete_symbols_by_name,
465 kxld_dict_string_hash, kxld_dict_string_cmp, 0);
466 require_noerr(rval, finish);
b0d623f7 467
6d2010ae
A
468 rval = kxld_dict_init(&context->vtables_by_name, kxld_dict_string_hash,
469 kxld_dict_string_cmp, 0);
470 require_noerr(rval, finish);
b0d623f7 471
6d2010ae
A
472 rval = KERN_SUCCESS;
473finish:
474 return rval;
475}
b0d623f7 476
6d2010ae 477/*******************************************************************************
39037602
A
478 *******************************************************************************/
479static kern_return_t
480init_kext_objects(KXLDContext *context,
481 u_char *file,
482 u_long size,
483 const char *name,
484 KXLDDependency *dependencies,
485 u_int ndependencies)
6d2010ae
A
486{
487 kern_return_t rval = KERN_FAILURE;
488 KXLDKext *kext = NULL;
489 KXLDObject *kext_object = NULL;
490 KXLDObject *interface_object = NULL;
491 u_int i = 0;
39037602 492
6d2010ae
A
493 /* Create a kext object for each dependency. If it's a direct dependency,
494 * export its symbols by name by value. If it's indirect, just export the
495 * C++ symbols by value.
496 */
316670eb
A
497 for (i = 0; i < ndependencies; ++i) {
498 kext = kxld_array_get_item(&context->dependencies, i);
499 kext_object = NULL;
6d2010ae 500 interface_object = NULL;
39037602 501
6d2010ae 502 kext_object = get_object_for_file(context, dependencies[i].kext,
39037602 503 dependencies[i].kext_size, dependencies[i].kext_name);
6d2010ae 504 require_action(kext_object, finish, rval=KERN_FAILURE);
39037602 505
6d2010ae 506 if (dependencies[i].interface) {
39037602
A
507 interface_object = get_object_for_file(context,
508 dependencies[i].interface, dependencies[i].interface_size,
509 dependencies[i].interface_name);
6d2010ae 510 require_action(interface_object, finish, rval=KERN_FAILURE);
b0d623f7 511 }
39037602 512
6d2010ae
A
513 rval = kxld_kext_init(kext, kext_object, interface_object);
514 require_noerr(rval, finish);
39037602 515
6d2010ae
A
516 if (dependencies[i].is_direct_dependency) {
517 rval = kxld_kext_export_symbols(kext,
39037602
A
518 &context->defined_symbols_by_name,
519 &context->obsolete_symbols_by_name,
520 &context->defined_cxx_symbols_by_value);
b0d623f7 521 require_noerr(rval, finish);
6d2010ae 522 } else {
39037602
A
523 rval = kxld_kext_export_symbols(kext,
524 /* defined_symbols */ NULL, /* obsolete_symbols */ NULL,
525 &context->defined_cxx_symbols_by_value);
b0d623f7
A
526 require_noerr(rval, finish);
527 }
528 }
39037602 529
6d2010ae
A
530 /* Export the vtables for all of the dependencies. */
531 for (i = 0; i < context->dependencies.nitems; ++i) {
532 kext = kxld_array_get_item(&context->dependencies, i);
39037602 533
6d2010ae 534 rval = kxld_kext_export_vtables(kext,
39037602
A
535 &context->defined_cxx_symbols_by_value,
536 &context->defined_symbols_by_name,
537 &context->vtables_by_name);
b0d623f7
A
538 require_noerr(rval, finish);
539 }
39037602 540
6d2010ae 541 /* Create a kext object for the kext we're linking and export its locally
39037602 542 * defined C++ symbols.
6d2010ae
A
543 */
544 kext_object = get_object_for_file(context, file, size, name);
545 require_action(kext_object, finish, rval=KERN_FAILURE);
39037602 546
6d2010ae
A
547 rval = kxld_kext_init(context->kext, kext_object, /* interface */ NULL);
548 require_noerr(rval, finish);
39037602 549
6d2010ae 550 rval = kxld_kext_export_symbols(context->kext,
39037602
A
551 /* defined_symbols */ NULL, /* obsolete_symbols */ NULL,
552 &context->defined_cxx_symbols_by_value);
6d2010ae 553 require_noerr(rval, finish);
39037602 554
6d2010ae
A
555 rval = KERN_SUCCESS;
556finish:
557 return rval;
558}
b0d623f7 559
6d2010ae
A
560/*******************************************************************************
561*******************************************************************************/
562static KXLDObject *
563get_object_for_file(KXLDContext *context, u_char *file, u_long size,
564 const char *name)
565{
566 KXLDObject *rval = NULL;
567 KXLDObject *object = NULL;
568 kern_return_t result = 0;
569 u_int i = 0;
b0d623f7 570
6d2010ae
A
571 for (i = 0; i < context->objects.nitems; ++i) {
572 object = kxld_array_get_item(&context->objects, i);
b0d623f7 573
6d2010ae
A
574 if (!kxld_object_get_file(object)) {
575 result = kxld_object_init_from_macho(object, file, size, name,
316670eb 576 context->section_order, context->cputype, context->cpusubtype, context->flags);
6d2010ae 577 require_noerr(result, finish);
b0d623f7 578
6d2010ae
A
579 rval = object;
580 break;
581 }
b0d623f7 582
6d2010ae
A
583 if (kxld_object_get_file(object) == file) {
584 rval = object;
585 break;
586 }
587 }
b0d623f7 588
6d2010ae
A
589finish:
590 return rval;
591}
39037602
A
592
593#include <mach-o/loader.h>
594
6d2010ae 595/*******************************************************************************
39037602
A
596 *******************************************************************************/
597static kern_return_t
598allocate_split_kext(KXLDContext *context, splitKextLinkInfo * link_info)
599{
600 kern_return_t rval = KERN_FAILURE;
601 u_long vmsize = 0;
602 u_long header_size = 0;
603 u_char * linked_object = NULL;
604
605 kxld_kext_get_vmsize(context->kext, &header_size, &vmsize);
606
607 if (isSplitKext) {
608 /* get __LINKEDIT vmsize */
609 kxld_kext_get_vmsize_for_seg_by_name(context->kext, SEG_LINKEDIT, &vmsize);
610 // add in the gaps
611 vmsize += (link_info->vmaddr_LINKEDIT - link_info->vmaddr_TEXT);
612 }
613 link_info->linkedKextSize = vmsize;
614
615 linked_object = kxld_page_alloc_untracked(link_info->linkedKextSize);
616 require(linked_object, finish);
617 link_info->linkedKext = linked_object;
618
619 bzero(linked_object, vmsize);
620 rval = KERN_SUCCESS;
621
622finish:
623 return rval;
624}
625
626/*******************************************************************************
627 *******************************************************************************/
6d2010ae 628static u_char *
39037602
A
629allocate_kext(KXLDContext *context,
630 void *callback_data,
631 kxld_addr_t *vmaddr_out,
632 u_long *vmsize_out,
633 u_char **linked_object_alloc_out)
6d2010ae
A
634{
635 KXLDAllocateFlags flags = 0;
636 kxld_addr_t vmaddr = 0;
637 u_long vmsize = 0;
638 u_long header_size = 0;
639 u_char * linked_object = NULL;
39037602 640
6d2010ae 641 *linked_object_alloc_out = NULL;
39037602 642
6d2010ae 643 kxld_kext_get_vmsize(context->kext, &header_size, &vmsize);
39037602 644
6d2010ae 645 vmaddr = context->allocate_callback(vmsize, &flags, callback_data);
3e170ce0 646 require_action(!(vmaddr & (kxld_get_effective_page_size()-1)), finish,
39037602
A
647 kxld_log(kKxldLogLinking, kKxldLogErr,
648 "Load address %p is not page-aligned.",
649 (void *) (uintptr_t) vmaddr));
650
6d2010ae
A
651 if (flags & kKxldAllocateWritable) {
652 linked_object = (u_char *) (u_long) vmaddr;
653 } else {
654 linked_object = kxld_page_alloc_untracked(vmsize);
655 require(linked_object, finish);
39037602 656
6d2010ae
A
657 *linked_object_alloc_out = linked_object;
658 }
39037602 659
316670eb
A
660 kxld_kext_set_linked_object_size(context->kext, vmsize);
661
6d2010ae
A
662 /* Zero out the memory before we fill it. We fill this buffer in a
663 * sparse fashion, and it's simpler to clear it now rather than
664 * track and zero any pieces we didn't touch after we've written
665 * all of the sections to memory.
666 */
667 bzero(linked_object, vmsize);
668 *vmaddr_out = vmaddr;
669 *vmsize_out = vmsize;
39037602 670
6d2010ae
A
671finish:
672 return linked_object;
b0d623f7
A
673}
674
675/*******************************************************************************
676*******************************************************************************/
677static void
678clear_context(KXLDContext *context)
679{
6d2010ae
A
680 KXLDObject * object = NULL;
681 KXLDKext * dep = NULL;
b0d623f7
A
682 u_int i = 0;
683
684 check(context);
685
686 kxld_kext_clear(context->kext);
6d2010ae
A
687
688 for (i = 0; i < context->objects.nitems; ++i) {
689 object = kxld_array_get_item(&context->objects, i);
690 kxld_object_clear(object);
691 }
692 kxld_array_reset(&context->objects);
693
694 for (i = 0; i < context->dependencies.nitems; ++i) {
695 dep = kxld_array_get_item(&context->dependencies, i);
696 kxld_kext_clear(dep);
b0d623f7 697 }
6d2010ae 698 kxld_array_reset(&context->dependencies);
b0d623f7 699
6d2010ae
A
700 kxld_dict_clear(&context->defined_symbols_by_name);
701 kxld_dict_clear(&context->defined_cxx_symbols_by_value);
702 kxld_dict_clear(&context->obsolete_symbols_by_name);
703 kxld_dict_clear(&context->vtables_by_name);
b0d623f7 704}