From: Mauricio Faria de Oliveira <mfo@canonical.com>
To: linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org,
linux-kbuild@vger.kernel.org, linux-fsdevel@vger.kernel.org
Cc: Masahiro Yamada <masahiroy@kernel.org>,
Michal Marek <michal.lkml@markovi.net>,
Nick Desaulniers <ndesaulniers@google.com>,
Luis Chamberlain <mcgrof@kernel.org>,
Kees Cook <keescook@chromium.org>,
Iurii Zaikin <yzaikin@google.com>
Subject: [RFC PATCH 2/6] modpost: deduplicate section_rel[a]()
Date: Thu, 21 Jul 2022 23:24:12 -0300 [thread overview]
Message-ID: <20220722022416.137548-3-mfo@canonical.com> (raw)
In-Reply-To: <20220722022416.137548-1-mfo@canonical.com>
Now both functions are almost identical, and we can again generalize
the relocation types Elf_Rela/Elf_Rel with Elf_Rela, and handle some
differences with conditionals on section header type (SHT_RELA/REL).
The important bit is to make sure the loop increment uses the right
size for pointer arithmethic.
The original reason for split functions to make program logic easier
to follow; commit 5b24c0715fc4 ("kbuild: code refactoring in modpost").
Hopefully these 2 commits may help improving that, without an impact
in understanding the code due to generalization of relocation types.
Signed-off-by: Mauricio Faria de Oliveira <mfo@canonical.com>
---
scripts/mod/modpost.c | 61 ++++++++++++++++---------------------------
1 file changed, 23 insertions(+), 38 deletions(-)
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 4c1038dccae0..d1ed67fa290b 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1794,63 +1794,49 @@ static int get_relx_sym(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *rela,
return 0;
}
-static void section_rela(const char *modname, struct elf_info *elf,
+/* The caller must ensure sechdr->sh_type == SHT_RELA or SHT_REL. */
+static void section_relx(const char *modname, struct elf_info *elf,
Elf_Shdr *sechdr)
{
Elf_Sym *sym;
- Elf_Rela *rela;
+ Elf_Rela *relx; /* access .r_addend in SHT_RELA _only_! */
Elf_Rela r;
+ size_t relx_size;
const char *fromsec;
Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Elf_Rela *stop = (void *)start + sechdr->sh_size;
fromsec = sech_name(elf, sechdr);
- fromsec += strlen(".rela");
+ if (sechdr->sh_type == SHT_RELA) {
+ relx_size = sizeof(Elf_Rela);
+ fromsec += strlen(".rela");
+ } else if (sechdr->sh_type == SHT_REL) {
+ relx_size = sizeof(Elf_Rel);
+ fromsec += strlen(".rel");
+ } else {
+ error("%s: [%s.ko] not relocation section\n", fromsec, modname);
+ return;
+ }
+
/* if from section (name) is know good then skip it */
if (match(fromsec, section_white_list))
return;
- for (rela = start; rela < stop; rela++) {
- if (get_relx_sym(elf, sechdr, rela, &r, &sym))
+ for (relx = start; relx < stop; relx = (void *)relx + relx_size) {
+ if (get_relx_sym(elf, sechdr, relx, &r, &sym))
continue;
switch (elf->hdr->e_machine) {
case EM_RISCV:
- if (!strcmp("__ex_table", fromsec) &&
+ if (sechdr->sh_type == SHT_RELA &&
+ !strcmp("__ex_table", fromsec) &&
ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
continue;
break;
}
- if (is_second_extable_reloc(start, rela, fromsec))
- find_extable_entry_size(fromsec, &r);
- check_section_mismatch(modname, elf, &r, sym, fromsec);
- }
-}
-
-static void section_rel(const char *modname, struct elf_info *elf,
- Elf_Shdr *sechdr)
-{
- Elf_Sym *sym;
- Elf_Rel *rel;
- Elf_Rela r;
- const char *fromsec;
-
- Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
- Elf_Rel *stop = (void *)start + sechdr->sh_size;
-
- fromsec = sech_name(elf, sechdr);
- fromsec += strlen(".rel");
- /* if from section (name) is know good then skip it */
- if (match(fromsec, section_white_list))
- return;
-
- for (rel = start; rel < stop; rel++) {
- if (get_relx_sym(elf, sechdr, (Elf_Rela *)rel, &r, &sym)
- continue;
-
- if (is_second_extable_reloc(start, rel, fromsec))
+ if (is_second_extable_reloc(start, relx, fromsec))
find_extable_entry_size(fromsec, &r);
check_section_mismatch(modname, elf, &r, sym, fromsec);
}
@@ -1877,10 +1863,9 @@ static void check_sec_ref(const char *modname, struct elf_info *elf)
for (i = 0; i < elf->num_sections; i++) {
check_section(modname, elf, &elf->sechdrs[i]);
/* We want to process only relocation sections and not .init */
- if (sechdrs[i].sh_type == SHT_RELA)
- section_rela(modname, elf, &elf->sechdrs[i]);
- else if (sechdrs[i].sh_type == SHT_REL)
- section_rel(modname, elf, &elf->sechdrs[i]);
+ if (sechdrs[i].sh_type == SHT_RELA ||
+ sechdrs[i].sh_type == SHT_REL)
+ section_relx(modname, elf, &elf->sechdrs[i]);
}
}
--
2.25.1
next prev parent reply other threads:[~2022-07-22 2:24 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-22 2:24 [RFC PATCH 0/6] Introduce "sysctl:" module aliases Mauricio Faria de Oliveira
2022-07-22 2:24 ` [RFC PATCH 1/6] modpost: factor out elf/arch-specific code from section_rel[a]() Mauricio Faria de Oliveira
2022-07-22 2:24 ` Mauricio Faria de Oliveira [this message]
2022-07-26 9:19 ` [RFC PATCH 2/6] modpost: deduplicate section_rel[a]() Masahiro Yamada
2022-07-27 17:10 ` Mauricio Faria de Oliveira
2022-07-22 2:24 ` [RFC PATCH 3/6] sysctl, mod_devicetable: shadow struct ctl_table.procname for file2alias Mauricio Faria de Oliveira
2022-07-26 9:25 ` Masahiro Yamada
2022-07-27 17:11 ` Mauricio Faria de Oliveira
2022-07-22 2:24 ` [RFC PATCH 4/6] module, modpost: introduce support for MODULE_SYSCTL_TABLE Mauricio Faria de Oliveira
2022-07-22 2:24 ` [RFC PATCH 5/6] netfilter: conntrack: use MODULE_SYSCTL_TABLE Mauricio Faria de Oliveira
2022-07-22 2:24 ` [RFC PATCH 6/6] sysctl: introduce /proc/sys/kernel/modprobe_sysctl_alias Mauricio Faria de Oliveira
2022-07-26 9:22 ` Masahiro Yamada
2022-07-27 17:11 ` Mauricio Faria de Oliveira
2022-07-26 9:02 ` [RFC PATCH 0/6] Introduce "sysctl:" module aliases Masahiro Yamada
2022-07-27 17:09 ` Mauricio Faria de Oliveira
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220722022416.137548-3-mfo@canonical.com \
--to=mfo@canonical.com \
--cc=keescook@chromium.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=masahiroy@kernel.org \
--cc=mcgrof@kernel.org \
--cc=michal.lkml@markovi.net \
--cc=ndesaulniers@google.com \
--cc=yzaikin@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).