2 * Copyright (c) 2007-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <mach/boolean.h>
30 #include <sys/types.h>
33 #include <libkern/libkern.h>
34 #include <mach/machine.h>
37 #include <libkern/OSByteOrder.h>
39 /* Get machine.h from the kernel source so we can support all platforms
40 * that the kernel supports. Otherwise we're at the mercy of the host.
42 #include "../../osfmk/mach/machine.h"
45 #define DEBUG_ASSERT_COMPONENT_NAME_STRING "kxld"
46 #include <AssertMacros.h>
48 #include "kxld_array.h"
49 #include "kxld_demangle.h"
50 #include "kxld_dict.h"
51 #include "kxld_reloc.h"
52 #include "kxld_sect.h"
55 #include "kxld_symtab.h"
56 #include "kxld_util.h"
57 #include "kxld_vtable.h"
60 /* This will try to pull in mach/machine.h, so it has to come after the
61 * explicit include above.
63 #include <mach-o/loader.h>
66 /* include target-specific relocation prototypes */
67 #include <mach-o/reloc.h>
68 #if KXLD_USER_OR_X86_64
69 #include <mach-o/x86_64/reloc.h>
72 #include <mach-o/arm/reloc.h>
74 #if KXLD_USER_OR_ARM64
75 #include <mach-o/arm64/reloc.h>
78 extern uint32_t kaslr_offsets_index
;
79 extern uint32_t kaslr_offsets_count
;
80 extern uint32_t *kaslr_offsets
;
82 #define KXLD_TARGET_NONE (u_int) 0x0
83 #define KXLD_TARGET_VALUE (u_int) 0x1
84 #define KXLD_TARGET_SECTNUM (u_int) 0x2
85 #define KXLD_TARGET_SYMBOLNUM (u_int) 0x3
86 #define KXLD_TARGET_LOOKUP (u_int) 0x4
87 #define KXLD_TARGET_GOT (u_int) 0x5
89 #define ABSOLUTE_VALUE(x) (((x) < 0) ? -(x) : (x))
91 #define LO16(x) (0x0000FFFF & x)
92 #define LO16S(x) ((0x0000FFFF & x) << 16)
93 #define HI16(x) (0xFFFF0000 & x)
94 #define HI16S(x) ((0xFFFF0000 & x) >> 16)
95 #define BIT15(x) (0x00008000 & x)
96 #define BR14I(x) (0xFFFF0003 & x)
97 #define BR14D(x) (0x0000FFFC & x)
98 #define BR24I(x) (0xFC000003 & x)
99 #define BR24D(x) (0x03FFFFFC & x)
100 #define HADISP 0x00010000
101 #define BR14_LIMIT 0x00008000
102 #define BR24_LIMIT 0x02000000
103 #define IS_COND_BR_INSTR(x) ((x & 0xFC000000) == 0x40000000)
104 #define IS_NOT_ALWAYS_TAKEN(x) ((x & 0x03E00000) != 0x02800000)
105 #define FLIP_PREDICT_BIT(x) x ^= 0x00200000
107 #define SIGN_EXTEND_MASK(n) (1 << ((n) - 1))
108 #define SIGN_EXTEND(x, n) (((x) ^ SIGN_EXTEND_MASK(n)) - SIGN_EXTEND_MASK(n))
109 #define BR14_NBITS_DISPLACEMENT 16
110 #define BR24_NBITS_DISPLACEMENT 26
112 #define X86_64_RIP_RELATIVE_LIMIT 0x80000000UL
114 /*******************************************************************************
116 *******************************************************************************/
117 #if KXLD_USER_OR_I386
118 static boolean_t
generic_reloc_has_pair(u_int _type
)
119 __attribute__((const));
120 static u_int
generic_reloc_get_pair_type(u_int _prev_type
)
121 __attribute__((const));
122 static boolean_t
generic_reloc_has_got(u_int _type
)
123 __attribute__((const));
124 static kern_return_t
generic_process_reloc(const KXLDRelocator
*relocator
,
125 u_char
*instruction
, u_int length
, u_int pcrel
, kxld_addr_t base_pc
,
126 kxld_addr_t link_pc
, kxld_addr_t link_disp
, u_int type
, kxld_addr_t target
,
127 kxld_addr_t pair_target
, boolean_t swap
);
128 #endif /* KXLD_USER_OR_I386 */
130 #if KXLD_USER_OR_X86_64
131 static boolean_t
x86_64_reloc_has_pair(u_int _type
)
132 __attribute__((const));
133 static u_int
x86_64_reloc_get_pair_type(u_int _prev_type
)
134 __attribute__((const));
135 static boolean_t
x86_64_reloc_has_got(u_int _type
)
136 __attribute__((const));
137 static kern_return_t
x86_64_process_reloc(const KXLDRelocator
*relocator
,
138 u_char
*instruction
, u_int length
, u_int pcrel
, kxld_addr_t base_pc
,
139 kxld_addr_t link_pc
, kxld_addr_t link_disp
, u_int type
, kxld_addr_t target
,
140 kxld_addr_t pair_target
, boolean_t swap
);
141 static kern_return_t
calculate_displacement_x86_64(uint64_t target
,
142 uint64_t adjustment
, int32_t *instr32
);
143 #endif /* KXLD_USER_OR_X86_64 */
146 static boolean_t
arm_reloc_has_pair(u_int _type
)
147 __attribute__((const));
148 static u_int
arm_reloc_get_pair_type(u_int _prev_type
)
149 __attribute__((const));
150 static boolean_t
arm_reloc_has_got(u_int _type
)
151 __attribute__((const));
152 static kern_return_t
arm_process_reloc(const KXLDRelocator
*relocator
,
153 u_char
*instruction
, u_int length
, u_int pcrel
, kxld_addr_t base_pc
,
154 kxld_addr_t link_pc
, kxld_addr_t link_disp
, u_int type
, kxld_addr_t target
,
155 kxld_addr_t pair_target
, boolean_t swap
);
156 #endif /* KXLD_USER_OR_ARM */
158 #if KXLD_USER_OR_ARM64
159 static boolean_t
arm64_reloc_has_pair(u_int _type
)
160 __attribute__((const));
161 static u_int
arm64_reloc_get_pair_type(u_int _prev_type
)
162 __attribute__((const));
163 static boolean_t
arm64_reloc_has_got(u_int _type
)
164 __attribute__((const));
165 static kern_return_t
arm64_process_reloc(const KXLDRelocator
*relocator
,
166 u_char
*instruction
, u_int length
, u_int pcrel
, kxld_addr_t base_pc
,
167 kxld_addr_t link_pc
, kxld_addr_t link_disp
, u_int type
, kxld_addr_t target
,
168 kxld_addr_t pair_target
, boolean_t swap
);
169 #endif /* KXLD_USER_OR_ARM64 */
171 #if KXLD_USER_OR_ILP32
172 static kxld_addr_t
get_pointer_at_addr_32(const KXLDRelocator
*relocator
,
173 const u_char
*data
, u_long offset
)
174 __attribute__((pure
, nonnull
));
175 #endif /* KXLD_USER_OR_ILP32 */
176 #if KXLD_USER_OR_LP64
177 static kxld_addr_t
get_pointer_at_addr_64(const KXLDRelocator
*relocator
,
178 const u_char
*data
, u_long offset
)
179 __attribute__((pure
, nonnull
));
180 #endif /* KXLD_USER_OR_LP64 */
182 static u_int
count_relocatable_relocs(const KXLDRelocator
*relocator
,
183 const struct relocation_info
*relocs
, u_int nrelocs
)
184 __attribute__((pure
));
186 static kern_return_t
calculate_targets(KXLDRelocator
*relocator
,
187 kxld_addr_t
*_target
, kxld_addr_t
*_pair_target
, const KXLDReloc
*reloc
);
189 static kxld_addr_t
align_raw_function_address(const KXLDRelocator
*relocator
,
192 static kern_return_t
get_target_by_address_lookup(kxld_addr_t
*target
,
193 kxld_addr_t addr
, const KXLDArray
*sectarray
);
195 static kern_return_t
check_for_direct_pure_virtual_call(
196 const KXLDRelocator
*relocator
, u_long offset
);
199 static u_long
get_macho_data_size_for_array(const KXLDArray
*relocs
);
201 static kern_return_t
export_macho_for_array(const KXLDRelocator
*relocator
,
202 const KXLDArray
*relocs
, struct relocation_info
**dstp
);
203 #endif /* KXLD_PIC_KEXTS */
205 /*******************************************************************************
206 *******************************************************************************/
208 kxld_relocator_init(KXLDRelocator
*relocator
, u_char
*file
,
209 const KXLDSymtab
*symtab
, const KXLDArray
*sectarray
, cpu_type_t cputype
,
210 cpu_subtype_t cpusubtype __unused
, boolean_t swap
)
212 kern_return_t rval
= KERN_FAILURE
;
217 #if KXLD_USER_OR_I386
219 relocator
->reloc_has_pair
= generic_reloc_has_pair
;
220 relocator
->reloc_get_pair_type
= generic_reloc_get_pair_type
;
221 relocator
->reloc_has_got
= generic_reloc_has_got
;
222 relocator
->process_reloc
= generic_process_reloc
;
223 relocator
->function_align
= 0;
224 relocator
->is_32_bit
= TRUE
;
225 relocator
->may_scatter
= TRUE
;
227 #endif /* KXLD_USER_OR_I386 */
228 #if KXLD_USER_OR_X86_64
229 case CPU_TYPE_X86_64
:
230 relocator
->reloc_has_pair
= x86_64_reloc_has_pair
;
231 relocator
->reloc_get_pair_type
= x86_64_reloc_get_pair_type
;
232 relocator
->reloc_has_got
= x86_64_reloc_has_got
;
233 relocator
->process_reloc
= x86_64_process_reloc
;
234 relocator
->function_align
= 0;
235 relocator
->is_32_bit
= FALSE
;
236 relocator
->may_scatter
= FALSE
;
238 #endif /* KXLD_USER_OR_X86_64 */
241 relocator
->reloc_has_pair
= arm_reloc_has_pair
;
242 relocator
->reloc_get_pair_type
= arm_reloc_get_pair_type
;
243 relocator
->reloc_has_got
= arm_reloc_has_got
;
244 relocator
->process_reloc
= arm_process_reloc
;
245 relocator
->function_align
= 1;
246 relocator
->is_32_bit
= TRUE
;
247 relocator
->may_scatter
= FALSE
;
249 #endif /* KXLD_USER_OR_ARM */
250 #if KXLD_USER_OR_ARM64
252 relocator
->reloc_has_pair
= arm64_reloc_has_pair
;
253 relocator
->reloc_get_pair_type
= arm64_reloc_get_pair_type
;
254 relocator
->reloc_has_got
= arm64_reloc_has_got
;
255 relocator
->process_reloc
= arm64_process_reloc
;
256 relocator
->function_align
= 0;
257 relocator
->is_32_bit
= FALSE
;
258 relocator
->may_scatter
= FALSE
;
260 #endif /* KXLD_USER_OR_ARM64 */
264 kxld_log(kKxldLogLinking
, kKxldLogErr
,
265 kKxldLogArchNotSupported
, cputype
);
269 relocator
->file
= file
;
270 relocator
->symtab
= symtab
;
271 relocator
->sectarray
= sectarray
;
272 relocator
->is_32_bit
= kxld_is_32_bit(cputype
);
273 relocator
->swap
= swap
;
281 /*******************************************************************************
282 *******************************************************************************/
284 kxld_reloc_create_macho(KXLDArray
*relocarray
, const KXLDRelocator
*relocator
,
285 const struct relocation_info
*srcs
, u_int nsrcs
)
287 kern_return_t rval
= KERN_FAILURE
;
288 KXLDReloc
*reloc
= NULL
;
290 const struct relocation_info
*src
= NULL
;
291 const struct scattered_relocation_info
*scatsrc
= NULL
;
293 u_int reloc_index
= 0;
298 /* If there are no relocation entries, just return */
304 /* Count the number of non-pair relocs */
305 nrelocs
= count_relocatable_relocs(relocator
, srcs
, nsrcs
);
308 /* Allocate the array of relocation entries */
310 rval
= kxld_array_init(relocarray
, sizeof(KXLDReloc
), nrelocs
);
311 require_noerr(rval
, finish
);
313 /* Initialize the relocation entries */
315 for (i
= 0; i
< nsrcs
; ++i
) {
317 scatsrc
= (const struct scattered_relocation_info
*) src
;
319 /* A section-based relocation entry can be skipped for absolute
323 if (!(relocator
->may_scatter
&& (src
->r_address
& R_SCATTERED
)) &&
324 !(src
->r_extern
) && (R_ABS
== src
->r_symbolnum
)) {
328 /* Pull out the data from the relocation entries. The target_type
329 * depends on the r_extern bit:
330 * Scattered -> Section Lookup by Address
331 * Local (not extern) -> Section by Index
332 * Extern -> Symbolnum by Index
334 reloc
= kxld_array_get_item(relocarray
, reloc_index
++);
335 if (relocator
->may_scatter
&& (src
->r_address
& R_SCATTERED
)) {
336 reloc
->address
= scatsrc
->r_address
;
337 reloc
->pcrel
= scatsrc
->r_pcrel
;
338 reloc
->length
= scatsrc
->r_length
;
339 reloc
->reloc_type
= scatsrc
->r_type
;
340 reloc
->target
= scatsrc
->r_value
;
341 reloc
->target_type
= KXLD_TARGET_LOOKUP
;
343 reloc
->address
= src
->r_address
;
344 reloc
->pcrel
= src
->r_pcrel
;
345 reloc
->length
= src
->r_length
;
346 reloc
->reloc_type
= src
->r_type
;
347 reloc
->target
= src
->r_symbolnum
;
349 if (0 == src
->r_extern
) {
350 reloc
->target_type
= KXLD_TARGET_SECTNUM
;
353 reloc
->target_type
= KXLD_TARGET_SYMBOLNUM
;
357 /* Find the pair entry if it exists */
359 if (relocator
->reloc_has_pair(reloc
->reloc_type
)) {
361 require_action(i
< nsrcs
, finish
, rval
= KERN_FAILURE
);
364 scatsrc
= (const struct scattered_relocation_info
*) src
;
366 if (relocator
->may_scatter
&& (src
->r_address
& R_SCATTERED
)) {
367 require_action(relocator
->reloc_get_pair_type(
368 reloc
->reloc_type
) == scatsrc
->r_type
,
369 finish
, rval
= KERN_FAILURE
);
370 reloc
->pair_address
= scatsrc
->r_address
;
371 reloc
->pair_target
= scatsrc
->r_value
;
372 reloc
->pair_target_type
= KXLD_TARGET_LOOKUP
;
374 require_action(relocator
->reloc_get_pair_type(
375 reloc
->reloc_type
) == scatsrc
->r_type
,
376 finish
, rval
= KERN_FAILURE
);
377 reloc
->pair_address
= scatsrc
->r_address
;
379 reloc
->pair_target
= src
->r_symbolnum
;
380 reloc
->pair_target_type
= KXLD_TARGET_SYMBOLNUM
;
382 reloc
->pair_target
= src
->r_address
;
383 reloc
->pair_target_type
= KXLD_TARGET_VALUE
;
387 reloc
->pair_target
= 0;
388 if (relocator
->reloc_has_got(reloc
->reloc_type
)) {
389 reloc
->pair_target_type
= KXLD_TARGET_GOT
;
391 reloc
->pair_target_type
= KXLD_TARGET_NONE
;
403 /*******************************************************************************
404 * Relocatable relocs :
405 * 1) Are not _PAIR_ relocs
406 * 2) Don't reference N_ABS symbols
407 *******************************************************************************/
409 count_relocatable_relocs(const KXLDRelocator
*relocator
,
410 const struct relocation_info
*relocs
, u_int nrelocs
)
412 u_int num_nonpair_relocs
= 0;
414 const struct relocation_info
*reloc
= NULL
;
415 const struct scattered_relocation_info
*sreloc
= NULL
;
420 /* Loop over all of the relocation entries */
422 num_nonpair_relocs
= 1;
423 for (i
= 1; i
< nrelocs
; ++i
) {
426 if (reloc
->r_address
& R_SCATTERED
) {
427 /* A scattered relocation entry is relocatable as long as it's not a
430 sreloc
= (const struct scattered_relocation_info
*) reloc
;
432 num_nonpair_relocs
+=
433 !relocator
->reloc_has_pair(sreloc
->r_type
);
435 /* A normal relocation entry is relocatable if it is not a pair and
436 * if it is not a section-based relocation for an absolute symbol.
438 num_nonpair_relocs
+=
439 !(relocator
->reloc_has_pair(reloc
->r_type
)
440 || (0 == reloc
->r_extern
&& R_ABS
== reloc
->r_symbolnum
));
444 return num_nonpair_relocs
;
447 /*******************************************************************************
448 *******************************************************************************/
450 kxld_relocator_clear(KXLDRelocator
*relocator
)
452 bzero(relocator
, sizeof(*relocator
));
455 /*******************************************************************************
456 *******************************************************************************/
458 kxld_relocator_has_pair(const KXLDRelocator
*relocator
, u_int r_type
)
462 return relocator
->reloc_has_pair(r_type
);
465 /*******************************************************************************
466 *******************************************************************************/
468 kxld_relocator_get_pair_type(const KXLDRelocator
*relocator
,
473 return relocator
->reloc_get_pair_type(prev_r_type
);
476 /*******************************************************************************
477 *******************************************************************************/
479 kxld_relocator_has_got(const KXLDRelocator
*relocator
, u_int r_type
)
483 return relocator
->reloc_has_got(r_type
);
486 /*******************************************************************************
487 *******************************************************************************/
489 kxld_reloc_get_symbol(const KXLDRelocator
*relocator
, const KXLDReloc
*reloc
,
493 kxld_addr_t value
= 0;
497 switch (reloc
->target_type
) {
498 case KXLD_TARGET_SYMBOLNUM
:
499 sym
= kxld_symtab_get_symbol_by_index(relocator
->symtab
, reloc
->target
);
501 case KXLD_TARGET_SECTNUM
:
503 value
= kxld_relocator_get_pointer_at_addr(relocator
, data
,
505 sym
= kxld_symtab_get_cxx_symbol_by_value(relocator
->symtab
, value
);
516 /*******************************************************************************
517 *******************************************************************************/
519 kxld_reloc_get_reloc_index_by_offset(const KXLDArray
*relocs
,
520 kxld_size_t offset
, u_int
*idx
)
522 kern_return_t rval
= KERN_FAILURE
;
523 KXLDReloc
*reloc
= NULL
;
526 for (i
= 0; i
< relocs
->nitems
; ++i
) {
527 reloc
= kxld_array_get_item(relocs
, i
);
528 if (reloc
->address
== offset
) {
533 if (i
>= relocs
->nitems
) {
545 /*******************************************************************************
546 *******************************************************************************/
548 kxld_reloc_get_reloc_by_offset(const KXLDArray
*relocs
, kxld_addr_t offset
)
550 kern_return_t rval
= KERN_FAILURE
;
551 KXLDReloc
*reloc
= NULL
;
554 rval
= kxld_reloc_get_reloc_index_by_offset(relocs
, offset
, &i
);
559 reloc
= kxld_array_get_item(relocs
, i
);
566 /*******************************************************************************
567 *******************************************************************************/
569 kxld_reloc_get_macho_header_size()
571 return sizeof(struct dysymtab_command
);
574 /*******************************************************************************
575 *******************************************************************************/
577 kxld_reloc_get_macho_data_size(
578 const KXLDArray
*locrelocs
,
579 const KXLDArray
*extrelocs
)
583 rval
+= get_macho_data_size_for_array(locrelocs
);
584 rval
+= get_macho_data_size_for_array(extrelocs
);
589 /*******************************************************************************
590 *******************************************************************************/
592 kxld_reloc_export_macho(const KXLDRelocator
*relocator
,
593 const KXLDArray
*locrelocs
, const KXLDArray
*extrelocs
,
594 u_char
*buf
, u_long
*header_offset
, u_long header_size
,
595 u_long
*data_offset
, u_long size
)
597 kern_return_t rval
= KERN_FAILURE
;
598 struct dysymtab_command
*dysymtabhdr
= NULL
;
599 struct relocation_info
*start
= NULL
;
600 struct relocation_info
*dst
= NULL
;
602 u_long data_size
= 0;
607 check(header_offset
);
610 require_action(sizeof(*dysymtabhdr
) <= header_size
- *header_offset
, finish
, rval
= KERN_FAILURE
);
611 dysymtabhdr
= (struct dysymtab_command
*) ((void *) (buf
+ *header_offset
));
612 *header_offset
+= sizeof(*dysymtabhdr
);
614 data_size
= kxld_reloc_get_macho_data_size(locrelocs
, extrelocs
);
615 require_action((*data_offset
+ data_size
) <= size
, finish
, rval
= KERN_FAILURE
);
617 start
= dst
= (struct relocation_info
*) ((void *) (buf
+ *data_offset
));
619 if (kaslr_offsets
== NULL
) {
620 kaslr_offsets_index
= 0;
621 kaslr_offsets_count
= locrelocs
->nitems
+ extrelocs
->nitems
;
622 kaslr_offsets
= (uint32_t *)calloc(kaslr_offsets_count
, sizeof(*kaslr_offsets
));
625 // copies the reloc data into the __LINKEDIT segment
626 // data_offset is the new value for locreloff
627 rval
= export_macho_for_array(relocator
, locrelocs
, &dst
);
628 require_noerr(rval
, finish
);
630 rval
= export_macho_for_array(relocator
, extrelocs
, &dst
);
631 require_noerr(rval
, finish
);
635 memset(dysymtabhdr
, 0, sizeof(*dysymtabhdr
));
636 dysymtabhdr
->cmd
= LC_DYSYMTAB
;
637 dysymtabhdr
->cmdsize
= (uint32_t) sizeof(*dysymtabhdr
);
638 dysymtabhdr
->locreloff
= (uint32_t) *data_offset
;
639 dysymtabhdr
->nlocrel
= (uint32_t) count
;
641 *data_offset
+= count
* sizeof(struct relocation_info
);
643 #if SPLIT_KEXTS_DEBUG
644 kxld_log(kKxldLogLinking
, kKxldLogErr
,
645 "%p >>> Start of dysymtabhdr (size %lu) <%s> ",
646 (void *) dysymtabhdr
,
647 sizeof(*dysymtabhdr
),
649 kxld_log(kKxldLogLinking
, kKxldLogErr
,
650 "%p <<< End of dysymtabhdr <%s> ",
651 (void *) ((u_char
*)dysymtabhdr
+ sizeof(*dysymtabhdr
)),
654 kxld_log(kKxldLogLinking
, kKxldLogErr
,
655 "dysymtabhdr at %p: cmdsize %u indirectsymoff %u nindirectsyms %u extreloff %u nextrel %u locreloff %u nlocrel %u <%s>",
656 (void *) dysymtabhdr
,
657 dysymtabhdr
->cmdsize
,
658 dysymtabhdr
->indirectsymoff
,
659 dysymtabhdr
->nindirectsyms
,
660 dysymtabhdr
->extreloff
,
661 dysymtabhdr
->nextrel
,
662 dysymtabhdr
->locreloff
,
663 dysymtabhdr
->nlocrel
,
671 #endif /* KXLD_PIC_KEXTS */
673 /*******************************************************************************
674 *******************************************************************************/
676 kxld_relocator_get_pointer_at_addr(const KXLDRelocator
*relocator
,
677 const u_char
*data
, u_long offset
)
681 KXLD_3264_FUNC(relocator
->is_32_bit
, value
,
682 get_pointer_at_addr_32
, get_pointer_at_addr_64
,
683 relocator
, data
, offset
);
688 #if KXLD_USER_OR_ILP32
689 /*******************************************************************************
690 *******************************************************************************/
692 get_pointer_at_addr_32(const KXLDRelocator
*relocator
,
693 const u_char
*data
, u_long offset
)
699 addr
= *(const uint32_t *) ((const void *) (data
+ offset
));
701 if (relocator
->swap
) {
702 addr
= OSSwapInt32(addr
);
706 return align_raw_function_address(relocator
, addr
);
708 #endif /* KXLD_USER_OR_ILP32 */
710 #if KXLD_USER_OR_LP64
711 /*******************************************************************************
712 *******************************************************************************/
714 get_pointer_at_addr_64(const KXLDRelocator
*relocator
,
715 const u_char
*data
, u_long offset
)
721 addr
= *(const uint64_t *) ((const void *) (data
+ offset
));
724 if (relocator
->swap
) {
725 addr
= OSSwapInt64(addr
);
729 return align_raw_function_address(relocator
, addr
);
731 #endif /* KXLD_USER_OR_LP64 */
733 /*******************************************************************************
734 *******************************************************************************/
736 kxld_relocator_set_vtables(KXLDRelocator
*relocator
, const KXLDDict
*vtables
)
738 relocator
->vtables
= vtables
;
741 /*******************************************************************************
742 * When we're inspecting the raw binary and not the symbol table, value may
743 * hold a THUMB address (with bit 0 set to 1) but the index will have the real
744 * address (bit 0 set to 0). So if bit 0 is set here, we clear it. This only
745 * impacts ARM for now, but it's implemented as a generic function alignment
747 *******************************************************************************/
749 align_raw_function_address(const KXLDRelocator
*relocator
, kxld_addr_t value
)
751 if (relocator
->function_align
) {
752 value
&= ~((1ULL << relocator
->function_align
) - 1);
758 /*******************************************************************************
759 *******************************************************************************/
761 kxld_relocator_process_sect_reloc(KXLDRelocator
*relocator
,
762 const KXLDReloc
*reloc
, const KXLDSect
*sect
)
764 kern_return_t rval
= KERN_FAILURE
;
765 u_char
*instruction
= NULL
;
766 kxld_addr_t target
= 0;
767 kxld_addr_t pair_target
= 0;
768 kxld_addr_t base_pc
= 0;
769 kxld_addr_t link_pc
= 0;
770 kxld_addr_t link_disp
= 0;
776 /* Find the instruction */
778 instruction
= sect
->data
+ reloc
->address
;
780 /* Calculate the target */
782 rval
= calculate_targets(relocator
, &target
, &pair_target
, reloc
);
783 require_noerr(rval
, finish
);
785 base_pc
= reloc
->address
;
786 link_pc
= base_pc
+ sect
->link_addr
;
787 link_disp
= sect
->link_addr
- sect
->base_addr
;
791 rval
= relocator
->process_reloc(relocator
, instruction
, reloc
->length
,
792 reloc
->pcrel
, base_pc
, link_pc
, link_disp
, reloc
->reloc_type
, target
,
793 pair_target
, relocator
->swap
);
794 require_noerr(rval
, finish
);
798 relocator
->current_vtable
= NULL
;
805 /*******************************************************************************
806 *******************************************************************************/
808 kxld_reloc_update_symindex(KXLDReloc
*reloc
, u_int symindex
)
810 kern_return_t rval
= KERN_FAILURE
;
812 require_action(reloc
->target_type
== KXLD_TARGET_SYMBOLNUM
,
813 finish
, rval
= KERN_FAILURE
);
815 reloc
->target
= symindex
;
823 /*******************************************************************************
824 *******************************************************************************/
826 kxld_relocator_process_table_reloc(KXLDRelocator
*relocator
,
827 const KXLDReloc
*reloc
,
829 kxld_addr_t link_addr
)
831 kern_return_t rval
= KERN_FAILURE
;
832 u_char
*instruction
= NULL
;
833 kxld_addr_t target
= 0;
834 kxld_addr_t pair_target
= 0;
835 kxld_addr_t base_pc
= 0;
836 kxld_addr_t link_pc
= 0;
842 /* Find the instruction in original kext file we are trying to link */
844 offset
= (u_long
)(seg
->fileoff
+ (reloc
->address
- seg
->base_addr
));
845 instruction
= relocator
->file
+ offset
;
847 /* Calculate the target */
849 rval
= calculate_targets(relocator
, &target
, &pair_target
, reloc
);
850 require_noerr(rval
, finish
);
852 base_pc
= reloc
->address
;
853 link_pc
= base_pc
+ link_addr
;
854 if (kxld_seg_is_split_seg(seg
)) {
855 // link_pc for split segment special case, do not add in the base_pc
861 rval
= relocator
->process_reloc(relocator
, instruction
, reloc
->length
,
862 reloc
->pcrel
, base_pc
, link_pc
, link_addr
, reloc
->reloc_type
, target
,
863 pair_target
, relocator
->swap
);
864 require_noerr(rval
, finish
);
868 relocator
->current_vtable
= NULL
;
875 /*******************************************************************************
876 *******************************************************************************/
878 calculate_targets(KXLDRelocator
*relocator
, kxld_addr_t
*_target
,
879 kxld_addr_t
*_pair_target
, const KXLDReloc
*reloc
)
881 kern_return_t rval
= KERN_FAILURE
;
882 const KXLDSect
*sect
= NULL
;
883 const KXLDSym
*sym
= NULL
;
884 kxld_addr_t target
= 0;
885 kxld_addr_t pair_target
= 0;
886 char *demangled_name
= NULL
;
887 size_t demangled_length
= 0;
894 /* Find the target based on the lookup type */
896 switch (reloc
->target_type
) {
897 case KXLD_TARGET_LOOKUP
:
898 require_action(reloc
->pair_target_type
== KXLD_TARGET_NONE
||
899 reloc
->pair_target_type
== KXLD_TARGET_LOOKUP
||
900 reloc
->pair_target_type
== KXLD_TARGET_VALUE
,
901 finish
, rval
= KERN_FAILURE
);
903 rval
= get_target_by_address_lookup(&target
, reloc
->target
,
904 relocator
->sectarray
);
905 require_noerr(rval
, finish
);
907 if (reloc
->pair_target_type
== KXLD_TARGET_LOOKUP
) {
908 rval
= get_target_by_address_lookup(&pair_target
,
909 reloc
->pair_target
, relocator
->sectarray
);
910 require_noerr(rval
, finish
);
911 } else if (reloc
->pair_target_type
== KXLD_TARGET_VALUE
) {
912 pair_target
= reloc
->pair_target
;
915 case KXLD_TARGET_SECTNUM
:
916 require_action(reloc
->pair_target_type
== KXLD_TARGET_NONE
||
917 reloc
->pair_target_type
== KXLD_TARGET_VALUE
,
918 finish
, rval
= KERN_FAILURE
);
920 /* Get the target's section by section number */
921 sect
= kxld_array_get_item(relocator
->sectarray
, reloc
->target
);
922 require_action(sect
, finish
, rval
= KERN_FAILURE
);
924 /* target is the change in the section's address */
925 target
= sect
->link_addr
- sect
->base_addr
;
927 if (reloc
->pair_target_type
) {
928 pair_target
= reloc
->pair_target
;
930 /* x86_64 needs to know when we have a non-external relocation,
931 * so we hack that information in here.
936 case KXLD_TARGET_SYMBOLNUM
:
937 require_action(reloc
->pair_target_type
== KXLD_TARGET_NONE
||
938 reloc
->pair_target_type
== KXLD_TARGET_GOT
||
939 reloc
->pair_target_type
== KXLD_TARGET_SYMBOLNUM
||
940 reloc
->pair_target_type
== KXLD_TARGET_VALUE
, finish
,
941 rval
= KERN_FAILURE
);
943 /* Get the target's symbol by symbol number */
944 sym
= kxld_symtab_get_symbol_by_index(relocator
->symtab
, reloc
->target
);
945 require_action(sym
, finish
, rval
= KERN_FAILURE
);
947 /* If this symbol is a padslot that has already been replaced, then the
948 * only way a relocation entry can still reference it is if there is a
949 * vtable that has not been patched. The vtable patcher uses the
950 * MetaClass structure to find classes for patching, so an unpatched
951 * vtable means that there is an OSObject-dervied class that is missing
952 * its OSDeclare/OSDefine macros.
954 if (kxld_sym_is_padslot(sym
) && kxld_sym_is_replaced(sym
)) {
955 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogRelocatingPatchedSym
,
956 kxld_demangle(sym
->name
, &demangled_name
, &demangled_length
));
959 target
= sym
->link_addr
;
961 if (kxld_sym_is_vtable(sym
)) {
962 relocator
->current_vtable
= kxld_dict_find(relocator
->vtables
, sym
->name
);
965 /* Some relocation types need the GOT entry address instead of the
966 * symbol's actual address. These types don't have pair relocation
967 * entries, so we store the GOT entry address as the pair target.
969 if (reloc
->pair_target_type
== KXLD_TARGET_VALUE
) {
970 pair_target
= reloc
->pair_target
;
971 } else if (reloc
->pair_target_type
== KXLD_TARGET_SYMBOLNUM
) {
972 sym
= kxld_symtab_get_symbol_by_index(relocator
->symtab
,
974 require_action(sym
, finish
, rval
= KERN_FAILURE
);
975 pair_target
= sym
->link_addr
;
976 } else if (reloc
->pair_target_type
== KXLD_TARGET_GOT
) {
977 pair_target
= sym
->got_addr
;
986 *_pair_target
= pair_target
;
990 if (demangled_name
) {
991 kxld_free(demangled_name
, demangled_length
);
996 /*******************************************************************************
997 *******************************************************************************/
999 get_target_by_address_lookup(kxld_addr_t
*target
, kxld_addr_t addr
,
1000 const KXLDArray
*sectarray
)
1002 kern_return_t rval
= KERN_FAILURE
;
1003 const KXLDSect
*sect
= NULL
;
1004 kxld_addr_t start
= 0;
1005 kxld_addr_t end
= 0;
1012 for (i
= 0; i
< sectarray
->nitems
; ++i
) {
1013 sect
= kxld_array_get_item(sectarray
, i
);
1014 start
= sect
->base_addr
;
1015 end
= start
+ sect
->size
;
1017 if (start
<= addr
&& addr
< end
) {
1023 require_action(sect
, finish
, rval
= KERN_FAILURE
);
1025 *target
= sect
->link_addr
- sect
->base_addr
;
1026 rval
= KERN_SUCCESS
;
1032 /*******************************************************************************
1033 *******************************************************************************/
1034 static kern_return_t
1035 check_for_direct_pure_virtual_call(const KXLDRelocator
*relocator
, u_long offset
)
1037 kern_return_t rval
= KERN_FAILURE
;
1038 const KXLDVTableEntry
*entry
= NULL
;
1040 if (relocator
->current_vtable
) {
1041 entry
= kxld_vtable_get_entry_for_offset(relocator
->current_vtable
,
1042 offset
, relocator
->is_32_bit
);
1043 require_action(!entry
|| !entry
->patched
.name
||
1044 !kxld_sym_name_is_pure_virtual(entry
->patched
.name
),
1045 finish
, rval
= KERN_FAILURE
;
1046 kxld_log(kKxldLogLinking
, kKxldLogErr
,
1047 kKxldLogDirectPureVirtualCall
));
1050 rval
= KERN_SUCCESS
;
1056 /*******************************************************************************
1057 *******************************************************************************/
1059 get_macho_data_size_for_array(const KXLDArray
*relocs
)
1061 const KXLDReloc
*reloc
= NULL
;
1067 for (i
= 0; i
< relocs
->nitems
; ++i
) {
1068 reloc
= kxld_array_get_item(relocs
, i
);
1069 if (!reloc
->pcrel
) {
1070 size
+= sizeof(struct relocation_info
);
1071 if (reloc
->pair_target_type
!= KXLD_TARGET_NONE
) {
1072 size
+= sizeof(struct relocation_info
);
1080 /*******************************************************************************
1081 *******************************************************************************/
1082 static kern_return_t
1083 export_macho_for_array(const KXLDRelocator
*relocator
,
1084 const KXLDArray
*relocs
, struct relocation_info
**dstp
)
1086 kern_return_t rval
= KERN_FAILURE
;
1087 const KXLDReloc
*reloc
= NULL
;
1088 struct relocation_info
*dst
= NULL
;
1089 struct scattered_relocation_info
*scatdst
= NULL
;
1094 for (i
= 0; i
< relocs
->nitems
; ++i
) {
1095 reloc
= kxld_array_get_item(relocs
, i
);
1096 scatdst
= (struct scattered_relocation_info
*) dst
;
1102 switch (reloc
->target_type
) {
1103 case KXLD_TARGET_LOOKUP
:
1104 if (kaslr_offsets
) {
1105 if (kaslr_offsets_index
>= kaslr_offsets_count
) {
1106 kxld_log(kKxldLogLinking
, kKxldLogErr
,
1107 "kaslr_offsets overflow %d > %d <%s> ",
1108 kaslr_offsets_index
, kaslr_offsets_count
,
1112 // reloc->address is really an offset from the start of the kext
1113 *(kaslr_offsets
+ kaslr_offsets_index
++) = reloc
->address
;
1115 scatdst
->r_address
= reloc
->address
;
1116 scatdst
->r_pcrel
= reloc
->pcrel
;
1117 scatdst
->r_length
= reloc
->length
;
1118 scatdst
->r_type
= reloc
->reloc_type
;
1119 scatdst
->r_value
= reloc
->target
;
1120 scatdst
->r_scattered
= 1;
1122 case KXLD_TARGET_SECTNUM
:
1123 if (kaslr_offsets
) {
1124 if (kaslr_offsets_index
>= kaslr_offsets_count
) {
1125 kxld_log(kKxldLogLinking
, kKxldLogErr
,
1126 "kaslr_offsets overflow <%s> ", __func__
);
1129 // reloc->address is really an offset from the start of the kext
1130 *(kaslr_offsets
+ kaslr_offsets_index
++) = reloc
->address
;
1132 dst
->r_address
= reloc
->address
;
1133 dst
->r_pcrel
= reloc
->pcrel
;
1134 dst
->r_length
= reloc
->length
;
1135 dst
->r_type
= reloc
->reloc_type
;
1136 dst
->r_symbolnum
= reloc
->target
+ 1;
1139 case KXLD_TARGET_SYMBOLNUM
:
1140 /* Assume that everything will be slid together; otherwise,
1141 * there is no sensible value for the section number.
1143 if (kaslr_offsets
) {
1144 if (kaslr_offsets_index
>= kaslr_offsets_count
) {
1145 kxld_log(kKxldLogLinking
, kKxldLogErr
,
1146 "kaslr_offsets overflow <%s> ", __func__
);
1149 // reloc->address is really an offset from the start of the kext
1150 *(kaslr_offsets
+ kaslr_offsets_index
++) = reloc
->address
;
1152 dst
->r_address
= reloc
->address
;
1153 dst
->r_pcrel
= reloc
->pcrel
;
1154 dst
->r_length
= reloc
->length
;
1155 dst
->r_type
= reloc
->reloc_type
;
1156 dst
->r_symbolnum
= 1;
1160 rval
= KERN_FAILURE
;
1166 if (reloc
->pair_target_type
!= KXLD_TARGET_NONE
) {
1168 require_action(i
< relocs
->nitems
, finish
, rval
= KERN_FAILURE
);
1169 scatdst
= (struct scattered_relocation_info
*) dst
;
1170 switch (reloc
->pair_target_type
) {
1171 case KXLD_TARGET_LOOKUP
:
1172 scatdst
->r_address
= reloc
->pair_address
;
1173 scatdst
->r_pcrel
= reloc
->pcrel
;
1174 scatdst
->r_length
= reloc
->length
;
1175 scatdst
->r_type
= relocator
->reloc_get_pair_type(reloc
->reloc_type
);
1176 scatdst
->r_value
= reloc
->pair_target
;
1177 scatdst
->r_scattered
= 1;
1179 case KXLD_TARGET_SECTNUM
:
1180 dst
->r_address
= reloc
->pair_address
;
1181 dst
->r_pcrel
= reloc
->pcrel
;
1182 dst
->r_length
= reloc
->length
;
1183 dst
->r_type
= relocator
->reloc_get_pair_type(reloc
->reloc_type
);
1184 dst
->r_symbolnum
= reloc
->pair_target
+ 1;
1187 case KXLD_TARGET_SYMBOLNUM
:
1188 dst
->r_address
= reloc
->pair_address
;
1189 dst
->r_pcrel
= reloc
->pcrel
;
1190 dst
->r_length
= reloc
->length
;
1191 dst
->r_type
= relocator
->reloc_get_pair_type(reloc
->reloc_type
);
1192 dst
->r_symbolnum
= 1;
1196 rval
= KERN_FAILURE
;
1203 rval
= KERN_SUCCESS
;
1208 #endif /* KXLD_PIC_KEXTS */
1210 #if KXLD_USER_OR_I386
1211 /*******************************************************************************
1212 *******************************************************************************/
1214 generic_reloc_has_pair(u_int _type
)
1216 enum reloc_type_generic type
= _type
;
1218 return type
== GENERIC_RELOC_SECTDIFF
||
1219 type
== GENERIC_RELOC_LOCAL_SECTDIFF
;
1222 /*******************************************************************************
1223 *******************************************************************************/
1225 generic_reloc_get_pair_type(u_int _prev_type __unused
)
1227 return GENERIC_RELOC_PAIR
;
1230 /*******************************************************************************
1231 *******************************************************************************/
1233 generic_reloc_has_got(u_int _type __unused
)
1238 /*******************************************************************************
1239 *******************************************************************************/
1240 static kern_return_t
1241 generic_process_reloc(const KXLDRelocator
*relocator
, u_char
*instruction
,
1242 u_int length
, u_int pcrel
, kxld_addr_t _base_pc
, kxld_addr_t _link_pc
,
1243 kxld_addr_t _link_disp __unused
, u_int _type
, kxld_addr_t _target
,
1244 kxld_addr_t _pair_target
, boolean_t swap __unused
)
1246 kern_return_t rval
= KERN_FAILURE
;
1247 uint32_t base_pc
= (uint32_t) _base_pc
;
1248 uint32_t link_pc
= (uint32_t) _link_pc
;
1249 uint32_t *instr_addr
= NULL
;
1250 uint32_t instr_data
= 0;
1251 uint32_t target
= (uint32_t) _target
;
1252 uint32_t pair_target
= (uint32_t) _pair_target
;
1253 enum reloc_type_generic type
= _type
;
1256 require_action(length
== 2, finish
, rval
= KERN_FAILURE
);
1259 target
= target
+ base_pc
- link_pc
;
1262 instr_addr
= (uint32_t *) ((void *) instruction
);
1263 instr_data
= *instr_addr
;
1267 instr_data
= OSSwapInt32(instr_data
);
1271 rval
= check_for_direct_pure_virtual_call(relocator
, instr_data
);
1272 require_noerr(rval
, finish
);
1275 case GENERIC_RELOC_VANILLA
:
1276 instr_data
+= target
;
1278 case GENERIC_RELOC_SECTDIFF
:
1279 case GENERIC_RELOC_LOCAL_SECTDIFF
:
1280 instr_data
= instr_data
+ target
- pair_target
;
1282 case GENERIC_RELOC_PB_LA_PTR
:
1283 rval
= KERN_FAILURE
;
1285 case GENERIC_RELOC_PAIR
:
1287 rval
= KERN_FAILURE
;
1293 instr_data
= OSSwapInt32(instr_data
);
1297 *instr_addr
= instr_data
;
1299 rval
= KERN_SUCCESS
;
1304 #endif /* KXLD_USER_OR_I386 */
1306 #if KXLD_USER_OR_X86_64
1307 /*******************************************************************************
1308 *******************************************************************************/
1310 x86_64_reloc_has_pair(u_int _type
)
1312 enum reloc_type_x86_64 type
= _type
;
1314 return type
== X86_64_RELOC_SUBTRACTOR
;
1317 /*******************************************************************************
1318 *******************************************************************************/
1320 x86_64_reloc_get_pair_type(u_int _prev_type __unused
)
1322 return X86_64_RELOC_UNSIGNED
;
1325 /*******************************************************************************
1326 *******************************************************************************/
1328 x86_64_reloc_has_got(u_int _type
)
1330 enum reloc_type_x86_64 type
= _type
;
1332 return type
== X86_64_RELOC_GOT_LOAD
|| type
== X86_64_RELOC_GOT
;
1335 /*******************************************************************************
1336 *******************************************************************************/
1337 static kern_return_t
1338 x86_64_process_reloc(const KXLDRelocator
*relocator __unused
, u_char
*instruction
,
1339 u_int length
, u_int pcrel
, kxld_addr_t _base_pc __unused
,
1340 kxld_addr_t _link_pc
, kxld_addr_t _link_disp
, u_int _type
,
1341 kxld_addr_t _target
, kxld_addr_t _pair_target
, boolean_t swap __unused
)
1343 kern_return_t rval
= KERN_FAILURE
;
1344 enum reloc_type_x86_64 type
= _type
;
1345 int32_t *instr32p
= NULL
;
1346 int32_t instr32
= 0;
1347 uint64_t *instr64p
= NULL
;
1348 uint64_t instr64
= 0;
1349 uint64_t target
= _target
;
1350 uint64_t pair_target
= _pair_target
;
1351 uint64_t link_pc
= (uint64_t) _link_pc
;
1352 uint64_t link_disp
= (uint64_t) _link_disp
;
1353 uint64_t adjustment
= 0;
1356 require_action(length
== 2 || length
== 3,
1357 finish
, rval
= KERN_FAILURE
);
1360 instr32p
= (int32_t *) ((void *) instruction
);
1361 instr32
= *instr32p
;
1365 instr32
= OSSwapInt32(instr32
);
1369 rval
= check_for_direct_pure_virtual_call(relocator
, instr32
);
1370 require_noerr(rval
, finish
);
1372 /* There are a number of different small adjustments for pc-relative
1373 * relocation entries. The general case is to subtract the size of the
1374 * relocation (represented by the length parameter), and it applies to
1375 * the GOT types and external SIGNED types. The non-external signed types
1376 * have a different adjustment corresponding to the specific type.
1379 case X86_64_RELOC_SIGNED
:
1385 case X86_64_RELOC_SIGNED_1
:
1391 case X86_64_RELOC_SIGNED_2
:
1397 case X86_64_RELOC_SIGNED_4
:
1403 case X86_64_RELOC_BRANCH
:
1404 case X86_64_RELOC_GOT
:
1405 case X86_64_RELOC_GOT_LOAD
:
1406 adjustment
= (1 << length
);
1412 /* Perform the actual relocation. All of the 32-bit relocations are
1413 * pc-relative except for SUBTRACTOR, so a good chunk of the logic is
1414 * stuck in calculate_displacement_x86_64. The signed relocations are
1415 * a special case, because when they are non-external, the instruction
1416 * already contains the pre-relocation displacement, so we only need to
1417 * find the difference between how far the PC was relocated, and how
1418 * far the target is relocated. Since the target variable already
1419 * contains the difference between the target's base and link
1420 * addresses, we add the difference between the PC's base and link
1421 * addresses to the adjustment variable. This will yield the
1422 * appropriate displacement in calculate_displacement.
1425 case X86_64_RELOC_BRANCH
:
1426 require_action(pcrel
, finish
, rval
= KERN_FAILURE
);
1427 adjustment
+= link_pc
;
1429 case X86_64_RELOC_SIGNED
:
1430 case X86_64_RELOC_SIGNED_1
:
1431 case X86_64_RELOC_SIGNED_2
:
1432 case X86_64_RELOC_SIGNED_4
:
1433 require_action(pcrel
, finish
, rval
= KERN_FAILURE
);
1434 adjustment
+= (pair_target
) ? (link_disp
) : (link_pc
);
1436 case X86_64_RELOC_GOT
:
1437 case X86_64_RELOC_GOT_LOAD
:
1438 require_action(pcrel
, finish
, rval
= KERN_FAILURE
);
1439 adjustment
+= link_pc
;
1440 target
= pair_target
;
1442 case X86_64_RELOC_SUBTRACTOR
:
1443 require_action(!pcrel
, finish
, rval
= KERN_FAILURE
);
1444 instr32
= (int32_t) (target
- pair_target
);
1446 case X86_64_RELOC_UNSIGNED
:
1448 rval
= KERN_FAILURE
;
1452 /* Call calculate_displacement for the pc-relative relocations */
1454 rval
= calculate_displacement_x86_64(target
, adjustment
, &instr32
);
1455 require_noerr(rval
, finish
);
1460 instr32
= OSSwapInt32(instr32
);
1464 *instr32p
= instr32
;
1466 instr64p
= (uint64_t *) ((void *) instruction
);
1467 instr64
= *instr64p
;
1471 instr64
= OSSwapInt64(instr64
);
1475 rval
= check_for_direct_pure_virtual_call(relocator
, (u_long
) instr64
);
1476 require_noerr(rval
, finish
);
1479 case X86_64_RELOC_UNSIGNED
:
1480 require_action(!pcrel
, finish
, rval
= KERN_FAILURE
);
1484 case X86_64_RELOC_SUBTRACTOR
:
1485 require_action(!pcrel
, finish
, rval
= KERN_FAILURE
);
1487 instr64
= target
- pair_target
;
1489 case X86_64_RELOC_SIGNED_1
:
1490 case X86_64_RELOC_SIGNED_2
:
1491 case X86_64_RELOC_SIGNED_4
:
1492 case X86_64_RELOC_GOT_LOAD
:
1493 case X86_64_RELOC_BRANCH
:
1494 case X86_64_RELOC_SIGNED
:
1495 case X86_64_RELOC_GOT
:
1497 rval
= KERN_FAILURE
;
1503 instr64
= OSSwapInt64(instr64
);
1506 *instr64p
= instr64
;
1509 rval
= KERN_SUCCESS
;
1515 /*******************************************************************************
1516 *******************************************************************************/
1517 static kern_return_t
1518 calculate_displacement_x86_64(uint64_t target
, uint64_t adjustment
,
1521 kern_return_t rval
= KERN_FAILURE
;
1522 int64_t displacement
;
1523 uint64_t difference
;
1525 displacement
= *instr32
+ target
- adjustment
;
1526 difference
= ABSOLUTE_VALUE(displacement
);
1527 require_action(difference
< X86_64_RIP_RELATIVE_LIMIT
, finish
,
1528 rval
= KERN_FAILURE
;
1529 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogRelocationOverflow
));
1531 *instr32
= (int32_t) displacement
;
1532 rval
= KERN_SUCCESS
;
1537 #endif /* KXLD_USER_OR_X86_64 */
1539 #if KXLD_USER_OR_ARM
1540 /*******************************************************************************
1541 *******************************************************************************/
1543 arm_reloc_has_pair(u_int _type
)
1545 enum reloc_type_arm type
= _type
;
1548 case ARM_RELOC_SECTDIFF
:
1556 /*******************************************************************************
1557 *******************************************************************************/
1559 arm_reloc_get_pair_type(u_int _prev_type __unused
)
1561 return ARM_RELOC_PAIR
;
1564 /*******************************************************************************
1565 *******************************************************************************/
1567 arm_reloc_has_got(u_int _type __unused
)
1572 /*******************************************************************************
1573 *******************************************************************************/
1574 static kern_return_t
1575 arm_process_reloc(const KXLDRelocator
*relocator __unused
, u_char
*instruction
,
1576 u_int length
, u_int pcrel
, kxld_addr_t _base_pc __unused
,
1577 kxld_addr_t _link_pc __unused
, kxld_addr_t _link_disp __unused
,
1578 u_int _type __unused
, kxld_addr_t _target __unused
,
1579 kxld_addr_t _pair_target __unused
, boolean_t swap __unused
)
1581 kern_return_t rval
= KERN_FAILURE
;
1582 uint32_t *instr_addr
= NULL
;
1583 uint32_t instr_data
= 0;
1584 uint32_t base_pc
= (uint32_t) _base_pc
;
1585 uint32_t link_pc
= (uint32_t) _link_pc
;
1586 uint32_t target
= (uint32_t) _target
;
1587 int32_t displacement
= 0;
1588 enum reloc_type_arm type
= _type
;
1591 require_action(length
== 2, finish
, rval
= KERN_FAILURE
);
1594 displacement
= target
+ base_pc
- link_pc
;
1597 instr_addr
= (uint32_t *) ((void *) instruction
);
1598 instr_data
= *instr_addr
;
1602 instr_data
= OSSwapInt32(instr_data
);
1606 rval
= check_for_direct_pure_virtual_call(relocator
, instr_data
);
1607 require_noerr(rval
, finish
);
1610 case ARM_RELOC_VANILLA
:
1611 instr_data
+= target
;
1615 * If the displacement is 0 (the offset between the pc and the target has
1616 * not changed), then we don't need to do anything for BR24 and BR22
1617 * relocs. As it turns out, because kexts build with -mlong-calls all
1618 * relocations currently end up being either vanilla (handled above) or
1619 * BR22/BR24 with a displacement of 0.
1620 * We could handle other displacements here but to keep things simple, we
1621 * won't until it is needed (at which point the kernelcache will fail to
1624 case ARM_RELOC_BR24
:
1625 require_action(pcrel
, finish
, rval
= KERN_FAILURE
);
1626 require_action(displacement
== 0, finish
, rval
= KERN_FAILURE
);
1628 case ARM_THUMB_RELOC_BR22
:
1629 require_action(pcrel
, finish
, rval
= KERN_FAILURE
);
1630 require_action(displacement
== 0, finish
, rval
= KERN_FAILURE
);
1633 case ARM_RELOC_SECTDIFF
:
1634 case ARM_RELOC_LOCAL_SECTDIFF
:
1635 case ARM_RELOC_PB_LA_PTR
:
1636 rval
= KERN_FAILURE
;
1639 case ARM_RELOC_PAIR
:
1641 rval
= KERN_FAILURE
;
1647 instr_data
= OSSwapInt32(instr_data
);
1651 *instr_addr
= instr_data
;
1653 rval
= KERN_SUCCESS
;
1659 #endif /* KXLD_USER_OR_ARM */
1661 #if KXLD_USER_OR_ARM64
1662 /*******************************************************************************
1663 *******************************************************************************/
1665 arm64_reloc_has_pair(u_int _type
)
1667 return _type
== ARM64_RELOC_SUBTRACTOR
;
1670 /*******************************************************************************
1671 *******************************************************************************/
1673 arm64_reloc_get_pair_type(u_int _prev_type __unused
)
1675 if (_prev_type
== ARM64_RELOC_SUBTRACTOR
) {
1676 return ARM64_RELOC_UNSIGNED
;
1682 /*******************************************************************************
1683 *******************************************************************************/
1685 arm64_reloc_has_got(u_int _type
)
1687 return _type
== ARM64_RELOC_GOT_LOAD_PAGE21
||
1688 _type
== ARM64_RELOC_GOT_LOAD_PAGEOFF12
;
1691 /*******************************************************************************
1692 *******************************************************************************/
1694 arm64_process_reloc(const KXLDRelocator
*relocator __unused
, u_char
*instruction
,
1695 u_int length
, u_int pcrel
, kxld_addr_t _base_pc __unused
, kxld_addr_t _link_pc
,
1696 kxld_addr_t _link_disp __unused
, u_int _type
, kxld_addr_t _target
,
1697 kxld_addr_t _pair_target __unused
, boolean_t swap
)
1699 kern_return_t rval
= KERN_FAILURE
;
1700 enum reloc_type_arm64 type
= _type
;
1701 uint64_t target
= _target
;
1702 uint64_t link_pc
= (uint64_t) _link_pc
;
1703 uint64_t difference
= 0;
1704 int64_t displacement
= 0;
1705 uint32_t addend
= 0;
1708 require_action((length
== 2 || length
== 3), finish
, rval
= KERN_FAILURE
);
1711 uint32_t *instr32p
= (uint32_t *) (void *) instruction
;
1712 uint32_t instr32
= *instr32p
;
1716 instr32
= OSSwapInt32(instr32
);
1721 case ARM64_RELOC_BRANCH26
:
1722 require_action(pcrel
, finish
, rval
= KERN_FAILURE
);
1723 addend
= (instr32
& 0x03FFFFFF) << 2;
1724 addend
= SIGN_EXTEND(addend
, 27);
1725 displacement
= (target
- link_pc
+ addend
);
1726 difference
= ABSOLUTE_VALUE(displacement
);
1727 displacement
= (displacement
>> 2);
1728 require_action(difference
< (128 * 1024 * 1024), finish
,
1729 rval
= KERN_FAILURE
;
1730 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogRelocationOverflow
));
1731 instr32
= (instr32
& 0xFC000000) | (displacement
& 0x03FFFFFF);
1735 rval
= KERN_FAILURE
;
1741 instr32
= OSSwapInt32(instr32
);
1745 *instr32p
= instr32
;
1746 } else { /* length == 3 */
1747 uint64_t *instr64p
= (uint64_t *) (void *) instruction
;
1748 uint64_t instr64
= *instr64p
;
1752 instr64
= OSSwapInt64(instr64
);
1757 case ARM64_RELOC_UNSIGNED
:
1758 require_action(!pcrel
, finish
, rval
= KERN_FAILURE
);
1762 rval
= KERN_FAILURE
;
1768 instr64
= OSSwapInt64(instr64
);
1772 *instr64p
= instr64
;
1775 rval
= KERN_SUCCESS
;
1781 #endif /* KXLD_USER_OR_ARM64 */