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