LKML Archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] modpost: refactor check_sec_ref()
@ 2023-10-01  5:47 Masahiro Yamada
  2023-10-01  5:47 ` [PATCH 2/2] modpost: factor out the common boilerplate of section_rel(a) Masahiro Yamada
  2023-10-02 15:55 ` [PATCH 1/2] modpost: refactor check_sec_ref() Nick Desaulniers
  0 siblings, 2 replies; 4+ messages in thread
From: Masahiro Yamada @ 2023-10-01  5:47 UTC (permalink / raw
  To: linux-kbuild
  Cc: linux-kernel, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier

We can replace &elf->sechdrs[i] with &sechdrs[i] to slightly shorten
the code because we already have the local variable 'sechdrs'.

However, defining 'sechdr' instead shortens the code further.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/mod/modpost.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 2f3b0fe6f68d..15d78fe152ac 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1523,16 +1523,17 @@ static void section_rel(struct module *mod, struct elf_info *elf,
 static void check_sec_ref(struct module *mod, struct elf_info *elf)
 {
 	int i;
-	Elf_Shdr *sechdrs = elf->sechdrs;
 
 	/* Walk through all sections */
 	for (i = 0; i < elf->num_sections; i++) {
-		check_section(mod->name, elf, &elf->sechdrs[i]);
+		Elf_Shdr *sechdr = &elf->sechdrs[i];
+
+		check_section(mod->name, elf, sechdr);
 		/* We want to process only relocation sections and not .init */
-		if (sechdrs[i].sh_type == SHT_RELA)
-			section_rela(mod, elf, &elf->sechdrs[i]);
-		else if (sechdrs[i].sh_type == SHT_REL)
-			section_rel(mod, elf, &elf->sechdrs[i]);
+		if (sechdr->sh_type == SHT_RELA)
+			section_rela(mod, elf, sechdr);
+		else if (sechdr->sh_type == SHT_REL)
+			section_rel(mod, elf, sechdr);
 	}
 }
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] modpost: factor out the common boilerplate of section_rel(a)
  2023-10-01  5:47 [PATCH 1/2] modpost: refactor check_sec_ref() Masahiro Yamada
@ 2023-10-01  5:47 ` Masahiro Yamada
  2023-10-02 16:01   ` Nick Desaulniers
  2023-10-02 15:55 ` [PATCH 1/2] modpost: refactor check_sec_ref() Nick Desaulniers
  1 sibling, 1 reply; 4+ messages in thread
From: Masahiro Yamada @ 2023-10-01  5:47 UTC (permalink / raw
  To: linux-kbuild
  Cc: linux-kernel, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier

The first few lines of section_rel() and section_rela() are the same.
They both retrieve the index of the section to which the relocaton
applies, and skip known-good sections. This common code should be moved
to check_sec_ref().

Avoid ugly casts when computing 'start' and 'stop', and also make the
Elf_Rel and Elf_Rela pointers const.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/mod/modpost.c | 50 ++++++++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 24 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 15d78fe152ac..0e18fe617ed1 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1425,17 +1425,10 @@ static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info,
 }
 
 static void section_rela(struct module *mod, struct elf_info *elf,
-			 Elf_Shdr *sechdr)
+			 unsigned int fsecndx, const char *fromsec,
+			 const Elf_Rela *start, const Elf_Rela *stop)
 {
-	Elf_Rela *rela;
-	unsigned int fsecndx = sechdr->sh_info;
-	const char *fromsec = sec_name(elf, fsecndx);
-	Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
-	Elf_Rela *stop  = (void *)start + sechdr->sh_size;
-
-	/* if from section (name) is know good then skip it */
-	if (match(fromsec, section_white_list))
-		return;
+	const Elf_Rela *rela;
 
 	for (rela = start; rela < stop; rela++) {
 		Elf_Addr taddr, r_offset;
@@ -1465,17 +1458,10 @@ static void section_rela(struct module *mod, struct elf_info *elf,
 }
 
 static void section_rel(struct module *mod, struct elf_info *elf,
-			Elf_Shdr *sechdr)
+			unsigned int fsecndx, const char *fromsec,
+			const Elf_Rel *start, const Elf_Rel *stop)
 {
-	Elf_Rel *rel;
-	unsigned int fsecndx = sechdr->sh_info;
-	const char *fromsec = sec_name(elf, fsecndx);
-	Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
-	Elf_Rel *stop  = (void *)start + sechdr->sh_size;
-
-	/* if from section (name) is know good then skip it */
-	if (match(fromsec, section_white_list))
-		return;
+	const Elf_Rel *rel;
 
 	for (rel = start; rel < stop; rel++) {
 		Elf_Sym *tsym;
@@ -1530,10 +1516,26 @@ static void check_sec_ref(struct module *mod, struct elf_info *elf)
 
 		check_section(mod->name, elf, sechdr);
 		/* We want to process only relocation sections and not .init */
-		if (sechdr->sh_type == SHT_RELA)
-			section_rela(mod, elf, sechdr);
-		else if (sechdr->sh_type == SHT_REL)
-			section_rel(mod, elf, sechdr);
+		if (sechdr->sh_type == SHT_REL || sechdr->sh_type == SHT_RELA) {
+			/* section to which the relocation applies */
+			unsigned int secndx = sechdr->sh_info;
+			const char *secname = sec_name(elf, secndx);
+			const void *start, *stop;
+
+			/* If the section is known good, skip it */
+			if (match(secname, section_white_list))
+				continue;
+
+			start = sym_get_data_by_offset(elf, i, 0);
+			stop = start + sechdr->sh_size;
+
+			if (sechdr->sh_type == SHT_RELA)
+				section_rela(mod, elf, secndx, secname,
+					     start, stop);
+			else
+				section_rel(mod, elf, secndx, secname,
+					    start, stop);
+		}
 	}
 }
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] modpost: refactor check_sec_ref()
  2023-10-01  5:47 [PATCH 1/2] modpost: refactor check_sec_ref() Masahiro Yamada
  2023-10-01  5:47 ` [PATCH 2/2] modpost: factor out the common boilerplate of section_rel(a) Masahiro Yamada
@ 2023-10-02 15:55 ` Nick Desaulniers
  1 sibling, 0 replies; 4+ messages in thread
From: Nick Desaulniers @ 2023-10-02 15:55 UTC (permalink / raw
  To: Masahiro Yamada
  Cc: linux-kbuild, linux-kernel, Nathan Chancellor, Nicolas Schier

On Sat, Sep 30, 2023 at 10:47 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> We can replace &elf->sechdrs[i] with &sechdrs[i] to slightly shorten
> the code because we already have the local variable 'sechdrs'.
>
> However, defining 'sechdr' instead shortens the code further.
>
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>
>  scripts/mod/modpost.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
>
> diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
> index 2f3b0fe6f68d..15d78fe152ac 100644
> --- a/scripts/mod/modpost.c
> +++ b/scripts/mod/modpost.c
> @@ -1523,16 +1523,17 @@ static void section_rel(struct module *mod, struct elf_info *elf,
>  static void check_sec_ref(struct module *mod, struct elf_info *elf)
>  {
>         int i;
> -       Elf_Shdr *sechdrs = elf->sechdrs;
>
>         /* Walk through all sections */
>         for (i = 0; i < elf->num_sections; i++) {
> -               check_section(mod->name, elf, &elf->sechdrs[i]);
> +               Elf_Shdr *sechdr = &elf->sechdrs[i];
> +
> +               check_section(mod->name, elf, sechdr);
>                 /* We want to process only relocation sections and not .init */
> -               if (sechdrs[i].sh_type == SHT_RELA)
> -                       section_rela(mod, elf, &elf->sechdrs[i]);
> -               else if (sechdrs[i].sh_type == SHT_REL)
> -                       section_rel(mod, elf, &elf->sechdrs[i]);
> +               if (sechdr->sh_type == SHT_RELA)
> +                       section_rela(mod, elf, sechdr);
> +               else if (sechdr->sh_type == SHT_REL)
> +                       section_rel(mod, elf, sechdr);
>         }
>  }
>
> --
> 2.39.2
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] modpost: factor out the common boilerplate of section_rel(a)
  2023-10-01  5:47 ` [PATCH 2/2] modpost: factor out the common boilerplate of section_rel(a) Masahiro Yamada
@ 2023-10-02 16:01   ` Nick Desaulniers
  0 siblings, 0 replies; 4+ messages in thread
From: Nick Desaulniers @ 2023-10-02 16:01 UTC (permalink / raw
  To: Masahiro Yamada
  Cc: linux-kbuild, linux-kernel, Nathan Chancellor, Nicolas Schier

On Sat, Sep 30, 2023 at 10:47 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> The first few lines of section_rel() and section_rela() are the same.
> They both retrieve the index of the section to which the relocaton

s/relocaton/relocation/

> applies, and skip known-good sections. This common code should be moved
> to check_sec_ref().
>
> Avoid ugly casts when computing 'start' and 'stop', and also make the
> Elf_Rel and Elf_Rela pointers const.
>
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>
>  scripts/mod/modpost.c | 50 ++++++++++++++++++++++---------------------
>  1 file changed, 26 insertions(+), 24 deletions(-)
>
> diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
> index 15d78fe152ac..0e18fe617ed1 100644
> --- a/scripts/mod/modpost.c
> +++ b/scripts/mod/modpost.c
> @@ -1425,17 +1425,10 @@ static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info,
>  }
>
>  static void section_rela(struct module *mod, struct elf_info *elf,
> -                        Elf_Shdr *sechdr)
> +                        unsigned int fsecndx, const char *fromsec,
> +                        const Elf_Rela *start, const Elf_Rela *stop)
>  {
> -       Elf_Rela *rela;
> -       unsigned int fsecndx = sechdr->sh_info;
> -       const char *fromsec = sec_name(elf, fsecndx);
> -       Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
> -       Elf_Rela *stop  = (void *)start + sechdr->sh_size;
> -
> -       /* if from section (name) is know good then skip it */
> -       if (match(fromsec, section_white_list))
> -               return;
> +       const Elf_Rela *rela;
>
>         for (rela = start; rela < stop; rela++) {
>                 Elf_Addr taddr, r_offset;
> @@ -1465,17 +1458,10 @@ static void section_rela(struct module *mod, struct elf_info *elf,
>  }
>
>  static void section_rel(struct module *mod, struct elf_info *elf,
> -                       Elf_Shdr *sechdr)
> +                       unsigned int fsecndx, const char *fromsec,
> +                       const Elf_Rel *start, const Elf_Rel *stop)
>  {
> -       Elf_Rel *rel;
> -       unsigned int fsecndx = sechdr->sh_info;
> -       const char *fromsec = sec_name(elf, fsecndx);
> -       Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
> -       Elf_Rel *stop  = (void *)start + sechdr->sh_size;
> -
> -       /* if from section (name) is know good then skip it */
> -       if (match(fromsec, section_white_list))
> -               return;
> +       const Elf_Rel *rel;
>
>         for (rel = start; rel < stop; rel++) {
>                 Elf_Sym *tsym;
> @@ -1530,10 +1516,26 @@ static void check_sec_ref(struct module *mod, struct elf_info *elf)
>
>                 check_section(mod->name, elf, sechdr);
>                 /* We want to process only relocation sections and not .init */
> -               if (sechdr->sh_type == SHT_RELA)
> -                       section_rela(mod, elf, sechdr);
> -               else if (sechdr->sh_type == SHT_REL)
> -                       section_rel(mod, elf, sechdr);
> +               if (sechdr->sh_type == SHT_REL || sechdr->sh_type == SHT_RELA) {
> +                       /* section to which the relocation applies */
> +                       unsigned int secndx = sechdr->sh_info;
> +                       const char *secname = sec_name(elf, secndx);
> +                       const void *start, *stop;
> +
> +                       /* If the section is known good, skip it */
> +                       if (match(secname, section_white_list))
> +                               continue;
> +
> +                       start = sym_get_data_by_offset(elf, i, 0);
> +                       stop = start + sechdr->sh_size;
> +
> +                       if (sechdr->sh_type == SHT_RELA)
> +                               section_rela(mod, elf, secndx, secname,
> +                                            start, stop);
> +                       else
> +                               section_rel(mod, elf, secndx, secname,
> +                                           start, stop);
> +               }
>         }
>  }
>
> --
> 2.39.2
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-10-02 16:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-01  5:47 [PATCH 1/2] modpost: refactor check_sec_ref() Masahiro Yamada
2023-10-01  5:47 ` [PATCH 2/2] modpost: factor out the common boilerplate of section_rel(a) Masahiro Yamada
2023-10-02 16:01   ` Nick Desaulniers
2023-10-02 15:55 ` [PATCH 1/2] modpost: refactor check_sec_ref() Nick Desaulniers

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 read-only IMAP folder(s) and NNTP newsgroup(s).