From nobody Sun Feb 8 20:34:50 2026 Received: from mailout4.samsung.com (mailout4.samsung.com [203.254.224.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF109221DB1 for ; Wed, 24 Dec 2025 04:32:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=203.254.224.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766550726; cv=none; b=VKXsq8HCE8SdgaEZOL91xU1UWXT33t5buF+hQHEUenVZKI6I2YLxSgvuv6IeIveIR9ybcVSrwSLkeDXXf1doUx0StL75MfBA+YXYvBdAZKMWPYdrHI6je4Xj4IWJx0EowdPUHgxGgPepUnEtY4isShvQlIpMC8SRTZzQcO0i2Ls= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766550726; c=relaxed/simple; bh=5hCqqnMuX901tu2ICqgwO+v8/gncXU+eR+poRyeGdhE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type: References; b=YmOnsv4iunBGN6l1OyH2T/xdt/Sl5tga52K6GKLQFUQiYE4Qjyd0G8FMNDG8JGdIxgp7k7EmVZu0TBomv5ZFPEXBNWpEHNUIs+TiUJsaOgCBzc5pas0tEZUHg4XmDPnmMLm7TeSbBjLFzl+ITWwU08OESLovTUmDubd75pGnE2k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=samsung.com; spf=pass smtp.mailfrom=samsung.com; dkim=pass (1024-bit key) header.d=samsung.com header.i=@samsung.com header.b=KcFQuvpr; arc=none smtp.client-ip=203.254.224.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=samsung.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=samsung.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=samsung.com header.i=@samsung.com header.b="KcFQuvpr" Received: from epcas2p3.samsung.com (unknown [182.195.41.55]) by mailout4.samsung.com (KnoxPortal) with ESMTP id 20251224043159epoutp04ee7b0e7352c7e9b9e5bfa7e1362784ec~EDIUYELyq1399013990epoutp043 for ; Wed, 24 Dec 2025 04:31:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout4.samsung.com 20251224043159epoutp04ee7b0e7352c7e9b9e5bfa7e1362784ec~EDIUYELyq1399013990epoutp043 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1766550719; bh=YXO066pxNrwE8u/0uOTahD5Dqr7uT2f/RtFKzM9d2wM=; h=From:To:Cc:Subject:Date:References:From; b=KcFQuvprpBZcEJTUP1iwq7jKeCjP1BBbfZf8qdeub3bas04Un6yMxqALKcSvaVFMZ NpYlC23WzPFs+EdJyQl7KI35W8bmlK7rFSVWIt1K6W15AKe1aiGigQ2DZKrg4N9O1j ZrWiEvNW8dkkQ2Z/i2HQdqwBaRFZfP+vb1Ovw83k= Received: from epsnrtp01.localdomain (unknown [182.195.42.153]) by epcas2p2.samsung.com (KnoxPortal) with ESMTPS id 20251224043159epcas2p2bec422ba6a2026ec0596fcfdba517134~EDIUIrhio0447704477epcas2p25; Wed, 24 Dec 2025 04:31:59 +0000 (GMT) Received: from epcas2p1.samsung.com (unknown [182.195.38.210]) by epsnrtp01.localdomain (Postfix) with ESMTP id 4dbf9B6kllz6B9m5; Wed, 24 Dec 2025 04:31:58 +0000 (GMT) Received: from epsmtip1.samsung.com (unknown [182.195.34.30]) by epcas2p2.samsung.com (KnoxPortal) with ESMTPA id 20251224043158epcas2p217889374e0ea4b1722371ca143741d85~EDITQtNh33231632316epcas2p2c; Wed, 24 Dec 2025 04:31:58 +0000 (GMT) Received: from KORCO166536.samsungds.net (unknown [12.80.211.123]) by epsmtip1.samsung.com (KnoxPortal) with ESMTPA id 20251224043158epsmtip1adab85e65ea1c837451facb7a8f6c346~EDITNzRPo0800208002epsmtip12; Wed, 24 Dec 2025 04:31:58 +0000 (GMT) From: Yunjin Kim To: Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez Cc: Yunjin Kim , linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org Subject: [PATCH] ANDROID: gki: kallsyms: add kallsyms_lookup_address_and_size. Date: Wed, 24 Dec 2025 13:31:57 +0900 Message-Id: <20251224043157.59289-1-yunzhen.kim@samsung.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMS-MailID: 20251224043158epcas2p217889374e0ea4b1722371ca143741d85 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-Sendblock-Type: AUTO_CONFIDENTIAL CMS-TYPE: 102P cpgsPolicy: CPGSC10-234,Y X-CFilter-Loop: Reflected X-CMS-RootMailID: 20251224043158epcas2p217889374e0ea4b1722371ca143741d85 References: This methods are used by AKKstub-ARM Kernel Kstub. We need to implement an automatic kernel-method mock that streamlines the mocking process during kernel-method testing and enables fully automated operations. This mechanism must traverse the binary instructions of the target function in memory, locate the appropriate instruction, and replace it. To perform the traversal, it must know the function=E2=80=99s entry add= ress and the size of its instruction range. Bug: Change-Id: I5a318f762d4412e70b0c8dcf2dfed326312bdc65 Signed-off-by: Yunjin Kim --- include/linux/kallsyms.h | 2 ++ include/linux/module.h | 2 ++ kernel/kallsyms.c | 38 ++++++++++++++++++++++++++ kernel/module/kallsyms.c | 58 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h index 1c6a6c1704d8..ec59f25259f2 100644 --- a/include/linux/kallsyms.h +++ b/include/linux/kallsyms.h @@ -78,6 +78,8 @@ int kallsyms_on_each_match_symbol(int (*fn)(void *, unsig= ned long), /* Lookup the address for a symbol. Returns 0 if not found. */ unsigned long kallsyms_lookup_name(const char *name); +unsigned long kallsyms_lookup_address_and_size(const char *name, unsigned = long *address, unsigned long * size); + extern int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize, unsigned long *offset); diff --git a/include/linux/module.h b/include/linux/module.h index 5beb39d56197..47fb46bd1b92 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -976,6 +976,8 @@ int module_get_kallsym(unsigned int symnum, unsigned lo= ng *value, char *type, /* Look for this name: can be of form module:name. */ unsigned long module_kallsyms_lookup_name(const char *name); +unsigned long module_kallsyms_lookup_address_and_size(const char *name, un= signed long *address, unsigned long *size); + unsigned long find_kallsyms_symbol_value(struct module *mod, const char *n= ame); #else /* CONFIG_MODULES && CONFIG_KALLSYMS */ diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index a9a0ca605d4a..5533816794da 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -160,6 +160,22 @@ unsigned long kallsyms_sym_address(int idx) return kallsyms_relative_base - 1 - kallsyms_offsets[idx]; } +unsigned long kallsyms_sym_address_and_size(int idx, unsigned long *size) +{ + /* values are unsigned offsets if --absolute-percpu is not in effect */ + *size =3D kallsyms_offsets[idx+1] - kallsyms_offsets[idx]; + + if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU)) + return kallsyms_relative_base + (u32)kallsyms_offsets[idx]; + + /* ...otherwise, positive offsets are absolute values */ + if (kallsyms_offsets[idx] >=3D 0) + return kallsyms_offsets[idx]; + + /* ...and negative offsets are relative to kallsyms_relative_base - 1 */ + return kallsyms_relative_base - 1 - kallsyms_offsets[idx]; +} + static unsigned int get_symbol_seq(int index) { unsigned int i, seq =3D 0; @@ -242,6 +258,27 @@ unsigned long kallsyms_lookup_name(const char *name) return module_kallsyms_lookup_name(name); } +EXPORT_SYMBOL(kallsyms_lookup_name); + +unsigned long kallsyms_lookup_address_and_size(const char *name, unsigned = long *address, unsigned long * size) +{ + int ret; + unsigned int i; + + /* Skip the search for empty string. */ + if (!*name) + return 0; + + ret =3D kallsyms_lookup_names(name, &i, NULL); + if (!ret){ + *address =3D kallsyms_sym_address_and_size(get_symbol_seq(i), size); + return *address; + } + + //return module_kallsyms_lookup_name(name); + return module_kallsyms_lookup_address_and_size(name, address, size); +} +EXPORT_SYMBOL(kallsyms_lookup_address_and_size); /* * Iterate over all symbols in vmlinux. For symbols from modules use @@ -430,6 +467,7 @@ int lookup_symbol_name(unsigned long addr, char *symnam= e) /* See if it's in a module. */ return lookup_module_symbol_name(addr, symname); } +EXPORT_SYMBOL(lookup_symbol_name); /* Look up a kernel symbol and return it in a text buffer. */ static int __sprint_symbol(char *buffer, unsigned long address, diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c index bf65e0c3c86f..e8552f5e64c8 100644 --- a/kernel/module/kallsyms.c +++ b/kernel/module/kallsyms.c @@ -462,6 +462,64 @@ unsigned long module_kallsyms_lookup_name(const char *= name) return ret; } +static unsigned long __find_kallsyms_symbol_address_and_size_value(struct = module *mod, const char *name, unsigned long *address, unsigned long* size) +{ + unsigned int i; + struct mod_kallsyms *kallsyms =3D rcu_dereference_sched(mod->kallsyms); + unsigned long ret =3D 0; + + for (i =3D 0; i < kallsyms->num_symtab; i++) { + const Elf_Sym *sym =3D &kallsyms->symtab[i]; + + if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) =3D=3D 0 && + sym->st_shndx !=3D SHN_UNDEF){ + ret =3D kallsyms_symbol_value(sym); + *address =3D ret; + *size =3D sym->st_size; + return ret; + } + } + return 0; +} + +static unsigned long __module_kallsyms_lookup_address_and_size(const char = *name, unsigned long *address, unsigned long *size) +{ + struct module *mod; + char *colon; + + colon =3D strnchr(name, MODULE_NAME_LEN, ':'); + if (colon) { + mod =3D find_module_all(name, colon - name, false); + if (mod) + return __find_kallsyms_symbol_address_and_size_value(mod, colon + 1, ad= dress, size); + return 0; + } + + list_for_each_entry_rcu(mod, &modules, list) { + unsigned long ret; + + if (mod->state =3D=3D MODULE_STATE_UNFORMED) + continue; + ret =3D __find_kallsyms_symbol_address_and_size_value(mod, name, address= , size); + if (ret) + return ret; + } + return 0; +} + +/* Look for this name: can be of form module:name. */ +unsigned long module_kallsyms_lookup_address_and_size(const char *name, un= signed long *address, unsigned long *size) +{ + unsigned long ret; + + /* Don't lock: we're in enough trouble already. */ + preempt_disable(); + ret =3D __module_kallsyms_lookup_address_and_size(name, address, size); + preempt_enable(); + return ret; +} + + unsigned long find_kallsyms_symbol_value(struct module *mod, const char *n= ame) { unsigned long ret; -- 2.34.1