Skip to content

Commit 3310bae

Browse files
committed
modpost: fix section_mismatch message for R_ARM_THM_{CALL,JUMP24,JUMP19}
addend_arm_rel() processes R_ARM_THM_CALL, R_ARM_THM_JUMP24, R_ARM_THM_JUMP19 in a wrong way. Here, test code. [test code for R_ARM_THM_JUMP24]   .section .init.text,"ax"   bar:           bx      lr   .section .text,"ax"   .globl foo   foo:           b       bar [test code for R_ARM_THM_CALL]   .section .init.text,"ax"   bar:           bx      lr   .section .text,"ax"   .globl foo   foo:           push    {lr}           bl      bar           pop     {pc} If you compile it with CONFIG_THUMB2_KERNEL=y, modpost will show the symbol name, (unknown).   WARNING: modpost: vmlinux.o: section mismatch in reference: foo (section: .text) -> (unknown) (section: .init.text) (You need to use GNU linker instead of LLD to reproduce it.) Fix the code to make modpost show the correct symbol name. I checked arch/arm/kernel/module.c to learn the encoding of R_ARM_THM_CALL and R_ARM_THM_JUMP24. The module does not support R_ARM_THM_JUMP19, but I checked its encoding in ARM ARM. The '+4' is the compensation for pc-relative instruction. It is documented in "ELF for the Arm Architecture" [1].   "If the relocation is pc-relative then compensation for the PC bias   (the PC value is 8 bytes ahead of the executing instruction in Arm   state and 4 bytes in Thumb state) must be encoded in the relocation   by the object producer." [1]: https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst Fixes: c9698e5 ("ARM: 7964/1: Detect section mismatches in thumb relocations") Signed-off-by: Masahiro Yamada <[email protected]>
1 parent cd1824f commit 3310bae

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

scripts/mod/modpost.c

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
12761276
unsigned int r_typ = ELF_R_TYPE(r->r_info);
12771277
Elf_Sym *sym = elf->symtab_start + ELF_R_SYM(r->r_info);
12781278
void *loc = reloc_location(elf, sechdr, r);
1279-
uint32_t inst, upper, lower;
1279+
uint32_t inst, upper, lower, sign, j1, j2;
12801280
int32_t offset;
12811281

12821282
switch (r_typ) {
@@ -1309,13 +1309,54 @@ static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
13091309
15);
13101310
r->r_addend = offset + sym->st_value;
13111311
break;
1312+
case R_ARM_THM_JUMP19:
1313+
/*
1314+
* Encoding T3:
1315+
* S = upper[10]
1316+
* imm6 = upper[5:0]
1317+
* J1 = lower[13]
1318+
* J2 = lower[11]
1319+
* imm11 = lower[10:0]
1320+
* imm32 = SignExtend(S:J2:J1:imm6:imm11:'0')
1321+
*/
1322+
upper = TO_NATIVE(*(uint16_t *)loc);
1323+
lower = TO_NATIVE(*((uint16_t *)loc + 1));
1324+
1325+
sign = (upper >> 10) & 1;
1326+
j1 = (lower >> 13) & 1;
1327+
j2 = (lower >> 11) & 1;
1328+
offset = sign_extend32((sign << 20) | (j2 << 19) | (j1 << 18) |
1329+
((upper & 0x03f) << 12) |
1330+
((lower & 0x07ff) << 1),
1331+
20);
1332+
r->r_addend = offset + sym->st_value + 4;
1333+
break;
13121334
case R_ARM_THM_CALL:
13131335
case R_ARM_THM_JUMP24:
1314-
case R_ARM_THM_JUMP19:
1315-
/* From ARM ABI: ((S + A) | T) - P */
1316-
r->r_addend = (int)(long)(elf->hdr +
1317-
sechdr->sh_offset +
1318-
(r->r_offset - sechdr->sh_addr));
1336+
/*
1337+
* Encoding T4:
1338+
* S = upper[10]
1339+
* imm10 = upper[9:0]
1340+
* J1 = lower[13]
1341+
* J2 = lower[11]
1342+
* imm11 = lower[10:0]
1343+
* I1 = NOT(J1 XOR S)
1344+
* I2 = NOT(J2 XOR S)
1345+
* imm32 = SignExtend(S:I1:I2:imm10:imm11:'0')
1346+
*/
1347+
upper = TO_NATIVE(*(uint16_t *)loc);
1348+
lower = TO_NATIVE(*((uint16_t *)loc + 1));
1349+
1350+
sign = (upper >> 10) & 1;
1351+
j1 = (lower >> 13) & 1;
1352+
j2 = (lower >> 11) & 1;
1353+
offset = sign_extend32((sign << 24) |
1354+
((~(j1 ^ sign) & 1) << 23) |
1355+
((~(j2 ^ sign) & 1) << 22) |
1356+
((upper & 0x03ff) << 12) |
1357+
((lower & 0x07ff) << 1),
1358+
24);
1359+
r->r_addend = offset + sym->st_value + 4;
13191360
break;
13201361
default:
13211362
return 1;

0 commit comments

Comments
 (0)