From a6b98ccbbe8b1aa7e3da4eefacca5987ad05b03e Mon Sep 17 00:00:00 2001 From: David Coles Date: Sun, 25 Sep 2022 14:06:46 -0700 Subject: [PATCH] Preliminary Rust support (#1781) * Add support for R_ARM_THM_MOVW_ABS_NC/THM_MOVT_ABS These are sometimes emitted by the Rust LLVM compiler. Ref: https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst#56relocation * Discard LLVM bitcode from extension applications LLVM-based compilers may include uncompressed bitcode in object files to help with link-time optimization. However this can bloat binary sizes from KB to MB. * Expose alligned_malloc/free functions to applications This is required to implement a global allocator in Rust. --- firmware/targets/f7/api_symbols.csv | 4 +-- firmware/targets/f7/application-ext.ld | 2 ++ lib/flipper_application/elf/elf.h | 2 ++ lib/flipper_application/elf/elf_file.c | 41 ++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv index 39365c39..11b719dd 100644 --- a/firmware/targets/f7/api_symbols.csv +++ b/firmware/targets/f7/api_symbols.csv @@ -433,8 +433,8 @@ Function,-,acoshl,long double,long double Function,-,acosl,long double,long double Function,+,acquire_mutex,void*,"ValueMutex*, uint32_t" Function,-,aligned_alloc,void*,"size_t, size_t" -Function,-,aligned_free,void,void* -Function,-,aligned_malloc,void*,"size_t, size_t" +Function,+,aligned_free,void,void* +Function,+,aligned_malloc,void*,"size_t, size_t" Function,-,arc4random,__uint32_t, Function,-,arc4random_buf,void,"void*, size_t" Function,-,arc4random_uniform,__uint32_t,__uint32_t diff --git a/firmware/targets/f7/application-ext.ld b/firmware/targets/f7/application-ext.ld index 8f79675b..01bb021b 100644 --- a/firmware/targets/f7/application-ext.ld +++ b/firmware/targets/f7/application-ext.ld @@ -48,5 +48,7 @@ SECTIONS { *(.comment) *(.comment.*) + *(.llvmbc) + *(.llvmcmd) } } diff --git a/lib/flipper_application/elf/elf.h b/lib/flipper_application/elf/elf.h index f1697ba4..a36622b5 100644 --- a/lib/flipper_application/elf/elf.h +++ b/lib/flipper_application/elf/elf.h @@ -1116,6 +1116,8 @@ typedef struct { #define R_ARM_LDR_SBREL_11_0 35 #define R_ARM_ALU_SBREL_19_12 36 #define R_ARM_ALU_SBREL_27_20 37 +#define R_ARM_THM_MOVW_ABS_NC 47 /* Direct 16 bit (Thumb32 MOVW) */ +#define R_ARM_THM_MOVT_ABS 48 /* Direct high 16 bit */ #define R_ARM_GNU_VTENTRY 100 #define R_ARM_GNU_VTINHERIT 101 #define R_ARM_THM_PC11 102 /* thumb unconditional branch */ diff --git a/lib/flipper_application/elf/elf_file.c b/lib/flipper_application/elf/elf_file.c index 202d0a87..e2616549 100644 --- a/lib/flipper_application/elf/elf_file.c +++ b/lib/flipper_application/elf/elf_file.c @@ -255,6 +255,42 @@ static void elf_relocate_jmp_call(ELFFile* elf, Elf32_Addr relAddr, int type, El (uint16_t)((lo & 0xc000) | (j1 << 13) | blx_bit | (j2 << 11) | imm11); } +static void elf_relocate_mov(Elf32_Addr relAddr, int type, Elf32_Addr symAddr) { + uint16_t upper_insn = ((uint16_t*)relAddr)[0]; + uint16_t lower_insn = ((uint16_t*)relAddr)[1]; + + /* MOV* ,# + * + * i = upper[10] + * imm4 = upper[3:0] + * imm3 = lower[14:12] + * imm8 = lower[7:0] + * + * imm16 = imm4:i:imm3:imm8 + */ + uint32_t i = (upper_insn >> 10) & 1; /* upper[10] */ + uint32_t imm4 = upper_insn & 0x000F; /* upper[3:0] */ + uint32_t imm3 = (lower_insn >> 12) & 0x7; /* lower[14:12] */ + uint32_t imm8 = lower_insn & 0x00FF; /* lower[7:0] */ + + int32_t addend = (imm4 << 12) | (i << 11) | (imm3 << 8) | imm8; /* imm16 */ + + uint32_t addr = (symAddr + addend); + if (type == R_ARM_THM_MOVT_ABS) { + addr >>= 16; /* upper 16 bits */ + } else { + addr &= 0x0000FFFF; /* lower 16 bits */ + } + + /* Re-encode */ + ((uint16_t*)relAddr)[0] = (upper_insn & 0xFBF0) + | (((addr >> 11) & 1) << 10) /* i */ + | ((addr >> 12) & 0x000F); /* imm4 */ + ((uint16_t*)relAddr)[1] = (lower_insn & 0x8F00) + | (((addr >> 8) & 0x7) << 12) /* imm3 */ + | (addr & 0x00FF); /* imm8 */ +} + static bool elf_relocate_symbol(ELFFile* elf, Elf32_Addr relAddr, int type, Elf32_Addr symAddr) { switch(type) { case R_ARM_TARGET1: @@ -268,6 +304,11 @@ static bool elf_relocate_symbol(ELFFile* elf, Elf32_Addr relAddr, int type, Elf3 FURI_LOG_D( TAG, " R_ARM_THM_CALL/JMP relocated is 0x%08X", (unsigned int)*((uint32_t*)relAddr)); break; + case R_ARM_THM_MOVW_ABS_NC: + case R_ARM_THM_MOVT_ABS: + elf_relocate_mov(relAddr, type, symAddr); + FURI_LOG_D(TAG, " R_ARM_THM_MOVW_ABS_NC/MOVT_ABS relocated is 0x%08X", (unsigned int)*((uint32_t*)relAddr)); + break; default: FURI_LOG_E(TAG, " Undefined relocation %d", type); return false;