Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Thumb-2: Add support for loadable modules

Modules compiled to Thumb-2 have two additional relocations needing to
be resolved at load time, R_ARM_THM_CALL and R_ARM_THM_JUMP24, for BL
and B.W instructions. The maximum Thumb-2 addressing range is +/-2^24
(+/-16MB) therefore the MODULES_VADDR macro in asm/memory.h is set to
(MODULES_END - 8MB) for the Thumb-2 compiled kernel.

Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>

+62
+3
arch/arm/include/asm/elf.h
··· 55 55 #define R_ARM_MOVW_ABS_NC 43 56 56 #define R_ARM_MOVT_ABS 44 57 57 58 + #define R_ARM_THM_CALL 10 59 + #define R_ARM_THM_JUMP24 30 60 + 58 61 /* 59 62 * These are used to set parameters in the core dumps. 60 63 */
+6
arch/arm/include/asm/memory.h
··· 44 44 * The module space lives between the addresses given by TASK_SIZE 45 45 * and PAGE_OFFSET - it must be within 32MB of the kernel text. 46 46 */ 47 + #ifndef CONFIG_THUMB2_KERNEL 47 48 #define MODULES_VADDR (PAGE_OFFSET - 16*1024*1024) 49 + #else 50 + /* smaller range for Thumb-2 symbols relocation (2^24)*/ 51 + #define MODULES_VADDR (PAGE_OFFSET - 8*1024*1024) 52 + #endif 53 + 48 54 #if TASK_SIZE > MODULES_VADDR 49 55 #error Top of user space clashes with start of module space 50 56 #endif
+53
arch/arm/kernel/module.c
··· 102 102 unsigned long loc; 103 103 Elf32_Sym *sym; 104 104 s32 offset; 105 + u32 upper, lower, sign, j1, j2; 105 106 106 107 offset = ELF32_R_SYM(rel->r_info); 107 108 if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) { ··· 183 182 *(u32 *)loc &= 0xfff0f000; 184 183 *(u32 *)loc |= ((offset & 0xf000) << 4) | 185 184 (offset & 0x0fff); 185 + break; 186 + 187 + case R_ARM_THM_CALL: 188 + case R_ARM_THM_JUMP24: 189 + upper = *(u16 *)loc; 190 + lower = *(u16 *)(loc + 2); 191 + 192 + /* 193 + * 25 bit signed address range (Thumb-2 BL and B.W 194 + * instructions): 195 + * S:I1:I2:imm10:imm11:0 196 + * where: 197 + * S = upper[10] = offset[24] 198 + * I1 = ~(J1 ^ S) = offset[23] 199 + * I2 = ~(J2 ^ S) = offset[22] 200 + * imm10 = upper[9:0] = offset[21:12] 201 + * imm11 = lower[10:0] = offset[11:1] 202 + * J1 = lower[13] 203 + * J2 = lower[11] 204 + */ 205 + sign = (upper >> 10) & 1; 206 + j1 = (lower >> 13) & 1; 207 + j2 = (lower >> 11) & 1; 208 + offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) | 209 + ((~(j2 ^ sign) & 1) << 22) | 210 + ((upper & 0x03ff) << 12) | 211 + ((lower & 0x07ff) << 1); 212 + if (offset & 0x01000000) 213 + offset -= 0x02000000; 214 + offset += sym->st_value - loc; 215 + 216 + /* only Thumb addresses allowed (no interworking) */ 217 + if (!(offset & 1) || 218 + offset <= (s32)0xff000000 || 219 + offset >= (s32)0x01000000) { 220 + printk(KERN_ERR 221 + "%s: relocation out of range, section " 222 + "%d reloc %d sym '%s'\n", module->name, 223 + relindex, i, strtab + sym->st_name); 224 + return -ENOEXEC; 225 + } 226 + 227 + sign = (offset >> 24) & 1; 228 + j1 = sign ^ (~(offset >> 23) & 1); 229 + j2 = sign ^ (~(offset >> 22) & 1); 230 + *(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) | 231 + ((offset >> 12) & 0x03ff)); 232 + *(u16 *)(loc + 2) = (u16)((lower & 0xd000) | 233 + (j1 << 13) | (j2 << 11) | 234 + ((offset >> 1) & 0x07ff)); 235 + upper = *(u16 *)loc; 236 + lower = *(u16 *)(loc + 2); 186 237 break; 187 238 188 239 default: