xref: /linux-6.15/arch/arm/kernel/module.c (revision 9b69b52c)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/arch/arm/kernel/module.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2002 Russell King.
66a570b28SHyok S. Choi  *  Modified for nommu by Hyok S. Choi
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Module allocation method suggested by Andi Kleen.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds #include <linux/module.h>
11f339ab3dSRussell King #include <linux/moduleloader.h>
121da177e4SLinus Torvalds #include <linux/kernel.h>
1327ac792cSAndrea Righi #include <linux/mm.h>
141da177e4SLinus Torvalds #include <linux/elf.h>
151da177e4SLinus Torvalds #include <linux/fs.h>
161da177e4SLinus Torvalds #include <linux/string.h>
171da177e4SLinus Torvalds 
1837efe642SRussell King #include <asm/sections.h>
194a9cb360SRussell King #include <asm/smp_plat.h>
202e1926e7SCatalin Marinas #include <asm/unwind.h>
21f592d323SBen Dooks #include <asm/opcodes.h>
221da177e4SLinus Torvalds 
module_init_section(const char * name)23cdcb07e4SVincent Whitchurch bool module_init_section(const char *name)
24cdcb07e4SVincent Whitchurch {
25cdcb07e4SVincent Whitchurch 	return strstarts(name, ".init") ||
26cdcb07e4SVincent Whitchurch 		strstarts(name, ".ARM.extab.init") ||
27cdcb07e4SVincent Whitchurch 		strstarts(name, ".ARM.exidx.init");
28cdcb07e4SVincent Whitchurch }
29cdcb07e4SVincent Whitchurch 
module_exit_section(const char * name)3070bac08dSMatthias Schiffer bool module_exit_section(const char *name)
3170bac08dSMatthias Schiffer {
3270bac08dSMatthias Schiffer 	return strstarts(name, ".exit") ||
3370bac08dSMatthias Schiffer 		strstarts(name, ".ARM.extab.exit") ||
3470bac08dSMatthias Schiffer 		strstarts(name, ".ARM.exidx.exit");
3570bac08dSMatthias Schiffer }
3670bac08dSMatthias Schiffer 
37d6905849SArd Biesheuvel #ifdef CONFIG_ARM_HAS_GROUP_RELOCS
381fa8c4b1SArd Biesheuvel /*
391fa8c4b1SArd Biesheuvel  * This implements the partitioning algorithm for group relocations as
401fa8c4b1SArd Biesheuvel  * documented in the ARM AArch32 ELF psABI (IHI 0044).
411fa8c4b1SArd Biesheuvel  *
421fa8c4b1SArd Biesheuvel  * A single PC-relative symbol reference is divided in up to 3 add or subtract
431fa8c4b1SArd Biesheuvel  * operations, where the final one could be incorporated into a load/store
441fa8c4b1SArd Biesheuvel  * instruction with immediate offset. E.g.,
451fa8c4b1SArd Biesheuvel  *
461fa8c4b1SArd Biesheuvel  *   ADD	Rd, PC, #...		or	ADD	Rd, PC, #...
471fa8c4b1SArd Biesheuvel  *   ADD	Rd, Rd, #...			ADD	Rd, Rd, #...
481fa8c4b1SArd Biesheuvel  *   LDR	Rd, [Rd, #...]			ADD	Rd, Rd, #...
491fa8c4b1SArd Biesheuvel  *
501fa8c4b1SArd Biesheuvel  * The latter has a guaranteed range of only 16 MiB (3x8 == 24 bits), so it is
511fa8c4b1SArd Biesheuvel  * of limited use in the kernel. However, the ADD/ADD/LDR combo has a range of
521fa8c4b1SArd Biesheuvel  * -/+ 256 MiB, (2x8 + 12 == 28 bits), which means it has sufficient range for
531fa8c4b1SArd Biesheuvel  * any in-kernel symbol reference (unless module PLTs are being used).
541fa8c4b1SArd Biesheuvel  *
551fa8c4b1SArd Biesheuvel  * The main advantage of this approach over the typical pattern using a literal
561fa8c4b1SArd Biesheuvel  * load is that literal loads may miss in the D-cache, and generally lead to
571fa8c4b1SArd Biesheuvel  * lower cache efficiency for variables that are referenced often from many
581fa8c4b1SArd Biesheuvel  * different places in the code.
591fa8c4b1SArd Biesheuvel  */
get_group_rem(u32 group,u32 * offset)601fa8c4b1SArd Biesheuvel static u32 get_group_rem(u32 group, u32 *offset)
611fa8c4b1SArd Biesheuvel {
621fa8c4b1SArd Biesheuvel 	u32 val = *offset;
631fa8c4b1SArd Biesheuvel 	u32 shift;
641fa8c4b1SArd Biesheuvel 	do {
651fa8c4b1SArd Biesheuvel 		shift = val ? (31 - __fls(val)) & ~1 : 32;
661fa8c4b1SArd Biesheuvel 		*offset = val;
671fa8c4b1SArd Biesheuvel 		if (!val)
681fa8c4b1SArd Biesheuvel 			break;
691fa8c4b1SArd Biesheuvel 		val &= 0xffffff >> shift;
701fa8c4b1SArd Biesheuvel 	} while (group--);
711fa8c4b1SArd Biesheuvel 	return shift;
721fa8c4b1SArd Biesheuvel }
73d6905849SArd Biesheuvel #endif
741fa8c4b1SArd Biesheuvel 
751da177e4SLinus Torvalds int
apply_relocate(Elf32_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relindex,struct module * module)761da177e4SLinus Torvalds apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
771da177e4SLinus Torvalds 	       unsigned int relindex, struct module *module)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	Elf32_Shdr *symsec = sechdrs + symindex;
801da177e4SLinus Torvalds 	Elf32_Shdr *relsec = sechdrs + relindex;
811da177e4SLinus Torvalds 	Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
821da177e4SLinus Torvalds 	Elf32_Rel *rel = (void *)relsec->sh_addr;
831da177e4SLinus Torvalds 	unsigned int i;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
861da177e4SLinus Torvalds 		unsigned long loc;
871da177e4SLinus Torvalds 		Elf32_Sym *sym;
8868e6fad4SRussell King 		const char *symname;
89d6905849SArd Biesheuvel #ifdef CONFIG_ARM_HAS_GROUP_RELOCS
901fa8c4b1SArd Biesheuvel 		u32 shift, group = 1;
91d6905849SArd Biesheuvel #endif
921da177e4SLinus Torvalds 		s32 offset;
93f592d323SBen Dooks 		u32 tmp;
94b7493156SCatalin Marinas #ifdef CONFIG_THUMB2_KERNEL
95adca6dc2SCatalin Marinas 		u32 upper, lower, sign, j1, j2;
96b7493156SCatalin Marinas #endif
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 		offset = ELF32_R_SYM(rel->r_info);
991da177e4SLinus Torvalds 		if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
10068e6fad4SRussell King 			pr_err("%s: section %u reloc %u: bad relocation sym offset\n",
1011da177e4SLinus Torvalds 				module->name, relindex, i);
1021da177e4SLinus Torvalds 			return -ENOEXEC;
1031da177e4SLinus Torvalds 		}
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 		sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
10668e6fad4SRussell King 		symname = strtab + sym->st_name;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 		if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
10968e6fad4SRussell King 			pr_err("%s: section %u reloc %u sym '%s': out of bounds relocation, offset %d size %u\n",
11068e6fad4SRussell King 			       module->name, relindex, i, symname,
11168e6fad4SRussell King 			       rel->r_offset, dstsec->sh_size);
1121da177e4SLinus Torvalds 			return -ENOEXEC;
1131da177e4SLinus Torvalds 		}
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 		loc = dstsec->sh_addr + rel->r_offset;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 		switch (ELF32_R_TYPE(rel->r_info)) {
1182e1926e7SCatalin Marinas 		case R_ARM_NONE:
1192e1926e7SCatalin Marinas 			/* ignore */
1202e1926e7SCatalin Marinas 			break;
1212e1926e7SCatalin Marinas 
1221da177e4SLinus Torvalds 		case R_ARM_ABS32:
12355f0fb6aSAndrey Ryabinin 		case R_ARM_TARGET1:
1241da177e4SLinus Torvalds 			*(u32 *)loc += sym->st_value;
1251da177e4SLinus Torvalds 			break;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 		case R_ARM_PC24:
128c2e26114SDaniel Jacobowitz 		case R_ARM_CALL:
129c2e26114SDaniel Jacobowitz 		case R_ARM_JUMP24:
1302b8514d0SArd Biesheuvel 			if (sym->st_value & 3) {
1312b8514d0SArd Biesheuvel 				pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (ARM -> Thumb)\n",
1322b8514d0SArd Biesheuvel 				       module->name, relindex, i, symname);
1332b8514d0SArd Biesheuvel 				return -ENOEXEC;
1342b8514d0SArd Biesheuvel 			}
1352b8514d0SArd Biesheuvel 
136f592d323SBen Dooks 			offset = __mem_to_opcode_arm(*(u32 *)loc);
137f592d323SBen Dooks 			offset = (offset & 0x00ffffff) << 2;
138*ddbb7ea9SMasahiro Yamada 			offset = sign_extend32(offset, 25);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 			offset += sym->st_value - loc;
1417d485f64SArd Biesheuvel 
1427d485f64SArd Biesheuvel 			/*
1437d485f64SArd Biesheuvel 			 * Route through a PLT entry if 'offset' exceeds the
1447d485f64SArd Biesheuvel 			 * supported range. Note that 'offset + loc + 8'
1457d485f64SArd Biesheuvel 			 * contains the absolute jump target, i.e.,
1467d485f64SArd Biesheuvel 			 * @sym + addend, corrected for the +8 PC bias.
1477d485f64SArd Biesheuvel 			 */
1487d485f64SArd Biesheuvel 			if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
1497d485f64SArd Biesheuvel 			    (offset <= (s32)0xfe000000 ||
1507d485f64SArd Biesheuvel 			     offset >= (s32)0x02000000))
1517d485f64SArd Biesheuvel 				offset = get_module_plt(module, loc,
1527d485f64SArd Biesheuvel 							offset + loc + 8)
1537d485f64SArd Biesheuvel 					 - loc - 8;
1547d485f64SArd Biesheuvel 
1552b8514d0SArd Biesheuvel 			if (offset <= (s32)0xfe000000 ||
156c5f12503SKevin Welton 			    offset >= (s32)0x02000000) {
15768e6fad4SRussell King 				pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
15868e6fad4SRussell King 				       module->name, relindex, i, symname,
15968e6fad4SRussell King 				       ELF32_R_TYPE(rel->r_info), loc,
16068e6fad4SRussell King 				       sym->st_value);
1611da177e4SLinus Torvalds 				return -ENOEXEC;
1621da177e4SLinus Torvalds 			}
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 			offset >>= 2;
165f592d323SBen Dooks 			offset &= 0x00ffffff;
1661da177e4SLinus Torvalds 
167f592d323SBen Dooks 			*(u32 *)loc &= __opcode_to_mem_arm(0xff000000);
168f592d323SBen Dooks 			*(u32 *)loc |= __opcode_to_mem_arm(offset);
1691da177e4SLinus Torvalds 			break;
1701da177e4SLinus Torvalds 
1714731f8b6SDaniel Silverstone 	       case R_ARM_V4BX:
1724731f8b6SDaniel Silverstone 		       /* Preserve Rm and the condition code. Alter
1734731f8b6SDaniel Silverstone 			* other bits to re-code instruction as
1744731f8b6SDaniel Silverstone 			* MOV PC,Rm.
1754731f8b6SDaniel Silverstone 			*/
176f592d323SBen Dooks 		       *(u32 *)loc &= __opcode_to_mem_arm(0xf000000f);
177f592d323SBen Dooks 		       *(u32 *)loc |= __opcode_to_mem_arm(0x01a0f000);
1784731f8b6SDaniel Silverstone 		       break;
1794731f8b6SDaniel Silverstone 
1802e1926e7SCatalin Marinas 		case R_ARM_PREL31:
181050d18d1SArd Biesheuvel 			offset = (*(s32 *)loc << 1) >> 1; /* sign extend */
182050d18d1SArd Biesheuvel 			offset += sym->st_value - loc;
183050d18d1SArd Biesheuvel 			if (offset >= 0x40000000 || offset < -0x40000000) {
184050d18d1SArd Biesheuvel 				pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
185050d18d1SArd Biesheuvel 				       module->name, relindex, i, symname,
186050d18d1SArd Biesheuvel 				       ELF32_R_TYPE(rel->r_info), loc,
187050d18d1SArd Biesheuvel 				       sym->st_value);
188050d18d1SArd Biesheuvel 				return -ENOEXEC;
189050d18d1SArd Biesheuvel 			}
190050d18d1SArd Biesheuvel 			*(u32 *)loc &= 0x80000000;
191050d18d1SArd Biesheuvel 			*(u32 *)loc |= offset & 0x7fffffff;
1922e1926e7SCatalin Marinas 			break;
1932e1926e7SCatalin Marinas 
19422f2d230SArd Biesheuvel 		case R_ARM_REL32:
19522f2d230SArd Biesheuvel 			*(u32 *)loc += sym->st_value - loc;
19622f2d230SArd Biesheuvel 			break;
19722f2d230SArd Biesheuvel 
198ae51e609SPaul Gortmaker 		case R_ARM_MOVW_ABS_NC:
199ae51e609SPaul Gortmaker 		case R_ARM_MOVT_ABS:
20022f2d230SArd Biesheuvel 		case R_ARM_MOVW_PREL_NC:
20122f2d230SArd Biesheuvel 		case R_ARM_MOVT_PREL:
202f592d323SBen Dooks 			offset = tmp = __mem_to_opcode_arm(*(u32 *)loc);
203ae51e609SPaul Gortmaker 			offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
204*ddbb7ea9SMasahiro Yamada 			offset = sign_extend32(offset, 15);
205ae51e609SPaul Gortmaker 
206ae51e609SPaul Gortmaker 			offset += sym->st_value;
20722f2d230SArd Biesheuvel 			if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_PREL ||
20822f2d230SArd Biesheuvel 			    ELF32_R_TYPE(rel->r_info) == R_ARM_MOVW_PREL_NC)
20922f2d230SArd Biesheuvel 				offset -= loc;
21022f2d230SArd Biesheuvel 			if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS ||
21122f2d230SArd Biesheuvel 			    ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_PREL)
212ae51e609SPaul Gortmaker 				offset >>= 16;
213ae51e609SPaul Gortmaker 
214f592d323SBen Dooks 			tmp &= 0xfff0f000;
215f592d323SBen Dooks 			tmp |= ((offset & 0xf000) << 4) |
216ae51e609SPaul Gortmaker 				(offset & 0x0fff);
217f592d323SBen Dooks 
218f592d323SBen Dooks 			*(u32 *)loc = __opcode_to_mem_arm(tmp);
219ae51e609SPaul Gortmaker 			break;
220ae51e609SPaul Gortmaker 
221d6905849SArd Biesheuvel #ifdef CONFIG_ARM_HAS_GROUP_RELOCS
2221fa8c4b1SArd Biesheuvel 		case R_ARM_ALU_PC_G0_NC:
2231fa8c4b1SArd Biesheuvel 			group = 0;
2241fa8c4b1SArd Biesheuvel 			fallthrough;
2251fa8c4b1SArd Biesheuvel 		case R_ARM_ALU_PC_G1_NC:
2261fa8c4b1SArd Biesheuvel 			tmp = __mem_to_opcode_arm(*(u32 *)loc);
2271fa8c4b1SArd Biesheuvel 			offset = ror32(tmp & 0xff, (tmp & 0xf00) >> 7);
2281fa8c4b1SArd Biesheuvel 			if (tmp & BIT(22))
2291fa8c4b1SArd Biesheuvel 				offset = -offset;
2301fa8c4b1SArd Biesheuvel 			offset += sym->st_value - loc;
2311fa8c4b1SArd Biesheuvel 			if (offset < 0) {
2321fa8c4b1SArd Biesheuvel 				offset = -offset;
2331fa8c4b1SArd Biesheuvel 				tmp = (tmp & ~BIT(23)) | BIT(22); // SUB opcode
2341fa8c4b1SArd Biesheuvel 			} else {
2351fa8c4b1SArd Biesheuvel 				tmp = (tmp & ~BIT(22)) | BIT(23); // ADD opcode
2361fa8c4b1SArd Biesheuvel 			}
2371fa8c4b1SArd Biesheuvel 
2381fa8c4b1SArd Biesheuvel 			shift = get_group_rem(group, &offset);
2391fa8c4b1SArd Biesheuvel 			if (shift < 24) {
2401fa8c4b1SArd Biesheuvel 				offset >>= 24 - shift;
2411fa8c4b1SArd Biesheuvel 				offset |= (shift + 8) << 7;
2421fa8c4b1SArd Biesheuvel 			}
2431fa8c4b1SArd Biesheuvel 			*(u32 *)loc = __opcode_to_mem_arm((tmp & ~0xfff) | offset);
2441fa8c4b1SArd Biesheuvel 			break;
2451fa8c4b1SArd Biesheuvel 
2461fa8c4b1SArd Biesheuvel 		case R_ARM_LDR_PC_G2:
2471fa8c4b1SArd Biesheuvel 			tmp = __mem_to_opcode_arm(*(u32 *)loc);
2481fa8c4b1SArd Biesheuvel 			offset = tmp & 0xfff;
2491fa8c4b1SArd Biesheuvel 			if (~tmp & BIT(23))		// U bit cleared?
2501fa8c4b1SArd Biesheuvel 				offset = -offset;
2511fa8c4b1SArd Biesheuvel 			offset += sym->st_value - loc;
2521fa8c4b1SArd Biesheuvel 			if (offset < 0) {
2531fa8c4b1SArd Biesheuvel 				offset = -offset;
2541fa8c4b1SArd Biesheuvel 				tmp &= ~BIT(23);	// clear U bit
2551fa8c4b1SArd Biesheuvel 			} else {
2561fa8c4b1SArd Biesheuvel 				tmp |= BIT(23);		// set U bit
2571fa8c4b1SArd Biesheuvel 			}
2581fa8c4b1SArd Biesheuvel 			get_group_rem(2, &offset);
2591fa8c4b1SArd Biesheuvel 
2601fa8c4b1SArd Biesheuvel 			if (offset > 0xfff) {
2611fa8c4b1SArd Biesheuvel 				pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
2621fa8c4b1SArd Biesheuvel 				       module->name, relindex, i, symname,
2631fa8c4b1SArd Biesheuvel 				       ELF32_R_TYPE(rel->r_info), loc,
2641fa8c4b1SArd Biesheuvel 				       sym->st_value);
2651fa8c4b1SArd Biesheuvel 				return -ENOEXEC;
2661fa8c4b1SArd Biesheuvel 			}
2671fa8c4b1SArd Biesheuvel 			*(u32 *)loc = __opcode_to_mem_arm((tmp & ~0xfff) | offset);
2681fa8c4b1SArd Biesheuvel 			break;
269d6905849SArd Biesheuvel #endif
270b7493156SCatalin Marinas #ifdef CONFIG_THUMB2_KERNEL
271adca6dc2SCatalin Marinas 		case R_ARM_THM_CALL:
272adca6dc2SCatalin Marinas 		case R_ARM_THM_JUMP24:
2732b8514d0SArd Biesheuvel 			/*
2742b8514d0SArd Biesheuvel 			 * For function symbols, only Thumb addresses are
2752b8514d0SArd Biesheuvel 			 * allowed (no interworking).
2762b8514d0SArd Biesheuvel 			 *
2772b8514d0SArd Biesheuvel 			 * For non-function symbols, the destination
2782b8514d0SArd Biesheuvel 			 * has no specific ARM/Thumb disposition, so
2792b8514d0SArd Biesheuvel 			 * the branch is resolved under the assumption
2802b8514d0SArd Biesheuvel 			 * that interworking is not required.
2812b8514d0SArd Biesheuvel 			 */
2822b8514d0SArd Biesheuvel 			if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC &&
2832b8514d0SArd Biesheuvel 			    !(sym->st_value & 1)) {
2842b8514d0SArd Biesheuvel 				pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (Thumb -> ARM)\n",
2852b8514d0SArd Biesheuvel 				       module->name, relindex, i, symname);
2862b8514d0SArd Biesheuvel 				return -ENOEXEC;
2872b8514d0SArd Biesheuvel 			}
2882b8514d0SArd Biesheuvel 
289f592d323SBen Dooks 			upper = __mem_to_opcode_thumb16(*(u16 *)loc);
290f592d323SBen Dooks 			lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
291adca6dc2SCatalin Marinas 
292adca6dc2SCatalin Marinas 			/*
293adca6dc2SCatalin Marinas 			 * 25 bit signed address range (Thumb-2 BL and B.W
294adca6dc2SCatalin Marinas 			 * instructions):
295adca6dc2SCatalin Marinas 			 *   S:I1:I2:imm10:imm11:0
296adca6dc2SCatalin Marinas 			 * where:
297adca6dc2SCatalin Marinas 			 *   S     = upper[10]   = offset[24]
298adca6dc2SCatalin Marinas 			 *   I1    = ~(J1 ^ S)   = offset[23]
299adca6dc2SCatalin Marinas 			 *   I2    = ~(J2 ^ S)   = offset[22]
300adca6dc2SCatalin Marinas 			 *   imm10 = upper[9:0]  = offset[21:12]
301adca6dc2SCatalin Marinas 			 *   imm11 = lower[10:0] = offset[11:1]
302adca6dc2SCatalin Marinas 			 *   J1    = lower[13]
303adca6dc2SCatalin Marinas 			 *   J2    = lower[11]
304adca6dc2SCatalin Marinas 			 */
305adca6dc2SCatalin Marinas 			sign = (upper >> 10) & 1;
306adca6dc2SCatalin Marinas 			j1 = (lower >> 13) & 1;
307adca6dc2SCatalin Marinas 			j2 = (lower >> 11) & 1;
308adca6dc2SCatalin Marinas 			offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
309adca6dc2SCatalin Marinas 				((~(j2 ^ sign) & 1) << 22) |
310adca6dc2SCatalin Marinas 				((upper & 0x03ff) << 12) |
311adca6dc2SCatalin Marinas 				((lower & 0x07ff) << 1);
312*ddbb7ea9SMasahiro Yamada 			offset = sign_extend32(offset, 24);
313adca6dc2SCatalin Marinas 			offset += sym->st_value - loc;
314adca6dc2SCatalin Marinas 
3157d485f64SArd Biesheuvel 			/*
3167d485f64SArd Biesheuvel 			 * Route through a PLT entry if 'offset' exceeds the
3177d485f64SArd Biesheuvel 			 * supported range.
3187d485f64SArd Biesheuvel 			 */
3197d485f64SArd Biesheuvel 			if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
3207d485f64SArd Biesheuvel 			    (offset <= (s32)0xff000000 ||
3217d485f64SArd Biesheuvel 			     offset >= (s32)0x01000000))
3227d485f64SArd Biesheuvel 				offset = get_module_plt(module, loc,
3237d485f64SArd Biesheuvel 							offset + loc + 4)
3247d485f64SArd Biesheuvel 					 - loc - 4;
3257d485f64SArd Biesheuvel 
3262b8514d0SArd Biesheuvel 			if (offset <= (s32)0xff000000 ||
327adca6dc2SCatalin Marinas 			    offset >= (s32)0x01000000) {
32868e6fad4SRussell King 				pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
32968e6fad4SRussell King 				       module->name, relindex, i, symname,
33068e6fad4SRussell King 				       ELF32_R_TYPE(rel->r_info), loc,
33168e6fad4SRussell King 				       sym->st_value);
332adca6dc2SCatalin Marinas 				return -ENOEXEC;
333adca6dc2SCatalin Marinas 			}
334adca6dc2SCatalin Marinas 
335adca6dc2SCatalin Marinas 			sign = (offset >> 24) & 1;
336adca6dc2SCatalin Marinas 			j1 = sign ^ (~(offset >> 23) & 1);
337adca6dc2SCatalin Marinas 			j2 = sign ^ (~(offset >> 22) & 1);
338f592d323SBen Dooks 			upper = (u16)((upper & 0xf800) | (sign << 10) |
339adca6dc2SCatalin Marinas 					    ((offset >> 12) & 0x03ff));
340f592d323SBen Dooks 			lower = (u16)((lower & 0xd000) |
341adca6dc2SCatalin Marinas 				      (j1 << 13) | (j2 << 11) |
342adca6dc2SCatalin Marinas 				      ((offset >> 1) & 0x07ff));
343f592d323SBen Dooks 
344f592d323SBen Dooks 			*(u16 *)loc = __opcode_to_mem_thumb16(upper);
345f592d323SBen Dooks 			*(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
346adca6dc2SCatalin Marinas 			break;
347adca6dc2SCatalin Marinas 
3488dd47741SCatalin Marinas 		case R_ARM_THM_MOVW_ABS_NC:
3498dd47741SCatalin Marinas 		case R_ARM_THM_MOVT_ABS:
35022f2d230SArd Biesheuvel 		case R_ARM_THM_MOVW_PREL_NC:
35122f2d230SArd Biesheuvel 		case R_ARM_THM_MOVT_PREL:
352f592d323SBen Dooks 			upper = __mem_to_opcode_thumb16(*(u16 *)loc);
353f592d323SBen Dooks 			lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
3548dd47741SCatalin Marinas 
3558dd47741SCatalin Marinas 			/*
3568dd47741SCatalin Marinas 			 * MOVT/MOVW instructions encoding in Thumb-2:
3578dd47741SCatalin Marinas 			 *
3588dd47741SCatalin Marinas 			 * i	= upper[10]
3598dd47741SCatalin Marinas 			 * imm4	= upper[3:0]
3608dd47741SCatalin Marinas 			 * imm3	= lower[14:12]
3618dd47741SCatalin Marinas 			 * imm8	= lower[7:0]
3628dd47741SCatalin Marinas 			 *
3638dd47741SCatalin Marinas 			 * imm16 = imm4:i:imm3:imm8
3648dd47741SCatalin Marinas 			 */
3658dd47741SCatalin Marinas 			offset = ((upper & 0x000f) << 12) |
3668dd47741SCatalin Marinas 				((upper & 0x0400) << 1) |
3678dd47741SCatalin Marinas 				((lower & 0x7000) >> 4) | (lower & 0x00ff);
368*ddbb7ea9SMasahiro Yamada 			offset = sign_extend32(offset, 15);
3698dd47741SCatalin Marinas 			offset += sym->st_value;
3708dd47741SCatalin Marinas 
37122f2d230SArd Biesheuvel 			if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_PREL ||
37222f2d230SArd Biesheuvel 			    ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVW_PREL_NC)
37322f2d230SArd Biesheuvel 				offset -= loc;
37422f2d230SArd Biesheuvel 			if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS ||
37522f2d230SArd Biesheuvel 			    ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_PREL)
3768dd47741SCatalin Marinas 				offset >>= 16;
3778dd47741SCatalin Marinas 
378f592d323SBen Dooks 			upper = (u16)((upper & 0xfbf0) |
3798dd47741SCatalin Marinas 				      ((offset & 0xf000) >> 12) |
3808dd47741SCatalin Marinas 				      ((offset & 0x0800) >> 1));
381f592d323SBen Dooks 			lower = (u16)((lower & 0x8f00) |
3828dd47741SCatalin Marinas 				      ((offset & 0x0700) << 4) |
3838dd47741SCatalin Marinas 				      (offset & 0x00ff));
384f592d323SBen Dooks 			*(u16 *)loc = __opcode_to_mem_thumb16(upper);
385f592d323SBen Dooks 			*(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
3868dd47741SCatalin Marinas 			break;
387b7493156SCatalin Marinas #endif
3888dd47741SCatalin Marinas 
3891da177e4SLinus Torvalds 		default:
3904ed89f22SRussell King 			pr_err("%s: unknown relocation: %u\n",
3911da177e4SLinus Torvalds 			       module->name, ELF32_R_TYPE(rel->r_info));
3921da177e4SLinus Torvalds 			return -ENOEXEC;
3931da177e4SLinus Torvalds 		}
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 	return 0;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
find_mod_section(const Elf32_Ehdr * hdr,const Elf_Shdr * sechdrs,const char * name)3984a9cb360SRussell King static const Elf_Shdr *find_mod_section(const Elf32_Ehdr *hdr,
3994a9cb360SRussell King 	const Elf_Shdr *sechdrs, const char *name)
4004a9cb360SRussell King {
4014a9cb360SRussell King 	const Elf_Shdr *s, *se;
4024a9cb360SRussell King 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
4034a9cb360SRussell King 
4044a9cb360SRussell King 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++)
4054a9cb360SRussell King 		if (strcmp(name, secstrs + s->sh_name) == 0)
4064a9cb360SRussell King 			return s;
4074a9cb360SRussell King 
4084a9cb360SRussell King 	return NULL;
4094a9cb360SRussell King }
4104a9cb360SRussell King 
411dc21af99SRussell King extern void fixup_pv_table(const void *, unsigned long);
4124a9cb360SRussell King extern void fixup_smp(const void *, unsigned long);
4134a9cb360SRussell King 
module_finalize(const Elf32_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)4148931360eSRussell King int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
4158931360eSRussell King 		    struct module *mod)
4168931360eSRussell King {
417dc21af99SRussell King 	const Elf_Shdr *s = NULL;
4182e1926e7SCatalin Marinas #ifdef CONFIG_ARM_UNWIND
4198931360eSRussell King 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
4204a9cb360SRussell King 	const Elf_Shdr *sechdrs_end = sechdrs + hdr->e_shnum;
421b6f21d14SChen Zhongjin 	struct list_head *unwind_list = &mod->arch.unwind_list;
4228931360eSRussell King 
423b6f21d14SChen Zhongjin 	INIT_LIST_HEAD(unwind_list);
424b6f21d14SChen Zhongjin 	mod->arch.init_table = NULL;
4258931360eSRussell King 
4268931360eSRussell King 	for (s = sechdrs; s < sechdrs_end; s++) {
4278931360eSRussell King 		const char *secname = secstrs + s->sh_name;
428b6f21d14SChen Zhongjin 		const char *txtname;
429b6f21d14SChen Zhongjin 		const Elf_Shdr *txt_sec;
4308931360eSRussell King 
431b6f21d14SChen Zhongjin 		if (!(s->sh_flags & SHF_ALLOC) ||
432b6f21d14SChen Zhongjin 		    s->sh_type != ELF_SECTION_UNWIND)
43350005a8dSRussell King 			continue;
43450005a8dSRussell King 
435b6f21d14SChen Zhongjin 		if (!strcmp(".ARM.exidx", secname))
436b6f21d14SChen Zhongjin 			txtname = ".text";
437b6f21d14SChen Zhongjin 		else
438b6f21d14SChen Zhongjin 			txtname = secname + strlen(".ARM.exidx");
439b6f21d14SChen Zhongjin 		txt_sec = find_mod_section(hdr, sechdrs, txtname);
4402e1926e7SCatalin Marinas 
441b6f21d14SChen Zhongjin 		if (txt_sec) {
442b6f21d14SChen Zhongjin 			struct unwind_table *table =
443b6f21d14SChen Zhongjin 				unwind_table_add(s->sh_addr,
444b6f21d14SChen Zhongjin 						s->sh_size,
445b6f21d14SChen Zhongjin 						txt_sec->sh_addr,
446b6f21d14SChen Zhongjin 						txt_sec->sh_size);
447b6f21d14SChen Zhongjin 
448b6f21d14SChen Zhongjin 			list_add(&table->mod_list, unwind_list);
449b6f21d14SChen Zhongjin 
450b6f21d14SChen Zhongjin 			/* save init table for module_arch_freeing_init */
451b6f21d14SChen Zhongjin 			if (strcmp(".ARM.exidx.init.text", secname) == 0)
452b6f21d14SChen Zhongjin 				mod->arch.init_table = table;
453b6f21d14SChen Zhongjin 		}
454b6f21d14SChen Zhongjin 	}
4552e1926e7SCatalin Marinas #endif
456dc21af99SRussell King #ifdef CONFIG_ARM_PATCH_PHYS_VIRT
457dc21af99SRussell King 	s = find_mod_section(hdr, sechdrs, ".pv_table");
458dc21af99SRussell King 	if (s)
459dc21af99SRussell King 		fixup_pv_table((void *)s->sh_addr, s->sh_size);
460dc21af99SRussell King #endif
4614a9cb360SRussell King 	s = find_mod_section(hdr, sechdrs, ".alt.smp.init");
4624a9cb360SRussell King 	if (s && !is_smp())
46320feaab0SRussell King #ifdef CONFIG_SMP_ON_UP
4644a9cb360SRussell King 		fixup_smp((void *)s->sh_addr, s->sh_size);
46520feaab0SRussell King #else
46620feaab0SRussell King 		return -EINVAL;
46720feaab0SRussell King #endif
4681da177e4SLinus Torvalds 	return 0;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds void
module_arch_cleanup(struct module * mod)4721da177e4SLinus Torvalds module_arch_cleanup(struct module *mod)
4731da177e4SLinus Torvalds {
4748931360eSRussell King #ifdef CONFIG_ARM_UNWIND
475b6f21d14SChen Zhongjin 	struct unwind_table *tmp;
476b6f21d14SChen Zhongjin 	struct unwind_table *n;
4778931360eSRussell King 
478b6f21d14SChen Zhongjin 	list_for_each_entry_safe(tmp, n,
479b6f21d14SChen Zhongjin 			&mod->arch.unwind_list, mod_list) {
480b6f21d14SChen Zhongjin 		list_del(&tmp->mod_list);
481b6f21d14SChen Zhongjin 		unwind_table_del(tmp);
482cdcb07e4SVincent Whitchurch 	}
483b6f21d14SChen Zhongjin 	mod->arch.init_table = NULL;
484cdcb07e4SVincent Whitchurch #endif
485cdcb07e4SVincent Whitchurch }
486cdcb07e4SVincent Whitchurch 
module_arch_freeing_init(struct module * mod)487cdcb07e4SVincent Whitchurch void __weak module_arch_freeing_init(struct module *mod)
488cdcb07e4SVincent Whitchurch {
489cdcb07e4SVincent Whitchurch #ifdef CONFIG_ARM_UNWIND
490b6f21d14SChen Zhongjin 	struct unwind_table *init = mod->arch.init_table;
491b6f21d14SChen Zhongjin 
492b6f21d14SChen Zhongjin 	if (init) {
493b6f21d14SChen Zhongjin 		mod->arch.init_table = NULL;
494b6f21d14SChen Zhongjin 		list_del(&init->mod_list);
495b6f21d14SChen Zhongjin 		unwind_table_del(init);
496b6f21d14SChen Zhongjin 	}
4978931360eSRussell King #endif
4981da177e4SLinus Torvalds }
499