Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*  Kernel module help for PPC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)     Copyright (C) 2001 Rusty Russell.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/moduleloader.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* Count how many different relocations (different symbol, different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)    addend) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	unsigned int i, r_info, r_addend, _count_relocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	_count_relocs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	r_info = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	r_addend = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	for (i = 0; i < num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		/* Only count 24-bit relocs, others don't need stubs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		    (r_info != ELF32_R_SYM(rela[i].r_info) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		     r_addend != rela[i].r_addend)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 			_count_relocs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			r_info = ELF32_R_SYM(rela[i].r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			r_addend = rela[i].r_addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #ifdef CONFIG_DYNAMIC_FTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	_count_relocs++;	/* add one for ftrace_caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return _count_relocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int relacmp(const void *_x, const void *_y)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	const Elf32_Rela *x, *y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	y = (Elf32_Rela *)_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	x = (Elf32_Rela *)_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	/* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	 * make the comparison cheaper/faster. It won't affect the sorting or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	 * the counting algorithms' performance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (x->r_info < y->r_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	else if (x->r_info > y->r_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	else if (x->r_addend < y->r_addend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	else if (x->r_addend > y->r_addend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) /* Get the potential trampolines size required of the init and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)    non-init sections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 				  const Elf32_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 				  const char *secstrings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 				  int is_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	unsigned long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/* Everything marked ALLOC (this includes the exported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)            symbols) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	for (i = 1; i < hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		/* If it's called *.init*, and we're not init, we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)                    not interested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		    != is_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		/* We don't want to look at debug sections. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (strstr(secstrings + sechdrs[i].sh_name, ".debug"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (sechdrs[i].sh_type == SHT_RELA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			pr_debug("Found relocations in section %u\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			pr_debug("Ptr: %p.  Number: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			       (void *)hdr + sechdrs[i].sh_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			       sechdrs[i].sh_size / sizeof(Elf32_Rela));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			/* Sort the relocation information based on a symbol and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			 * addend key. This is a stable O(n*log n) complexity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			 * alogrithm but it will reduce the complexity of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			 * count_relocs() to linear complexity O(n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			sort((void *)hdr + sechdrs[i].sh_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			     sechdrs[i].sh_size / sizeof(Elf32_Rela),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			     sizeof(Elf32_Rela), relacmp, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			ret += count_relocs((void *)hdr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 					     + sechdrs[i].sh_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 					     sechdrs[i].sh_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 					     / sizeof(Elf32_Rela))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 				* sizeof(struct ppc_plt_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int module_frob_arch_sections(Elf32_Ehdr *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			      Elf32_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			      char *secstrings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			      struct module *me)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* Find .plt and .init.plt sections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	for (i = 0; i < hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			me->arch.init_plt_section = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			me->arch.core_plt_section = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		pr_err("Module doesn't contain .plt or .init.plt sections.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	/* Override their sizes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	sechdrs[me->arch.core_plt_section].sh_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		= get_plt_size(hdr, sechdrs, secstrings, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	sechdrs[me->arch.init_plt_section].sh_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		= get_plt_size(hdr, sechdrs, secstrings, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (entry->jump[0] != (PPC_INST_ADDIS | __PPC_RT(R12) | PPC_HA(val)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (entry->jump[1] != (PPC_INST_ADDI | __PPC_RT(R12) | __PPC_RA(R12) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			       PPC_LO(val)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* Set up a trampoline in the PLT to bounce us to the distant function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static uint32_t do_plt_call(void *location,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			    Elf32_Addr val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			    const Elf32_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			    struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct ppc_plt_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/* Init, or core PLT? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (location >= mod->core_layout.base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	    && location < mod->core_layout.base + mod->core_layout.size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	/* Find this entry, or if that fails, the next avail. entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	while (entry->jump[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		if (entry_matches(entry, val)) return (uint32_t)entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		entry++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * lis r12, sym@ha
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 * addi r12, r12, sym@l
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 * mtctr r12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	 * bctr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	entry->jump[0] = PPC_INST_ADDIS | __PPC_RT(R12) | PPC_HA(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	entry->jump[1] = PPC_INST_ADDI | __PPC_RT(R12) | __PPC_RA(R12) | PPC_LO(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	entry->jump[2] = PPC_INST_MTCTR | __PPC_RS(R12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	entry->jump[3] = PPC_INST_BCTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	pr_debug("Initialized plt for 0x%x at %p\n", val, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	return (uint32_t)entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int apply_relocate_add(Elf32_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		       const char *strtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		       unsigned int symindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		       unsigned int relsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		       struct module *module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	Elf32_Sym *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	uint32_t *location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	uint32_t value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	pr_debug("Applying ADD relocate section %u to %u\n", relsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	       sechdrs[relsec].sh_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		/* This is where to make the change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			+ rela[i].r_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		/* This is the symbol it is referring to.  Note that all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		   undefined symbols have been resolved.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			+ ELF32_R_SYM(rela[i].r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		/* `Everything is relative'. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		value = sym->st_value + rela[i].r_addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		switch (ELF32_R_TYPE(rela[i].r_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		case R_PPC_ADDR32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			/* Simply set it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			*(uint32_t *)location = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		case R_PPC_ADDR16_LO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			/* Low half of the symbol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			*(uint16_t *)location = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		case R_PPC_ADDR16_HI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			/* Higher half of the symbol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			*(uint16_t *)location = (value >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		case R_PPC_ADDR16_HA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			/* Sign-adjusted lower 16 bits: PPC ELF ABI says:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			   (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			   This is the same, only sane.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			*(uint16_t *)location = (value + 0x8000) >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		case R_PPC_REL24:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			if ((int)(value - (uint32_t)location) < -0x02000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			    || (int)(value - (uint32_t)location) >= 0x02000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 				value = do_plt_call(location, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 						    sechdrs, module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			/* Only replace bits 2 through 26 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			pr_debug("REL24 value = %08X. location = %08X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			       value, (uint32_t)location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			pr_debug("Location before: %08X.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			       *(uint32_t *)location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			*(uint32_t *)location
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				= (*(uint32_t *)location & ~0x03fffffc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 				| ((value - (uint32_t)location)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 				   & 0x03fffffc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			pr_debug("Location after: %08X.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			       *(uint32_t *)location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			pr_debug("ie. jump to %08X+%08X = %08X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			       *(uint32_t *)location & 0x03fffffc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			       (uint32_t)location,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			       (*(uint32_t *)location & 0x03fffffc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			       + (uint32_t)location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		case R_PPC_REL32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			/* 32-bit relative jump. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			*(uint32_t *)location = value - (uint32_t)location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			pr_err("%s: unknown ADD relocation: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			       module->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			       ELF32_R_TYPE(rela[i].r_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) #ifdef CONFIG_DYNAMIC_FTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	module->arch.tramp = do_plt_call(module->core_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 					 (unsigned long)ftrace_caller,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 					 sechdrs, module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (!module->arch.tramp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #endif