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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * IA-64-specific support for kernel module loader.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2003 Hewlett-Packard Co
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *	David Mosberger-Tang <davidm@hpl.hp.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Loosely based on patch by Rusty Russell.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) /* relocs tested so far:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)    DIR64LSB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)    FPTR64LSB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)    GPREL22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)    LDXMOV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)    LDXMOV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)    LTOFF22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)    LTOFF22X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)    LTOFF22X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)    LTOFF_FPTR22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)    PCREL21B	(for br.call only; br.cond is not supported out of modules!)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)    PCREL60B	(for brl.cond only; brl.call is not supported for modules!)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)    PCREL64LSB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)    SECREL32LSB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)    SEGREL64LSB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/moduleloader.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <asm/patch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define ARCH_MODULE_DEBUG 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #if ARCH_MODULE_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) # define DEBUGP printk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) # define inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) # define DEBUGP(fmt , a...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #ifdef CONFIG_ITANIUM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) # define USE_BRL	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) # define USE_BRL	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define MAX_LTOFF	((uint64_t) (1 << 22))	/* max. allowable linkage-table offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) /* Define some relocation helper macros/types: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define FORMAT_SHIFT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define FORMAT_BITS	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define FORMAT_MASK	((1 << FORMAT_BITS) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define VALUE_SHIFT	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define VALUE_BITS	5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define VALUE_MASK	((1 << VALUE_BITS) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) enum reloc_target_format {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	/* direct encoded formats: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	RF_NONE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	RF_INSN14 = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	RF_INSN22 = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	RF_INSN64 = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	RF_32MSB = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	RF_32LSB = 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	RF_64MSB = 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	RF_64LSB = 7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	/* formats that cannot be directly decoded: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	RF_INSN60,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	RF_INSN21B,	/* imm21 form 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	RF_INSN21M,	/* imm21 form 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	RF_INSN21F	/* imm21 form 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) enum reloc_value_formula {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	RV_DIRECT = 4,		/* S + A */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	RV_GPREL = 5,		/* @gprel(S + A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	RV_LTREL = 6,		/* @ltoff(S + A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	RV_PLTREL = 7,		/* @pltoff(S + A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	RV_FPTR = 8,		/* @fptr(S + A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	RV_PCREL = 9,		/* S + A - P */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	RV_LTREL_FPTR = 10,	/* @ltoff(@fptr(S + A)) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	RV_SEGREL = 11,		/* @segrel(S + A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	RV_SECREL = 12,		/* @secrel(S + A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	RV_BDREL = 13,		/* BD + A */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	RV_LTV = 14,		/* S + A (like RV_DIRECT, except frozen at static link-time) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	RV_PCREL2 = 15,		/* S + A - P */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	RV_SPECIAL = 16,	/* various (see below) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	RV_RSVD17 = 17,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	RV_TPREL = 18,		/* @tprel(S + A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	RV_LTREL_TPREL = 19,	/* @ltoff(@tprel(S + A)) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	RV_DTPMOD = 20,		/* @dtpmod(S + A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	RV_LTREL_DTPMOD = 21,	/* @ltoff(@dtpmod(S + A)) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	RV_DTPREL = 22,		/* @dtprel(S + A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	RV_LTREL_DTPREL = 23,	/* @ltoff(@dtprel(S + A)) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	RV_RSVD24 = 24,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	RV_RSVD25 = 25,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	RV_RSVD26 = 26,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	RV_RSVD27 = 27
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/* 28-31 reserved for implementation-specific purposes.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #define N(reloc)	[R_IA64_##reloc] = #reloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static const char *reloc_name[256] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	N(NONE),		N(IMM14),		N(IMM22),		N(IMM64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	N(DIR32MSB),		N(DIR32LSB),		N(DIR64MSB),		N(DIR64LSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	N(GPREL22),		N(GPREL64I),		N(GPREL32MSB),		N(GPREL32LSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	N(GPREL64MSB),		N(GPREL64LSB),		N(LTOFF22),		N(LTOFF64I),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	N(PLTOFF22),		N(PLTOFF64I),		N(PLTOFF64MSB),		N(PLTOFF64LSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	N(FPTR64I),		N(FPTR32MSB),		N(FPTR32LSB),		N(FPTR64MSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	N(FPTR64LSB),		N(PCREL60B),		N(PCREL21B),		N(PCREL21M),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	N(PCREL21F),		N(PCREL32MSB),		N(PCREL32LSB),		N(PCREL64MSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	N(PCREL64LSB),		N(LTOFF_FPTR22),	N(LTOFF_FPTR64I),	N(LTOFF_FPTR32MSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	N(LTOFF_FPTR32LSB),	N(LTOFF_FPTR64MSB),	N(LTOFF_FPTR64LSB),	N(SEGREL32MSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	N(SEGREL32LSB),		N(SEGREL64MSB),		N(SEGREL64LSB),		N(SECREL32MSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	N(SECREL32LSB),		N(SECREL64MSB),		N(SECREL64LSB),		N(REL32MSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	N(REL32LSB),		N(REL64MSB),		N(REL64LSB),		N(LTV32MSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	N(LTV32LSB),		N(LTV64MSB),		N(LTV64LSB),		N(PCREL21BI),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	N(PCREL22),		N(PCREL64I),		N(IPLTMSB),		N(IPLTLSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	N(COPY),		N(LTOFF22X),		N(LDXMOV),		N(TPREL14),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	N(TPREL22),		N(TPREL64I),		N(TPREL64MSB),		N(TPREL64LSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	N(LTOFF_TPREL22),	N(DTPMOD64MSB),		N(DTPMOD64LSB),		N(LTOFF_DTPMOD22),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	N(DTPREL14),		N(DTPREL22),		N(DTPREL64I),		N(DTPREL32MSB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	N(DTPREL32LSB),		N(DTPREL64MSB),		N(DTPREL64LSB),		N(LTOFF_DTPREL22)
^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) #undef N
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* Opaque struct for insns, to protect against derefs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static inline uint64_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) bundle (const struct insn *insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return (uint64_t) insn & ~0xfUL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) slot (const struct insn *insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return (uint64_t) insn & 0x3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) apply_imm64 (struct module *mod, struct insn *insn, uint64_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (slot(insn) != 1 && slot(insn) != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		printk(KERN_ERR "%s: invalid slot number %d for IMM64\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		       mod->name, slot(insn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	ia64_patch_imm64((u64) insn, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) apply_imm60 (struct module *mod, struct insn *insn, uint64_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (slot(insn) != 1 && slot(insn) != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		printk(KERN_ERR "%s: invalid slot number %d for IMM60\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		       mod->name, slot(insn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (val + ((uint64_t) 1 << 59) >= (1UL << 60)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		printk(KERN_ERR "%s: value %ld out of IMM60 range\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			mod->name, (long) val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ia64_patch_imm60((u64) insn, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) apply_imm22 (struct module *mod, struct insn *insn, uint64_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (val + (1 << 21) >= (1 << 22)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		printk(KERN_ERR "%s: value %li out of IMM22 range\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			mod->name, (long)val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	ia64_patch((u64) insn, 0x01fffcfe000UL, (  ((val & 0x200000UL) << 15) /* bit 21 -> 36 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 					         | ((val & 0x1f0000UL) <<  6) /* bit 16 -> 22 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 					         | ((val & 0x00ff80UL) << 20) /* bit  7 -> 27 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 					         | ((val & 0x00007fUL) << 13) /* bit  0 -> 13 */));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) apply_imm21b (struct module *mod, struct insn *insn, uint64_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (val + (1 << 20) >= (1 << 21)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		printk(KERN_ERR "%s: value %li out of IMM21b range\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			mod->name, (long)val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	ia64_patch((u64) insn, 0x11ffffe000UL, (  ((val & 0x100000UL) << 16) /* bit 20 -> 36 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 					        | ((val & 0x0fffffUL) << 13) /* bit  0 -> 13 */));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) #if USE_BRL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct plt_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	/* Three instruction bundles in PLT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  	unsigned char bundle[2][16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static const struct plt_entry ia64_plt_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			0x00, 0x00, 0x00, 0x00, 0x00, 0x20, /*	     movl gp=TARGET_GP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			0x00, 0x00, 0x00, 0x60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*	     brl.many gp=TARGET_GP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			0x08, 0x00, 0x00, 0xc0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) patch_plt (struct module *mod, struct plt_entry *plt, long target_ip, unsigned long target_gp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (apply_imm64(mod, (struct insn *) (plt->bundle[0] + 2), target_gp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	    && apply_imm60(mod, (struct insn *) (plt->bundle[1] + 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			   (target_ip - (int64_t) plt->bundle[1]) / 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) plt_target (struct plt_entry *plt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	uint64_t b0, b1, *b = (uint64_t *) plt->bundle[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	long off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	b0 = b[0]; b1 = b[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	off = (  ((b1 & 0x00fffff000000000UL) >> 36)		/* imm20b -> bit 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	       | ((b0 >> 48) << 20) | ((b1 & 0x7fffffUL) << 36)	/* imm39 -> bit 20 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	       | ((b1 & 0x0800000000000000UL) << 0));		/* i -> bit 59 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return (long) plt->bundle[1] + 16*off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) #else /* !USE_BRL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct plt_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/* Three instruction bundles in PLT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  	unsigned char bundle[3][16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static const struct plt_entry ia64_plt_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*	     movl r16=TARGET_IP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			0x02, 0x00, 0x00, 0x60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			0x00, 0x00, 0x00, 0x00, 0x00, 0x20, /*	     movl gp=TARGET_GP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			0x00, 0x00, 0x00, 0x60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			0x11, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MIB] nop.m 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /*	     mov b6=r16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			0x60, 0x00, 0x80, 0x00		    /*	     br.few b6 */
^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) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) patch_plt (struct module *mod, struct plt_entry *plt, long target_ip, unsigned long target_gp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (apply_imm64(mod, (struct insn *) (plt->bundle[0] + 2), target_ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	    && apply_imm64(mod, (struct insn *) (plt->bundle[1] + 2), target_gp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) plt_target (struct plt_entry *plt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	uint64_t b0, b1, *b = (uint64_t *) plt->bundle[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	b0 = b[0]; b1 = b[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return (  ((b1 & 0x000007f000000000) >> 36)		/* imm7b -> bit 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		| ((b1 & 0x07fc000000000000) >> 43)		/* imm9d -> bit 7 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		| ((b1 & 0x0003e00000000000) >> 29)		/* imm5c -> bit 16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		| ((b1 & 0x0000100000000000) >> 23)		/* ic -> bit 21 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		| ((b0 >> 46) << 22) | ((b1 & 0x7fffff) << 40)	/* imm41 -> bit 22 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		| ((b1 & 0x0800000000000000) <<  4));		/* i -> bit 63 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) #endif /* !USE_BRL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) module_arch_freeing_init (struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (mod->arch.init_unw_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		unw_remove_unwind_table(mod->arch.init_unw_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		mod->arch.init_unw_table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* Have we already seen one of these relocations? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* FIXME: we could look in other sections, too --RR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) duplicate_reloc (const Elf64_Rela *rela, unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		if (rela[i].r_info == rela[num].r_info && rela[i].r_addend == rela[num].r_addend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* Count how many GOT entries we may need */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) count_gots (const Elf64_Rela *rela, unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	unsigned int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	/* Sure, this is order(n^2), but it's usually short, and not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)            time critical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		switch (ELF64_R_TYPE(rela[i].r_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		      case R_IA64_LTOFF22:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		      case R_IA64_LTOFF22X:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		      case R_IA64_LTOFF64I:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		      case R_IA64_LTOFF_FPTR22:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		      case R_IA64_LTOFF_FPTR64I:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		      case R_IA64_LTOFF_FPTR32MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		      case R_IA64_LTOFF_FPTR32LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		      case R_IA64_LTOFF_FPTR64MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		      case R_IA64_LTOFF_FPTR64LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			if (!duplicate_reloc(rela, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				ret++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /* Count how many PLT entries we may need */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) count_plts (const Elf64_Rela *rela, unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	unsigned int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	/* Sure, this is order(n^2), but it's usually short, and not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)            time critical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		switch (ELF64_R_TYPE(rela[i].r_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		      case R_IA64_PCREL21B:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		      case R_IA64_PLTOFF22:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		      case R_IA64_PLTOFF64I:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		      case R_IA64_PLTOFF64MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		      case R_IA64_PLTOFF64LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		      case R_IA64_IPLTMSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		      case R_IA64_IPLTLSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			if (!duplicate_reloc(rela, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 				ret++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* We need to create an function-descriptors for any internal function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)    which is referenced. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) count_fdescs (const Elf64_Rela *rela, unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	unsigned int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	/* Sure, this is order(n^2), but it's usually short, and not time critical.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		switch (ELF64_R_TYPE(rela[i].r_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		      case R_IA64_FPTR64I:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		      case R_IA64_FPTR32LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		      case R_IA64_FPTR32MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		      case R_IA64_FPTR64LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		      case R_IA64_FPTR64MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		      case R_IA64_LTOFF_FPTR22:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		      case R_IA64_LTOFF_FPTR32LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		      case R_IA64_LTOFF_FPTR32MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		      case R_IA64_LTOFF_FPTR64I:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		      case R_IA64_LTOFF_FPTR64LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		      case R_IA64_LTOFF_FPTR64MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		      case R_IA64_IPLTMSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		      case R_IA64_IPLTLSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			 * Jumps to static functions sometimes go straight to their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			 * offset.  Of course, that may not be possible if the jump is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			 * from init -> core or vice. versa, so we need to generate an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			 * FDESC (and PLT etc) for that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		      case R_IA64_PCREL21B:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			if (!duplicate_reloc(rela, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 				ret++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) module_frob_arch_sections (Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, char *secstrings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			   struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	unsigned long core_plts = 0, init_plts = 0, gots = 0, fdescs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	Elf64_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	 * To store the PLTs and function-descriptors, we expand the .text section for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	 * core module-code and the .init.text section for initialization code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	for (s = sechdrs; s < sechdrs_end; ++s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		if (strcmp(".core.plt", secstrings + s->sh_name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 			mod->arch.core_plt = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		else if (strcmp(".init.plt", secstrings + s->sh_name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			mod->arch.init_plt = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		else if (strcmp(".got", secstrings + s->sh_name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			mod->arch.got = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		else if (strcmp(".opd", secstrings + s->sh_name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			mod->arch.opd = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		else if (strcmp(".IA_64.unwind", secstrings + s->sh_name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			mod->arch.unwind = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (!mod->arch.core_plt || !mod->arch.init_plt || !mod->arch.got || !mod->arch.opd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		printk(KERN_ERR "%s: sections missing\n", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	/* GOT and PLTs can occur in any relocated section... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	for (s = sechdrs + 1; s < sechdrs_end; ++s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		const Elf64_Rela *rels = (void *)ehdr + s->sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		unsigned long numrels = s->sh_size/sizeof(Elf64_Rela);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		if (s->sh_type != SHT_RELA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		gots += count_gots(rels, numrels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		fdescs += count_fdescs(rels, numrels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		if (strstr(secstrings + s->sh_name, ".init"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			init_plts += count_plts(rels, numrels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			core_plts += count_plts(rels, numrels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	mod->arch.core_plt->sh_type = SHT_NOBITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	mod->arch.core_plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	mod->arch.core_plt->sh_addralign = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	mod->arch.core_plt->sh_size = core_plts * sizeof(struct plt_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	mod->arch.init_plt->sh_type = SHT_NOBITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	mod->arch.init_plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	mod->arch.init_plt->sh_addralign = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	mod->arch.init_plt->sh_size = init_plts * sizeof(struct plt_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	mod->arch.got->sh_type = SHT_NOBITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	mod->arch.got->sh_flags = ARCH_SHF_SMALL | SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	mod->arch.got->sh_addralign = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	mod->arch.got->sh_size = gots * sizeof(struct got_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	mod->arch.opd->sh_type = SHT_NOBITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	mod->arch.opd->sh_flags = SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	mod->arch.opd->sh_addralign = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	mod->arch.opd->sh_size = fdescs * sizeof(struct fdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	DEBUGP("%s: core.plt=%lx, init.plt=%lx, got=%lx, fdesc=%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	       __func__, mod->arch.core_plt->sh_size, mod->arch.init_plt->sh_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	       mod->arch.got->sh_size, mod->arch.opd->sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) in_init (const struct module *mod, uint64_t addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	return addr - (uint64_t) mod->init_layout.base < mod->init_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) in_core (const struct module *mod, uint64_t addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	return addr - (uint64_t) mod->core_layout.base < mod->core_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) is_internal (const struct module *mod, uint64_t value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	return in_init(mod, value) || in_core(mod, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)  * Get gp-relative offset for the linkage-table entry of VALUE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) static uint64_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) get_ltoff (struct module *mod, uint64_t value, int *okp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	struct got_entry *got, *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (!*okp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	got = (void *) mod->arch.got->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	for (e = got; e < got + mod->arch.next_got_entry; ++e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		if (e->val == value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	/* Not enough GOT entries? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	BUG_ON(e >= (struct got_entry *) (mod->arch.got->sh_addr + mod->arch.got->sh_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	e->val = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	++mod->arch.next_got_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)   found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	return (uint64_t) e - mod->arch.gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) gp_addressable (struct module *mod, uint64_t value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	return value - mod->arch.gp + MAX_LTOFF/2 < MAX_LTOFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) /* Get PC-relative PLT entry for this value.  Returns 0 on failure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static uint64_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) get_plt (struct module *mod, const struct insn *insn, uint64_t value, int *okp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	struct plt_entry *plt, *plt_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	uint64_t target_ip, target_gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	if (!*okp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (in_init(mod, (uint64_t) insn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		plt = (void *) mod->arch.init_plt->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		plt_end = (void *) plt + mod->arch.init_plt->sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		plt = (void *) mod->arch.core_plt->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		plt_end = (void *) plt + mod->arch.core_plt->sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	/* "value" is a pointer to a function-descriptor; fetch the target ip/gp from it: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	target_ip = ((uint64_t *) value)[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	target_gp = ((uint64_t *) value)[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	/* Look for existing PLT entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	while (plt->bundle[0][0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		if (plt_target(plt) == target_ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		if (++plt >= plt_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	*plt = ia64_plt_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if (!patch_plt(mod, plt, target_ip, target_gp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		*okp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) #if ARCH_MODULE_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	if (plt_target(plt) != target_ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		printk("%s: mistargeted PLT: wanted %lx, got %lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		       __func__, target_ip, plt_target(plt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		*okp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)   found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	return (uint64_t) plt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /* Get function descriptor for VALUE. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static uint64_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) get_fdesc (struct module *mod, uint64_t value, int *okp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	struct fdesc *fdesc = (void *) mod->arch.opd->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	if (!*okp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	if (!value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		printk(KERN_ERR "%s: fdesc for zero requested!\n", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	if (!is_internal(mod, value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		 * If it's not a module-local entry-point, "value" already points to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		 * function-descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	/* Look for existing function descriptor. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	while (fdesc->ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		if (fdesc->ip == value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			return (uint64_t)fdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		if ((uint64_t) ++fdesc >= mod->arch.opd->sh_addr + mod->arch.opd->sh_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	/* Create new one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	fdesc->ip = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	fdesc->gp = mod->arch.gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	return (uint64_t) fdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) do_reloc (struct module *mod, uint8_t r_type, Elf64_Sym *sym, uint64_t addend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	  Elf64_Shdr *sec, void *location)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	enum reloc_target_format format = (r_type >> FORMAT_SHIFT) & FORMAT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	enum reloc_value_formula formula = (r_type >> VALUE_SHIFT) & VALUE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	uint64_t val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	int ok = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	val = sym->st_value + addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	switch (formula) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	      case RV_SEGREL:	/* segment base is arbitrarily chosen to be 0 for kernel modules */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	      case RV_DIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	      case RV_GPREL:	  val -= mod->arch.gp; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	      case RV_LTREL:	  val = get_ltoff(mod, val, &ok); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	      case RV_PLTREL:	  val = get_plt(mod, location, val, &ok); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	      case RV_FPTR:	  val = get_fdesc(mod, val, &ok); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	      case RV_SECREL:	  val -= sec->sh_addr; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	      case RV_LTREL_FPTR: val = get_ltoff(mod, get_fdesc(mod, val, &ok), &ok); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	      case RV_PCREL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		switch (r_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		      case R_IA64_PCREL21B:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 			if ((in_init(mod, val) && in_core(mod, (uint64_t)location)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			    (in_core(mod, val) && in_init(mod, (uint64_t)location))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 				 * Init section may have been allocated far away from core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 				 * if the branch won't reach, then allocate a plt for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 				uint64_t delta = ((int64_t)val - (int64_t)location) / 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 				if (delta + (1 << 20) >= (1 << 21)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 					val = get_fdesc(mod, val, &ok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 					val = get_plt(mod, location, val, &ok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 			} else if (!is_internal(mod, val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 				val = get_plt(mod, location, val, &ok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		      default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 			val -= bundle(location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		      case R_IA64_PCREL32MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		      case R_IA64_PCREL32LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		      case R_IA64_PCREL64MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		      case R_IA64_PCREL64LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 			val -= (uint64_t) location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		switch (r_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		      case R_IA64_PCREL60B: format = RF_INSN60; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		      case R_IA64_PCREL21B: format = RF_INSN21B; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		      case R_IA64_PCREL21M: format = RF_INSN21M; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		      case R_IA64_PCREL21F: format = RF_INSN21F; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		      default: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	      case RV_BDREL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		val -= (uint64_t) (in_init(mod, val) ? mod->init_layout.base : mod->core_layout.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	      case RV_LTV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		/* can link-time value relocs happen here?  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	      case RV_PCREL2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		if (r_type == R_IA64_PCREL21BI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 			if (!is_internal(mod, val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 				printk(KERN_ERR "%s: %s reloc against "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 					"non-local symbol (%lx)\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 					reloc_name[r_type], (unsigned long)val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 				return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 			format = RF_INSN21B;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		val -= bundle(location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	      case RV_SPECIAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		switch (r_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		      case R_IA64_IPLTMSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		      case R_IA64_IPLTLSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 			val = get_fdesc(mod, get_plt(mod, location, val, &ok), &ok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 			format = RF_64LSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 			if (r_type == R_IA64_IPLTMSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 				format = RF_64MSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		      case R_IA64_SUB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 			val = addend - sym->st_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 			format = RF_INSN64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 		      case R_IA64_LTOFF22X:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 			if (gp_addressable(mod, val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 				val -= mod->arch.gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 				val = get_ltoff(mod, val, &ok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 			format = RF_INSN22;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		      case R_IA64_LDXMOV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 			if (gp_addressable(mod, val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 				/* turn "ld8" into "mov": */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 				DEBUGP("%s: patching ld8 at %p to mov\n", __func__, location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 				ia64_patch((u64) location, 0x1fff80fe000UL, 0x10000000000UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		      default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 			if (reloc_name[r_type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 				printk(KERN_ERR "%s: special reloc %s not supported",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 				       mod->name, reloc_name[r_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 				printk(KERN_ERR "%s: unknown special reloc %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 				       mod->name, r_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 			return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	      case RV_TPREL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	      case RV_LTREL_TPREL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	      case RV_DTPMOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	      case RV_LTREL_DTPMOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	      case RV_DTPREL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	      case RV_LTREL_DTPREL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		printk(KERN_ERR "%s: %s reloc not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		       mod->name, reloc_name[r_type] ? reloc_name[r_type] : "?");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	      default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 		printk(KERN_ERR "%s: unknown reloc %x\n", mod->name, r_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 		return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	if (!ok)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 		return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	DEBUGP("%s: [%p]<-%016lx = %s(%lx)\n", __func__, location, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	       reloc_name[r_type] ? reloc_name[r_type] : "?", sym->st_value + addend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	switch (format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	      case RF_INSN21B:	ok = apply_imm21b(mod, location, (int64_t) val / 16); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	      case RF_INSN22:	ok = apply_imm22(mod, location, val); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	      case RF_INSN64:	ok = apply_imm64(mod, location, val); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	      case RF_INSN60:	ok = apply_imm60(mod, location, (int64_t) val / 16); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	      case RF_32LSB:	put_unaligned(val, (uint32_t *) location); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	      case RF_64LSB:	put_unaligned(val, (uint64_t *) location); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	      case RF_32MSB:	/* ia64 Linux is little-endian... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	      case RF_64MSB:	/* ia64 Linux is little-endian... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	      case RF_INSN14:	/* must be within-module, i.e., resolved by "ld -r" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	      case RF_INSN21M:	/* must be within-module, i.e., resolved by "ld -r" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	      case RF_INSN21F:	/* must be within-module, i.e., resolved by "ld -r" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 		printk(KERN_ERR "%s: format %u needed by %s reloc is not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 		       mod->name, format, reloc_name[r_type] ? reloc_name[r_type] : "?");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 		return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	      default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 		printk(KERN_ERR "%s: relocation %s resulted in unknown format %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 		       mod->name, reloc_name[r_type] ? reloc_name[r_type] : "?", format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	return ok ? 0 : -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) apply_relocate_add (Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 		    unsigned int relsec, struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	unsigned int i, n = sechdrs[relsec].sh_size / sizeof(Elf64_Rela);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	Elf64_Rela *rela = (void *) sechdrs[relsec].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	Elf64_Shdr *target_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	DEBUGP("%s: applying section %u (%u relocs) to %u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	       relsec, n, sechdrs[relsec].sh_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	target_sec = sechdrs + sechdrs[relsec].sh_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	if (target_sec->sh_entsize == ~0UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 		 * If target section wasn't allocated, we don't need to relocate it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 		 * Happens, e.g., for debug sections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	if (!mod->arch.gp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		 * XXX Should have an arch-hook for running this after final section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 		 *     addresses have been selected...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 		uint64_t gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 		if (mod->core_layout.size > MAX_LTOFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 			 * This takes advantage of fact that SHF_ARCH_SMALL gets allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 			 * at the end of the module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 			gp = mod->core_layout.size - MAX_LTOFF / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 			gp = mod->core_layout.size / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 		gp = (uint64_t) mod->core_layout.base + ((gp + 7) & -8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 		mod->arch.gp = gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 		DEBUGP("%s: placing gp at 0x%lx\n", __func__, gp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	for (i = 0; i < n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 		ret = do_reloc(mod, ELF64_R_TYPE(rela[i].r_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 			       ((Elf64_Sym *) sechdrs[symindex].sh_addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 				+ ELF64_R_SYM(rela[i].r_info)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 			       rela[i].r_addend, target_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 			       (void *) target_sec->sh_addr + rela[i].r_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)  * Modules contain a single unwind table which covers both the core and the init text
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)  * sections but since the two are not contiguous, we need to split this table up such that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)  * we can register (and unregister) each "segment" separately.  Fortunately, this sounds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)  * more complicated than it really is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) register_unwind_table (struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	struct unw_table_entry *start = (void *) mod->arch.unwind->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	struct unw_table_entry *end = start + mod->arch.unwind->sh_size / sizeof (*start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	struct unw_table_entry tmp, *e1, *e2, *core, *init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	unsigned long num_init = 0, num_core = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	/* First, count how many init and core unwind-table entries there are.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	for (e1 = start; e1 < end; ++e1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 		if (in_init(mod, e1->start_offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 			++num_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 			++num_core;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	 * Second, sort the table such that all unwind-table entries for the init and core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	 * text sections are nicely separated.  We do this with a stupid bubble sort
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 	 * (unwind tables don't get ridiculously huge).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	for (e1 = start; e1 < end; ++e1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 		for (e2 = e1 + 1; e2 < end; ++e2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 			if (e2->start_offset < e1->start_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 				tmp = *e1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 				*e1 = *e2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 				*e2 = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 	 * Third, locate the init and core segments in the unwind table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 	if (in_init(mod, start->start_offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 		init = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 		core = start + num_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 		core = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 		init = start + num_core;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 	DEBUGP("%s: name=%s, gp=%lx, num_init=%lu, num_core=%lu\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	       mod->name, mod->arch.gp, num_init, num_core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	 * Fourth, register both tables (if not empty).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	if (num_core > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 		mod->arch.core_unw_table = unw_add_unwind_table(mod->name, 0, mod->arch.gp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 								core, core + num_core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 		DEBUGP("%s:  core: handle=%p [%p-%p)\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 		       mod->arch.core_unw_table, core, core + num_core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	if (num_init > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 		mod->arch.init_unw_table = unw_add_unwind_table(mod->name, 0, mod->arch.gp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 								init, init + num_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 		DEBUGP("%s:  init: handle=%p [%p-%p)\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 		       mod->arch.init_unw_table, init, init + num_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) module_finalize (const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 	struct mod_arch_specific *mas = &mod->arch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 	DEBUGP("%s: init: entry=%p\n", __func__, mod->init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	if (mas->unwind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 		register_unwind_table(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	 * ".opd" was already relocated to the final destination. Store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 	 * it's address for use in symbolizer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 	mas->opd_addr = (void *)mas->opd->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 	mas->opd_size = mas->opd->sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	 * Module relocation was already done at this point. Section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 	 * headers are about to be deleted. Wipe out load-time context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 	mas->core_plt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 	mas->init_plt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 	mas->got = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 	mas->opd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 	mas->unwind = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	mas->gp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 	mas->next_got_entry = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) module_arch_cleanup (struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 	if (mod->arch.init_unw_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 		unw_remove_unwind_table(mod->arch.init_unw_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 		mod->arch.init_unw_table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 	if (mod->arch.core_unw_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 		unw_remove_unwind_table(mod->arch.core_unw_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 		mod->arch.core_unw_table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) void *dereference_module_function_descriptor(struct module *mod, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 	struct mod_arch_specific *mas = &mod->arch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) 	if (ptr < mas->opd_addr || ptr >= mas->opd_addr + mas->opd_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) 		return ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) 	return dereference_function_descriptor(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) }