[PATCH v4 2/2] lib: sbi: Fix compile errors using -Os option

Anup Patel anup at brainfault.org
Sat Dec 11 04:18:32 PST 2021


On Fri, Dec 3, 2021 at 10:34 AM Anup Patel <anup.patel at wdc.com> wrote:
>
> When compiling with -Os option along with -ffreestanding, both GCC
> and clang will add implicit calls to memcpy() and memset() for stack
> variables initialized in declaration.
>
> The C standard as per Clause 4, the compiler cannot necessarily
> assume that anything beyond:
>
>  * float.h
>  * iso646.h
>  * limits.h
>  * stdalign.h
>  * stdarg.h
>  * stdbool.h
>  * stddef.h
>  * stdint.h
>  * stdnoreturn.h
>  * fenv.h
>  * math.h
>  * and the numeric conversion functions of stdlib.h.
>
> This patch maps memcpy() and memset() as weak-alias of sbi_memcpy()
> and sbi_memset() respectively so that implicit calls to memcpy()
> and memset() will compile properly.
>
> Signed-off-by: Anup Patel <anup.patel at wdc.com>
> Reviewed-by: Dong Du <Dd_nirvana at sjtu.edu.cn>
> Reviewed-by: Xiang W <wxjstz at 126.com>

Applied this patch to the riscv/opensbi repo

Regards,
Anup

> ---
>  lib/sbi/sbi_string.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/lib/sbi/sbi_string.c b/lib/sbi/sbi_string.c
> index c87bce9..c67c02e 100644
> --- a/lib/sbi/sbi_string.c
> +++ b/lib/sbi/sbi_string.c
> @@ -122,6 +122,9 @@ void *sbi_memset(void *s, int c, size_t count)
>         return s;
>  }
>
> +void *memset(void *s, int c, size_t count) \
> +__attribute__((weak, alias("sbi_memset")));
> +
>  void *sbi_memcpy(void *dest, const void *src, size_t count)
>  {
>         char *temp1       = dest;
> @@ -135,6 +138,9 @@ void *sbi_memcpy(void *dest, const void *src, size_t count)
>         return dest;
>  }
>
> +void *memcpy(void *dest, const void *src, size_t count) \
> +__attribute__((weak, alias("sbi_memcpy")));
> +
>  void *sbi_memmove(void *dest, const void *src, size_t count)
>  {
>         char *temp1       = (char *)dest;
> --
> 2.25.1
>



More information about the opensbi mailing list