Skip to content

Commit adca6dc

Browse files
committed
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>
1 parent 0e056f2 commit adca6dc

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎arch/arm/include/asm/elf.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ typedef struct user_fp elf_fpregset_t;
5555
#define R_ARM_MOVW_ABS_NC 43
5656
#define R_ARM_MOVT_ABS 44
5757

58+
#define R_ARM_THM_CALL 10
59+
#define R_ARM_THM_JUMP24 30
60+
5861
/*
5962
* These are used to set parameters in the core dumps.
6063
*/

‎arch/arm/include/asm/memory.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@
4444
* The module space lives between the addresses given by TASK_SIZE
4545
* and PAGE_OFFSET - it must be within 32MB of the kernel text.
4646
*/
47+
#ifndef CONFIG_THUMB2_KERNEL
4748
#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+
4854
#if TASK_SIZE > MODULES_VADDR
4955
#error Top of user space clashes with start of module space
5056
#endif

‎arch/arm/kernel/module.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
102102
unsigned long loc;
103103
Elf32_Sym *sym;
104104
s32 offset;
105+
u32 upper, lower, sign, j1, j2;
105106

106107
offset = ELF32_R_SYM(rel->r_info);
107108
if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
@@ -184,6 +185,58 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
184185
(offset & 0x0fff);
185186
break;
186187

188+
case R_ARM_THM_CALL:
189+
case R_ARM_THM_JUMP24:
190+
upper = *(u16 *)loc;
191+
lower = *(u16 *)(loc + 2);
192+
193+
/*
194+
* 25 bit signed address range (Thumb-2 BL and B.W
195+
* instructions):
196+
* S:I1:I2:imm10:imm11:0
197+
* where:
198+
* S = upper[10] = offset[24]
199+
* I1 = ~(J1 ^ S) = offset[23]
200+
* I2 = ~(J2 ^ S) = offset[22]
201+
* imm10 = upper[9:0] = offset[21:12]
202+
* imm11 = lower[10:0] = offset[11:1]
203+
* J1 = lower[13]
204+
* J2 = lower[11]
205+
*/
206+
sign = (upper >> 10) & 1;
207+
j1 = (lower >> 13) & 1;
208+
j2 = (lower >> 11) & 1;
209+
offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
210+
((~(j2 ^ sign) & 1) << 22) |
211+
((upper & 0x03ff) << 12) |
212+
((lower & 0x07ff) << 1);
213+
if (offset & 0x01000000)
214+
offset -= 0x02000000;
215+
offset += sym->st_value - loc;
216+
217+
/* only Thumb addresses allowed (no interworking) */
218+
if (!(offset & 1) ||
219+
offset <= (s32)0xff000000 ||
220+
offset >= (s32)0x01000000) {
221+
printk(KERN_ERR
222+
"%s: relocation out of range, section "
223+
"%d reloc %d sym '%s'\n", module->name,
224+
relindex, i, strtab + sym->st_name);
225+
return -ENOEXEC;
226+
}
227+
228+
sign = (offset >> 24) & 1;
229+
j1 = sign ^ (~(offset >> 23) & 1);
230+
j2 = sign ^ (~(offset >> 22) & 1);
231+
*(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) |
232+
((offset >> 12) & 0x03ff));
233+
*(u16 *)(loc + 2) = (u16)((lower & 0xd000) |
234+
(j1 << 13) | (j2 << 11) |
235+
((offset >> 1) & 0x07ff));
236+
upper = *(u16 *)loc;
237+
lower = *(u16 *)(loc + 2);
238+
break;
239+
187240
default:
188241
printk(KERN_ERR "%s: unknown relocation: %u\n",
189242
module->name, ELF32_R_TYPE(rel->r_info));

0 commit comments

Comments
 (0)