5 typedef unsigned long long uint64
;
7 //SHIFT must be 16KB / 2MB align according to KALSR config
9 #define SHIFT(idx) ((uint64)(idx) * 0x4000)
11 #ifndef R_AARCH64_RELATIVE
12 #define R_AARCH64_RELATIVE 1027
14 #ifndef R_AARCH64_ABS64
15 #define R_AARCH64_ABS64 257
19 * The rela section uses the VA
20 * the VA to file offset is:
23 int patch_rela(char *, uint64
, uint64
, uint64
, uint64
, uint64
, uint64
, uint64
, uint64
);
24 int main(int argc
, char *argv
[]){
25 uint64 rs_offset
= 0, re_offset
= 0, ds_offset
= 0;
31 printf ("\nUsage : \n");
32 printf ("kaslr_fips vmlinux_file reloc_start_addr reloc_end_addr dynsym_addr index first_crypto_rodata last_crypto_rodata first_fmp_rodata last_fmp_rodata");
38 rs_offset
= atol(argv
[2]);
39 re_offset
= atol(argv
[3]);
40 ds_offset
= atol(argv
[4]);
41 index
= atoi(argv
[5]);
42 va_to_file
= atol(argv
[10]);
44 if( !file
|| !rs_offset
|| !re_offset
|| !ds_offset
)
46 printf ("kaslr_fips vmlinux_file reloc_start_addr reloc_end_addr dynsym_add\n");
47 printf ("kaslr_fips index %d\n", index
);
51 // printf("----- start patching %s with reloc_s %llx reloc_e %llx dynsym_s %llx index %d----\n", file, rs_offset, re_offset, ds_offset, index);
52 ret
= patch_rela(file
, rs_offset
, re_offset
, ds_offset
, SHIFT(index
), atol(argv
[6]), atol(argv
[7]), atol(argv
[8]), atol(argv
[9]));
54 // printf("----- end patching %s -----\n", file);
59 * rela_start: relocation section start in the vmlinux
64 int patch_rela(char *file
, uint64 rela_start
, uint64 rela_end
, uint64 dynsym_start
, uint64 offset
, uint64 first_crypto_rodata
, uint64 last_crypto_rodata
, uint64 first_fmp_rodata
, uint64 last_fmp_rodata
){
65 uint64 rs_offset
= rela_start
- va_to_file
;
66 uint64 re_offset
= rela_end
- va_to_file
;
67 uint64 ds_offset
= dynsym_start
- va_to_file
;
71 fp
= fopen(file
, "r+");
73 printf ("Unable to open file : %s", file
);
77 Elf64_Rela rela_entry
;
79 uint64 addr
= 0, value
= 0;
81 for (; rs_offset
< re_offset
; rs_offset
+= sizeof(Elf64_Rela
)){
82 //seek and read the rela entry
83 if(0 != fseek(fp
, rs_offset
, SEEK_SET
)){
88 read_size
= fread((void*) &rela_entry
, sizeof(rela_entry
), 1, fp
);
89 if(0 == read_size
) continue;
90 /*printf("%llx, %llx\n", ELF64_R_TYPE(rela_entry.r_info), R_AARCH64_RELATIVE);*/
91 addr
= rela_entry
.r_offset
;
92 if (0x0 == addr
) continue;
94 if ( !((addr
>= first_crypto_rodata
&& addr
<= last_crypto_rodata
) ||
95 (addr
>= first_fmp_rodata
&& addr
<= last_fmp_rodata
)))
98 if (ELF64_R_TYPE(rela_entry
.r_info
) == R_AARCH64_RELATIVE
) {
99 value
= offset
+ rela_entry
.r_addend
;
101 } else if(ELF64_R_TYPE(rela_entry
.r_info
) == R_AARCH64_ABS64
) {
102 uint64 sym_index
= ELF64_R_SYM(rela_entry
.r_info
);
103 uint64 sym_offset
= ds_offset
+ sym_index
* (sizeof(Elf64_Sym
));
105 //seek to the start of the symbol table entry
106 if (0 !=fseek(fp
, sym_offset
, SEEK_SET
)){
110 read_size
= fread((void*) &sym_entry
, sizeof(sym_entry
), 1, fp
);
111 if(0 == read_size
) continue;
113 value
= sym_entry
.st_value
+ rela_entry
.r_addend
+ offset
;
115 // printf("Try to patch none supported type %llx\n", (uint64)ELF64_R_TYPE(rela_entry.r_info));
117 /*printf("%llx, %llx, %llx, %llx, %llx\n", (uint64) rela_entry.r_offset, */
118 /*(uint64)rela_entry.r_info, */
119 /*(uint64)rela_entry.r_addend, */
120 /*addr - VA_TO_FILE, value);*/
121 if (0 != fseek(fp
, addr
- va_to_file
, SEEK_SET
)){
126 if (fwrite((const void *) &value
, sizeof(uint64
), 1, fp
) != 1){