From: Emil Renner Berthing <kernel@esmil.dk>
To: linux-riscv@lists.infradead.org
Cc: Emil Renner Berthing <kernel@esmil.dk>,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Peter Zijlstra <peterz@infradead.org>,
Josh Poimboeuf <jpoimboe@redhat.com>,
Jason Baron <jbaron@akamai.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ard Biesheuvel <ardb@kernel.org>, Alexandre Ghiti <alex@ghiti.fr>,
Jisheng Zhang <jszhang@kernel.org>,
linux-kernel@vger.kernel.org
Subject: [PATCH v1 7/7] riscv: kernel/modules.c simplification
Date: Mon, 31 Jan 2022 19:21:45 +0100 [thread overview]
Message-ID: <20220131182145.236005-8-kernel@esmil.dk> (raw)
In-Reply-To: <20220131182145.236005-1-kernel@esmil.dk>
Signed-off-by: Emil Renner Berthing <kernel@esmil.dk>
---
arch/riscv/kernel/module.c | 93 ++++++++++++++++----------------------
1 file changed, 39 insertions(+), 54 deletions(-)
diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
index 2212d88776e0..e371977aecfd 100644
--- a/arch/riscv/kernel/module.c
+++ b/arch/riscv/kernel/module.c
@@ -298,24 +298,23 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
unsigned int symindex, unsigned int relsec,
struct module *me)
{
- Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
- int (*handler)(struct module *me, void *location, Elf_Addr v);
- Elf_Sym *sym;
- void *location;
- unsigned int i, type;
- Elf_Addr v;
- int res;
+ Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
+ unsigned int entries = sechdrs[relsec].sh_size / sizeof(*rel);
+ unsigned int i;
pr_debug("Applying relocate section %u to %u\n", relsec,
sechdrs[relsec].sh_info);
- for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
- /* This is where to make the change */
- location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
- + rel[i].r_offset;
- /* This is the symbol it is referring to */
- sym = (Elf_Sym *)sechdrs[symindex].sh_addr
+ for (i = 0; i < entries; i++) {
+ Elf_Sym *sym = (Elf_Sym *)sechdrs[symindex].sh_addr
+ ELF_RISCV_R_SYM(rel[i].r_info);
+ Elf_Addr loc = sechdrs[sechdrs[relsec].sh_info].sh_addr
+ + rel[i].r_offset;
+ unsigned int type = ELF_RISCV_R_TYPE(rel[i].r_info);
+ int (*handler)(struct module *me, void *location, Elf_Addr v);
+ Elf_Addr v;
+ int res;
+
if (IS_ERR_VALUE(sym->st_value)) {
/* Ignore unresolved weak symbol */
if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
@@ -325,8 +324,6 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
return -ENOENT;
}
- type = ELF_RISCV_R_TYPE(rel[i].r_info);
-
if (type < ARRAY_SIZE(reloc_handlers_rela))
handler = reloc_handlers_rela[type];
else
@@ -343,48 +340,36 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
unsigned int j;
- for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
- unsigned long hi20_loc =
- sechdrs[sechdrs[relsec].sh_info].sh_addr
+ /* find the corresponding HI20 entry */
+ for (j = 0; j < entries; j++) {
+ Elf_Sym *hi20_sym = (Elf_Sym *)sechdrs[symindex].sh_addr
+ + ELF_RISCV_R_SYM(rel[j].r_info);
+ Elf_Addr hi20_loc = sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rel[j].r_offset;
- u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
-
- /* Find the corresponding HI20 relocation entry */
- if (hi20_loc == sym->st_value
- && (hi20_type == R_RISCV_PCREL_HI20
- || hi20_type == R_RISCV_GOT_HI20)) {
- s32 hi20, lo12;
- Elf_Sym *hi20_sym =
- (Elf_Sym *)sechdrs[symindex].sh_addr
- + ELF_RISCV_R_SYM(rel[j].r_info);
- unsigned long hi20_sym_val =
- hi20_sym->st_value
- + rel[j].r_addend;
-
- /* Calculate lo12 */
- size_t offset = hi20_sym_val - hi20_loc;
- if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
- && hi20_type == R_RISCV_GOT_HI20) {
- offset = module_emit_got_entry(
- me, hi20_sym_val);
- offset = offset - hi20_loc;
- }
- hi20 = (offset + 0x800) & 0xfffff000;
- lo12 = offset - hi20;
- v = lo12;
-
- break;
- }
- }
- if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
- pr_err(
- "%s: Can not find HI20 relocation information\n",
- me->name);
- return -EINVAL;
+ unsigned int hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
+
+ if (hi20_loc != sym->st_value ||
+ (hi20_type != R_RISCV_PCREL_HI20 &&
+ hi20_type != R_RISCV_GOT_HI20))
+ continue;
+
+ /* calculate relative offset */
+ v = hi20_sym->st_value + rel[j].r_addend;
+
+ if (IS_ENABLED(CONFIG_MODULE_SECTIONS) &&
+ hi20_type == R_RISCV_GOT_HI20)
+ v = module_emit_got_entry(me, v);
+
+ v -= hi20_loc;
+ goto handle_reloc;
}
- }
- res = handler(me, location, v);
+ pr_err("%s: Cannot find HI20 relocation information\n",
+ me->name);
+ return -EINVAL;
+ }
+handle_reloc:
+ res = handler(me, (void *)loc, v);
if (res)
return res;
}
--
2.35.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
prev parent reply other threads:[~2022-01-31 18:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-31 18:21 [PATCH v1 0/7] Module relocation fixes and asm/insn.h header Emil Renner Berthing
2022-01-31 18:21 ` [PATCH v1 1/7] riscv: Avoid unaligned access when relocating modules Emil Renner Berthing
2022-01-31 18:21 ` [PATCH v1 2/7] riscv: Fix auipc+jalr relocation range checks Emil Renner Berthing
2022-01-31 18:21 ` [PATCH v1 3/7] riscv: Add asm/insn.h header Emil Renner Berthing
2022-01-31 18:21 ` [PATCH v1 4/7] riscv: Use asm/insn.h for module relocations Emil Renner Berthing
2022-01-31 18:21 ` [PATCH v1 5/7] riscv: Use asm/insn.h to generate plt entries Emil Renner Berthing
2022-01-31 18:21 ` [PATCH v1 6/7] riscv: Use asm/insn.h for jump labels Emil Renner Berthing
2022-01-31 18:21 ` Emil Renner Berthing [this message]
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=20220131182145.236005-8-kernel@esmil.dk \
--to=kernel@esmil.dk \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=ardb@kernel.org \
--cc=jbaron@akamai.com \
--cc=jpoimboe@redhat.com \
--cc=jszhang@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
/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).