From patchwork Tue Jun 20 12:05:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13285748 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 612B1EB64D7 for ; Tue, 20 Jun 2023 12:05:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232566AbjFTMFg (ORCPT ); Tue, 20 Jun 2023 08:05:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232574AbjFTMF3 (ORCPT ); Tue, 20 Jun 2023 08:05:29 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39DFE170C; Tue, 20 Jun 2023 05:05:28 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C1F1C611EA; Tue, 20 Jun 2023 12:05:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C678FC433C8; Tue, 20 Jun 2023 12:05:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687262727; bh=fo/HY+4/U2YoL8+5myRX8J+xt11fEMJWxO7l4IEHPH8=; h=From:To:Cc:Subject:Date:From; b=BdJBhZ2pi6yfDXYGE1RXqL+cmvlCGxYZJ3/FD0mqpEWoHOfavsABlo5ii6L5HoPvl GeQsxNRHs3NimIw8hB438iHjwAHWncN5XGRiyYI0iourWuzGJEGmlwXSskFvi4ncGe +Q6fqMMSS5Bc/DgF/2wElaJuuBTg5WUfyt2kUp6LRKFXGT/ruYPee1WJ0JWdzY6xh9 kWzscZNA0+18nP8SJedpsX83oTz+CRHvV1V9Iha53KJSKjMQAudIZTNEIN+VlQejgB 7e1MDtVirm4VgCZJNHTkVkb7noEd794QJ+GcEe2P78q8TsQWtB/BmJFRHSahjTQ6kz B4gaoRVlkLHqQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Nathan Chancellor , Nick Desaulniers , Nicolas Schier Subject: [PATCH 1/3] modpost: factor out inst location calculation to section_rel() Date: Tue, 20 Jun 2023 21:05:19 +0900 Message-Id: <20230620120521.688124-1-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org All the addend_*_rel() functions calculate the instruction location in the same way. Factor out the similar code to the caller. Squash reloc_location() too. Signed-off-by: Masahiro Yamada --- scripts/mod/modpost.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 6e0b8be32648..2551ac9d5bd3 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1256,16 +1256,9 @@ static void check_section_mismatch(struct module *mod, struct elf_info *elf, tosec, taddr); } -static unsigned int *reloc_location(struct elf_info *elf, - Elf_Shdr *sechdr, Elf_Rela *r) -{ - return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset); -} - -static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) +static int addend_386_rel(uint32_t *location, Elf_Rela *r) { unsigned int r_typ = ELF_R_TYPE(r->r_info); - unsigned int *location = reloc_location(elf, sechdr, r); switch (r_typ) { case R_386_32: @@ -1302,11 +1295,10 @@ static int32_t sign_extend32(int32_t value, int index) return (int32_t)(value << shift) >> shift; } -static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) +static int addend_arm_rel(void *loc, struct elf_info *elf, Elf_Rela *r) { unsigned int r_typ = ELF_R_TYPE(r->r_info); Elf_Sym *sym = elf->symtab_start + ELF_R_SYM(r->r_info); - void *loc = reloc_location(elf, sechdr, r); uint32_t inst, upper, lower, sign, j1, j2; int32_t offset; @@ -1396,11 +1388,10 @@ static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) return 0; } -static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) +static int addend_mips_rel(uint32_t *location, Elf_Rela *r) { unsigned int r_typ = ELF_R_TYPE(r->r_info); - unsigned int *location = reloc_location(elf, sechdr, r); - unsigned int inst; + uint32_t inst; if (r_typ == R_MIPS_HI16) return 1; /* skip this */ @@ -1502,6 +1493,8 @@ static void section_rel(struct module *mod, struct elf_info *elf, return; for (rel = start; rel < stop; rel++) { + void *loc; + r.r_offset = TO_NATIVE(rel->r_offset); #if KERNEL_ELFCLASS == ELFCLASS64 if (elf->hdr->e_machine == EM_MIPS) { @@ -1519,17 +1512,20 @@ static void section_rel(struct module *mod, struct elf_info *elf, r_sym = ELF_R_SYM(r.r_info); #endif r.r_addend = 0; + + loc = sym_get_data_by_offset(elf, fsecndx, r.r_offset); + switch (elf->hdr->e_machine) { case EM_386: - if (addend_386_rel(elf, sechdr, &r)) + if (addend_386_rel(loc, &r)) continue; break; case EM_ARM: - if (addend_arm_rel(elf, sechdr, &r)) + if (addend_arm_rel(loc, elf, &r)) continue; break; case EM_MIPS: - if (addend_mips_rel(elf, sechdr, &r)) + if (addend_mips_rel(loc, &r)) continue; break; default: From patchwork Tue Jun 20 12:05:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13285749 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF343EB64D8 for ; Tue, 20 Jun 2023 12:05:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232624AbjFTMFo (ORCPT ); Tue, 20 Jun 2023 08:05:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232618AbjFTMFb (ORCPT ); Tue, 20 Jun 2023 08:05:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C951010F3; Tue, 20 Jun 2023 05:05:29 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5B9D5611FB; Tue, 20 Jun 2023 12:05:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C0CEC433CA; Tue, 20 Jun 2023 12:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687262728; bh=2ReNXn5QKyfmsIaf+RPx+AKwwDkxRAR+rcyRaaWC81Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AgueyVrtoivd4awbdEnSIoa4Jbw+JcNKE+wbISLopEeSRnV96cql5isRbTSgBJbzQ 25YKqrFH+ofdk+3xOhIXEWAr1sIpkrC6NXYhiT8O7G4awRamuhivH9umAUmhN7Jdfm sA/VauSD3Pn097KlGDesTcvIrqZNn81ZiOKfnmXHl71sqO1v3xPoC6To8wMZvrrQvP Ovv/8b7THfmlLF1Q+RX2oU/mlDJVxWUt/ZPOG5b6FczqIPF+6m29sOE0Hs79Ml1+RR MUkP7sZDDZGyF3Ymm6oZsJbV79gaNaP0vkv2I/1rR2ZqI9CFSzfiixmpHSmdl2x6ft aHDygN0MS8ueQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Nathan Chancellor , Nick Desaulniers , Nicolas Schier Subject: [PATCH 2/3] modpost: factor out Elf_Sym pointer calculation to section_rel() Date: Tue, 20 Jun 2023 21:05:20 +0900 Message-Id: <20230620120521.688124-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230620120521.688124-1-masahiroy@kernel.org> References: <20230620120521.688124-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Pass the Elf_Sym pointer to addend_arm_rel() as well as to check_section_mismatch(). Signed-off-by: Masahiro Yamada Reviewed-by: Nick Desaulniers --- scripts/mod/modpost.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 2551ac9d5bd3..ffe45c54f024 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1295,10 +1295,9 @@ static int32_t sign_extend32(int32_t value, int index) return (int32_t)(value << shift) >> shift; } -static int addend_arm_rel(void *loc, struct elf_info *elf, Elf_Rela *r) +static int addend_arm_rel(void *loc, Elf_Sym *sym, Elf_Rela *r) { unsigned int r_typ = ELF_R_TYPE(r->r_info); - Elf_Sym *sym = elf->symtab_start + ELF_R_SYM(r->r_info); uint32_t inst, upper, lower, sign, j1, j2; int32_t offset; @@ -1493,6 +1492,7 @@ static void section_rel(struct module *mod, struct elf_info *elf, return; for (rel = start; rel < stop; rel++) { + Elf_Sym *tsym; void *loc; r.r_offset = TO_NATIVE(rel->r_offset); @@ -1514,6 +1514,7 @@ static void section_rel(struct module *mod, struct elf_info *elf, r.r_addend = 0; loc = sym_get_data_by_offset(elf, fsecndx, r.r_offset); + tsym = elf->symtab_start + ELF_R_SYM(r.r_info); switch (elf->hdr->e_machine) { case EM_386: @@ -1521,7 +1522,7 @@ static void section_rel(struct module *mod, struct elf_info *elf, continue; break; case EM_ARM: - if (addend_arm_rel(loc, elf, &r)) + if (addend_arm_rel(loc, tsym, &r)) continue; break; case EM_MIPS: @@ -1532,7 +1533,7 @@ static void section_rel(struct module *mod, struct elf_info *elf, fatal("Please add code to calculate addend for this architecture\n"); } - check_section_mismatch(mod, elf, elf->symtab_start + r_sym, + check_section_mismatch(mod, elf, tsym, fsecndx, fromsec, r.r_offset, r.r_addend); } } From patchwork Tue Jun 20 12:05:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13285750 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 576EBEB64D7 for ; Tue, 20 Jun 2023 12:05:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232735AbjFTMFp (ORCPT ); Tue, 20 Jun 2023 08:05:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232642AbjFTMFc (ORCPT ); Tue, 20 Jun 2023 08:05:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A46BBE7; Tue, 20 Jun 2023 05:05:31 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 41354611FD; Tue, 20 Jun 2023 12:05:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43F70C433CB; Tue, 20 Jun 2023 12:05:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687262730; bh=ZLfc+BSXjZJWCTWdJRPUCYMoN06HfdLCglJJPJ8L31Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m9Y5Pp5twFPMxnncPtk/qaRl6y59jcV46j961RurHjbJBOLkh7Z/blHDOcPHOSSS/ ozQxCkpfI8e+MEn/iGB0qYzfKHLEIDNirR5Qh9tuCEha3ijLYRpzT+LykDOdirkCbV 0dCnCdRgxYx3dRq9PsjnxO4O4nOUVLC1yjAQsZ0pUvoAQysIE9AJx+VNl1sAcx7XWq lTgjpwGhTvAJUMQEyZX25gja5520ZEWYUSZOfLayal1b1WzJlA0CuRE/ePF3/gxzKL dXi2TdV9+bCRiCmvaw6CeVm1vDSGuI95AKpSJCdYBFerD7yoFh+axB7R7JNZqga2TU /l9bxglQFZqZw== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Nathan Chancellor , Nick Desaulniers , Nicolas Schier Subject: [PATCH 3/3] modpost: continue even with unknown relocation type Date: Tue, 20 Jun 2023 21:05:21 +0900 Message-Id: <20230620120521.688124-3-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230620120521.688124-1-masahiroy@kernel.org> References: <20230620120521.688124-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Currently, unknown relocation types are just skipped. The value of r_addend is only needed to get the symbol name in case is_valid_name(elf, sym) returns false. Even if we do not know how to calculate r_addend, we should continue. At worst, we will get "(unknown)" as the symbol name, but it is better than failing to detect section mismatches. Signed-off-by: Masahiro Yamada --- scripts/mod/modpost.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index ffe45c54f024..7e92380d4f1f 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1267,6 +1267,8 @@ static int addend_386_rel(uint32_t *location, Elf_Rela *r) case R_386_PC32: r->r_addend = TO_NATIVE(*location) + 4; break; + default: + r->r_addend = (Elf_Addr)(-1); } return 0; } @@ -1382,7 +1384,7 @@ static int addend_arm_rel(void *loc, Elf_Sym *sym, Elf_Rela *r) r->r_addend = offset + sym->st_value + 4; break; default: - return 1; + r->r_addend = (Elf_Addr)(-1); } return 0; } @@ -1392,8 +1394,6 @@ static int addend_mips_rel(uint32_t *location, Elf_Rela *r) unsigned int r_typ = ELF_R_TYPE(r->r_info); uint32_t inst; - if (r_typ == R_MIPS_HI16) - return 1; /* skip this */ inst = TO_NATIVE(*location); switch (r_typ) { case R_MIPS_LO16: @@ -1405,6 +1405,8 @@ static int addend_mips_rel(uint32_t *location, Elf_Rela *r) case R_MIPS_32: r->r_addend = inst; break; + default: + r->r_addend = (Elf_Addr)(-1); } return 0; } @@ -1514,20 +1516,17 @@ static void section_rel(struct module *mod, struct elf_info *elf, r.r_addend = 0; loc = sym_get_data_by_offset(elf, fsecndx, r.r_offset); - tsym = elf->symtab_start + ELF_R_SYM(r.r_info); + tsym = elf->symtab_start + r_sym; switch (elf->hdr->e_machine) { case EM_386: - if (addend_386_rel(loc, &r)) - continue; + addend_386_rel(loc, &r); break; case EM_ARM: - if (addend_arm_rel(loc, tsym, &r)) - continue; + addend_arm_rel(loc, tsym, &r); break; case EM_MIPS: - if (addend_mips_rel(loc, &r)) - continue; + addend_mips_rel(loc, &r); break; default: fatal("Please add code to calculate addend for this architecture\n");