2023-07-26 10:57:53

by Petr Tesarik

[permalink] [raw]
Subject: [PATCH v2 2/3] riscv/purgatory: do not link with string.o and its dependencies

From: Petr Tesarik <[email protected]>

Linking with this object file makes kexec_file_load(2) fail because of
multiple unknown relocation types:

- R_RISCV_ADD16, R_RISCV_SUB16: used by alternatives in strcmp()
- R_RISCV_GOT_HI20: used to resolve _ctype in strcasecmp()

All this hassle is needed for one single call to memcmp() from
verify_sha256_digest() to compare 32 bytes of SHA256 checksum.

Simply replace this memcmp() call with an explicit loop over those 32 bytes
and remove the need to link with string.o and all the other object files
that provide undefined symbols from that object file.

Fixes: 838b3e28488f ("RISC-V: Load purgatory in kexec_file")
Signed-off-by: Petr Tesarik <[email protected]>
Cc: Li Zhengyu <[email protected]>
Cc: [email protected]
---
arch/riscv/purgatory/Makefile | 26 +-------------------------
arch/riscv/purgatory/purgatory.c | 6 ++++--
2 files changed, 5 insertions(+), 27 deletions(-)

diff --git a/arch/riscv/purgatory/Makefile b/arch/riscv/purgatory/Makefile
index dc20e166983e..00f50cd29310 100644
--- a/arch/riscv/purgatory/Makefile
+++ b/arch/riscv/purgatory/Makefile
@@ -1,39 +1,21 @@
# SPDX-License-Identifier: GPL-2.0
OBJECT_FILES_NON_STANDARD := y

-purgatory-y := purgatory.o sha256.o entry.o string.o ctype.o memcpy.o memset.o
-purgatory-y += strcmp.o strlen.o strncmp.o
+purgatory-y := purgatory.o sha256.o entry.o memcpy.o memset.o

targets += $(purgatory-y)
PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y))

-$(obj)/string.o: $(srctree)/lib/string.c FORCE
- $(call if_changed_rule,cc_o_c)
-
-$(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE
- $(call if_changed_rule,cc_o_c)
-
$(obj)/memcpy.o: $(srctree)/arch/riscv/lib/memcpy.S FORCE
$(call if_changed_rule,as_o_S)

$(obj)/memset.o: $(srctree)/arch/riscv/lib/memset.S FORCE
$(call if_changed_rule,as_o_S)

-$(obj)/strcmp.o: $(srctree)/arch/riscv/lib/strcmp.S FORCE
- $(call if_changed_rule,as_o_S)
-
-$(obj)/strlen.o: $(srctree)/arch/riscv/lib/strlen.S FORCE
- $(call if_changed_rule,as_o_S)
-
-$(obj)/strncmp.o: $(srctree)/arch/riscv/lib/strncmp.S FORCE
- $(call if_changed_rule,as_o_S)
-
$(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
$(call if_changed_rule,cc_o_c)

CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
-CFLAGS_string.o := -D__DISABLE_EXPORTS
-CFLAGS_ctype.o := -D__DISABLE_EXPORTS

# When profile-guided optimization is enabled, llvm emits two different
# overlapping text sections, which is not supported by kexec. Remove profile
@@ -83,12 +65,6 @@ CFLAGS_purgatory.o += $(PURGATORY_CFLAGS)
CFLAGS_REMOVE_sha256.o += $(PURGATORY_CFLAGS_REMOVE)
CFLAGS_sha256.o += $(PURGATORY_CFLAGS)

-CFLAGS_REMOVE_string.o += $(PURGATORY_CFLAGS_REMOVE)
-CFLAGS_string.o += $(PURGATORY_CFLAGS)
-
-CFLAGS_REMOVE_ctype.o += $(PURGATORY_CFLAGS_REMOVE)
-CFLAGS_ctype.o += $(PURGATORY_CFLAGS)
-
asflags-remove-y += $(foreach x, -g -gdwarf-4 -gdwarf-5, $(x) -Wa,$(x))

$(obj)/purgatory.ro: $(PURGATORY_OBJS) FORCE
diff --git a/arch/riscv/purgatory/purgatory.c b/arch/riscv/purgatory/purgatory.c
index 80596ab5fb62..1d30103d2047 100644
--- a/arch/riscv/purgatory/purgatory.c
+++ b/arch/riscv/purgatory/purgatory.c
@@ -22,14 +22,16 @@ static int verify_sha256_digest(void)
struct kexec_sha_region *ptr, *end;
struct sha256_state ss;
u8 digest[SHA256_DIGEST_SIZE];
+ int i;

sha256_init(&ss);
end = purgatory_sha_regions + ARRAY_SIZE(purgatory_sha_regions);
for (ptr = purgatory_sha_regions; ptr < end; ptr++)
sha256_update(&ss, (uint8_t *)(ptr->start), ptr->len);
sha256_final(&ss, digest);
- if (memcmp(digest, purgatory_sha256_digest, sizeof(digest)) != 0)
- return 1;
+ for (i = 0; i < SHA256_DIGEST_SIZE; ++i)
+ if (digest[i] != purgatory_sha256_digest[i])
+ return 1;
return 0;
}

--
2.25.1



2023-07-26 17:07:26

by Conor Dooley

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] riscv/purgatory: do not link with string.o and its dependencies

On Wed, Jul 26, 2023 at 11:54:00AM +0200, Petr Tesarik wrote:
> From: Petr Tesarik <[email protected]>
>
> Linking with this object file makes kexec_file_load(2) fail because of
> multiple unknown relocation types:
>
> - R_RISCV_ADD16, R_RISCV_SUB16: used by alternatives in strcmp()
> - R_RISCV_GOT_HI20: used to resolve _ctype in strcasecmp()
>
> All this hassle is needed for one single call to memcmp() from
> verify_sha256_digest() to compare 32 bytes of SHA256 checksum.
>
> Simply replace this memcmp() call with an explicit loop over those 32 bytes
> and remove the need to link with string.o and all the other object files
> that provide undefined symbols from that object file.
>
> Fixes: 838b3e28488f ("RISC-V: Load purgatory in kexec_file")
> Signed-off-by: Petr Tesarik <[email protected]>

This version keeps the automation happy,
Acked-by: Conor Dooley <[email protected]>

Thanks,
Conor.


Attachments:
(No filename) (962.00 B)
signature.asc (235.00 B)
Download all attachments

2023-08-03 15:36:43

by Conor Dooley

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] riscv/purgatory: do not link with string.o and its dependencies

On Thu, Aug 03, 2023 at 08:13:48AM -0700, Palmer Dabbelt wrote:
> On Wed, 26 Jul 2023 09:33:49 PDT (-0700), Conor Dooley wrote:
> > On Wed, Jul 26, 2023 at 11:54:00AM +0200, Petr Tesarik wrote:
> > > From: Petr Tesarik <[email protected]>
> > >
> > > Linking with this object file makes kexec_file_load(2) fail because of
> > > multiple unknown relocation types:
> > >
> > > - R_RISCV_ADD16, R_RISCV_SUB16: used by alternatives in strcmp()
> > > - R_RISCV_GOT_HI20: used to resolve _ctype in strcasecmp()
> > >
> > > All this hassle is needed for one single call to memcmp() from
> > > verify_sha256_digest() to compare 32 bytes of SHA256 checksum.
> > >
> > > Simply replace this memcmp() call with an explicit loop over those 32 bytes
> > > and remove the need to link with string.o and all the other object files
> > > that provide undefined symbols from that object file.
> > >
> > > Fixes: 838b3e28488f ("RISC-V: Load purgatory in kexec_file")
> > > Signed-off-by: Petr Tesarik <[email protected]>
> >
> > This version keeps the automation happy,
> > Acked-by: Conor Dooley <[email protected]>
>
> Oddly enough, this breaks my builds. I tried fixing the first one with
> something like

You say "tried", does that mean it also didn't work?
What's the config?

Cheers,
Conor.

>
> From 41c5a952f77e53bf4201296abff0132725aa19e6 Mon Sep 17 00:00:00 2001
> From: Palmer Dabbelt <[email protected]>
> Date: Wed, 2 Aug 2023 20:22:33 -0700
> Subject: [PATCH] RISC-V: Include io from timex
> Without this I get some implicit declarations.
> CC arch/riscv/kernel/asm-offsets.s
> In file included from linux/include/linux/timex.h:67,
> from linux/include/linux/time32.h:13,
> from linux/include/linux/time.h:60,
> from linux/include/linux/ktime.h:24,
> from linux/include/linux/timer.h:6,
> from linux/include/linux/workqueue.h:9,
> from linux/include/linux/mm_types.h:19,
> from linux/include/linux/mmzone.h:22,
> from linux/include/linux/gfp.h:7,
> from linux/include/linux/mm.h:7,
> from linux/arch/riscv/kernel/asm-offsets.c:10:
> linux/arch/riscv/include/asm/timex.h: In function 'get_cycles':
> linux/arch/riscv/include/asm/timex.h:25:16: error: implicit declaration of function 'readl_relaxed' [-Werror=implicit-function-declaration]
> 25 | return readl_relaxed(((u32 *)clint_time_val));
> |
> Signed-off-by: Palmer Dabbelt <[email protected]>
> ---
> arch/riscv/include/asm/timex.h | 1 +
> 1 file changed, 1 insertion(+)
> diff --git a/arch/riscv/include/asm/timex.h b/arch/riscv/include/asm/timex.h
> index a06697846e69..1a4d181193e0 100644
> --- a/arch/riscv/include/asm/timex.h
> +++ b/arch/riscv/include/asm/timex.h
> @@ -7,6 +7,7 @@
> #define _ASM_RISCV_TIMEX_H
> #include <asm/csr.h>
> +#include <asm/io.h>
> typedef unsigned long cycles_t;
>
> -- 2.41.0
>
> The other two look fine and are somewhat independent, so I've picked those up
> for fixes.
>
> Thanks!
>
> >
> > Thanks,
> > Conor.


Attachments:
(No filename) (3.25 kB)
signature.asc (235.00 B)
Download all attachments

2023-08-03 15:59:56

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] riscv/purgatory: do not link with string.o and its dependencies

On Wed, 26 Jul 2023 09:33:49 PDT (-0700), Conor Dooley wrote:
> On Wed, Jul 26, 2023 at 11:54:00AM +0200, Petr Tesarik wrote:
>> From: Petr Tesarik <[email protected]>
>>
>> Linking with this object file makes kexec_file_load(2) fail because of
>> multiple unknown relocation types:
>>
>> - R_RISCV_ADD16, R_RISCV_SUB16: used by alternatives in strcmp()
>> - R_RISCV_GOT_HI20: used to resolve _ctype in strcasecmp()
>>
>> All this hassle is needed for one single call to memcmp() from
>> verify_sha256_digest() to compare 32 bytes of SHA256 checksum.
>>
>> Simply replace this memcmp() call with an explicit loop over those 32 bytes
>> and remove the need to link with string.o and all the other object files
>> that provide undefined symbols from that object file.
>>
>> Fixes: 838b3e28488f ("RISC-V: Load purgatory in kexec_file")
>> Signed-off-by: Petr Tesarik <[email protected]>
>
> This version keeps the automation happy,
> Acked-by: Conor Dooley <[email protected]>

Oddly enough, this breaks my builds. I tried fixing the first one with
something like

From 41c5a952f77e53bf4201296abff0132725aa19e6 Mon Sep 17 00:00:00 2001
From: Palmer Dabbelt <[email protected]>
Date: Wed, 2 Aug 2023 20:22:33 -0700
Subject: [PATCH] RISC-V: Include io from timex

Without this I get some implicit declarations.

CC arch/riscv/kernel/asm-offsets.s
In file included from linux/include/linux/timex.h:67,
from linux/include/linux/time32.h:13,
from linux/include/linux/time.h:60,
from linux/include/linux/ktime.h:24,
from linux/include/linux/timer.h:6,
from linux/include/linux/workqueue.h:9,
from linux/include/linux/mm_types.h:19,
from linux/include/linux/mmzone.h:22,
from linux/include/linux/gfp.h:7,
from linux/include/linux/mm.h:7,
from linux/arch/riscv/kernel/asm-offsets.c:10:
linux/arch/riscv/include/asm/timex.h: In function 'get_cycles':
linux/arch/riscv/include/asm/timex.h:25:16: error: implicit declaration of function 'readl_relaxed' [-Werror=implicit-function-declaration]
25 | return readl_relaxed(((u32 *)clint_time_val));
|

Signed-off-by: Palmer Dabbelt <[email protected]>
---
arch/riscv/include/asm/timex.h | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/riscv/include/asm/timex.h b/arch/riscv/include/asm/timex.h
index a06697846e69..1a4d181193e0 100644
--- a/arch/riscv/include/asm/timex.h
+++ b/arch/riscv/include/asm/timex.h
@@ -7,6 +7,7 @@
#define _ASM_RISCV_TIMEX_H

#include <asm/csr.h>
+#include <asm/io.h>

typedef unsigned long cycles_t;

--
2.41.0

The other two look fine and are somewhat independent, so I've picked those up
for fixes.

Thanks!

>
> Thanks,
> Conor.

2023-08-03 16:11:35

by Petr Tesarik

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] riscv/purgatory: do not link with string.o and its dependencies

On 8/3/2023 5:13 PM, Palmer Dabbelt wrote:
> On Wed, 26 Jul 2023 09:33:49 PDT (-0700), Conor Dooley wrote:
>> On Wed, Jul 26, 2023 at 11:54:00AM +0200, Petr Tesarik wrote:
>>> From: Petr Tesarik <[email protected]>
>>>
>>> Linking with this object file makes kexec_file_load(2) fail because of
>>> multiple unknown relocation types:
>>>
>>> - R_RISCV_ADD16, R_RISCV_SUB16: used by alternatives in strcmp()
>>> - R_RISCV_GOT_HI20: used to resolve _ctype in strcasecmp()
>>>
>>> All this hassle is needed for one single call to memcmp() from
>>> verify_sha256_digest() to compare 32 bytes of SHA256 checksum.
>>>
>>> Simply replace this memcmp() call with an explicit loop over those 32
>>> bytes
>>> and remove the need to link with string.o and all the other object files
>>> that provide undefined symbols from that object file.
>>>
>>> Fixes: 838b3e28488f ("RISC-V: Load purgatory in kexec_file")
>>> Signed-off-by: Petr Tesarik <[email protected]>
>>
>> This version keeps the automation happy,
>> Acked-by: Conor Dooley <[email protected]>
>
> Oddly enough, this breaks my builds.  I tried fixing the first one with
> something like

FTR I have no idea how a patch to the purgatory code and its Makefile
can have any effect on the compilation of asm-offsets.s in another
directory.

Petr T

>    From 41c5a952f77e53bf4201296abff0132725aa19e6 Mon Sep 17 00:00:00 2001
>    From: Palmer Dabbelt <[email protected]>
>    Date: Wed, 2 Aug 2023 20:22:33 -0700
>    Subject: [PATCH] RISC-V: Include io from timex
>       Without this I get some implicit declarations.
>         CC      arch/riscv/kernel/asm-offsets.s
>    In file included from linux/include/linux/timex.h:67,
>                     from linux/include/linux/time32.h:13,
>                     from linux/include/linux/time.h:60,
>                     from linux/include/linux/ktime.h:24,
>                     from linux/include/linux/timer.h:6,
>                     from linux/include/linux/workqueue.h:9,
>                     from linux/include/linux/mm_types.h:19,
>                     from linux/include/linux/mmzone.h:22,
>                     from linux/include/linux/gfp.h:7,
>                     from linux/include/linux/mm.h:7,
>                     from linux/arch/riscv/kernel/asm-offsets.c:10:
>    linux/arch/riscv/include/asm/timex.h: In function 'get_cycles':
>    linux/arch/riscv/include/asm/timex.h:25:16: error: implicit
> declaration of function 'readl_relaxed'
> [-Werror=implicit-function-declaration]
>       25 |         return readl_relaxed(((u32 *)clint_time_val));
>          |
>       Signed-off-by: Palmer Dabbelt <[email protected]>
>    ---
>     arch/riscv/include/asm/timex.h | 1 +
>     1 file changed, 1 insertion(+)
>       diff --git a/arch/riscv/include/asm/timex.h
> b/arch/riscv/include/asm/timex.h
>    index a06697846e69..1a4d181193e0 100644
>    --- a/arch/riscv/include/asm/timex.h
>    +++ b/arch/riscv/include/asm/timex.h
>    @@ -7,6 +7,7 @@
>     #define _ASM_RISCV_TIMEX_H
>         #include <asm/csr.h>
>    +#include <asm/io.h>
>         typedef unsigned long cycles_t;
>
>    --    2.41.0
>
> The other two look fine and are somewhat independent, so I've picked
> those up
> for fixes.
>
> Thanks!
>
>>
>> Thanks,
>> Conor.