2021-04-17 01:12:03

by Damien Le Moal

[permalink] [raw]
Subject: [PATCH v4 0/2] Fix binfmt_flat loader for RISC-V

RISC-V NOMMU flat binaries cannot tolerate a gap between the text and
data section as the toolchain fully resolves at compile time the PC
relative global pointer (__global_pointer$ value loaded in the gp
register). Without a relocation entry provided, the flat bin loader
cannot fix the value if a gap is introduced and user executables fail
to run.

This series fixes this problem by allowing an architecture to request
the flat loader to suppress the offset of the data start section.
Combined with the use of elf2flt "-r" option to mark the flat
executables with the FLAT_FLAG_RAM flag, the text and data sections are
loaded contiguously in memory, without a change in their relative
position from compile time.

The first patch fixes binfmt_flat flat_load_file() using the new
configuration option CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. The
second patch enables this new option for RISCV NOMMU builds.

These patches do not change the binfmt_flat loader behavior for other
architectures.

Changes from v3:
* Renamed the configuration option from
CONFIG_BINFMT_FLAT_NO_TEXT_DATA_GAP to
CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to clarify that only the
offseting of the data section start is suppressed.
* Do not force loding to RAM (contiguously) if the flat binary does not
have the FLAT_FLAG_RAM flag set.
* Updated commit messages to reflect above changes.

Changes from v2:
* Updated distribution list
* Added Palmer ack-by tag

Changes from v1:
* Replace FLAT_TEXT_DATA_NO_GAP macro with
CONFIG_BINFMT_FLAT_NO_TEXT_DATA_GAP config option (patch 1).
* Remove the addition of riscv/include/asm/flat.h and set
CONFIG_BINFMT_FLAT_NO_TEXT_DATA_GAP for RISCV and !MMU

Damien Le Moal (2):
binfmt_flat: allow not offsetting data start
riscv: Disable data start offset in flat binaries

arch/riscv/Kconfig | 1 +
fs/Kconfig.binfmt | 3 +++
fs/binfmt_flat.c | 19 ++++++++++++++-----
3 files changed, 18 insertions(+), 5 deletions(-)

--
2.30.2


2021-04-17 01:13:25

by Damien Le Moal

[permalink] [raw]
Subject: [PATCH v4 2/2] riscv: Disable data start offset in flat binaries

uclibc/gcc combined with elf2flt riscv linker file fully resolve the
PC relative __global_pointer$ value at compile time and do not generate
a relocation entry to set a correct value of the gp register at runtime.
As a result, if the flatbin loader offsets the start of the data
section, the relative position change between the text and data sections
compared to the compile time positions results in an incorrect gp value
being used. This causes flatbin executables to crash.

Avoid this problem by enabling CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
automatically when CONFIG_RISCV is enabled and CONFIG_MMU is disabled.

Signed-off-by: Damien Le Moal <[email protected]>
Acked-by: Palmer Dabbelt <[email protected]>
---
arch/riscv/Kconfig | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 4515a10c5d22..add528eb9235 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -33,6 +33,7 @@ config RISCV
select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
select ARCH_WANT_FRAME_POINTERS
select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
+ select BINFMT_FLAT_NO_DATA_START_OFFSET if !MMU
select CLONE_BACKWARDS
select CLINT_TIMER if !MMU
select COMMON_CLK
--
2.30.2

2021-04-17 04:58:25

by Greg Ungerer

[permalink] [raw]
Subject: Re: [PATCH v4 2/2] riscv: Disable data start offset in flat binaries


On 17/4/21 11:10 am, Damien Le Moal wrote:
> uclibc/gcc combined with elf2flt riscv linker file fully resolve the
> PC relative __global_pointer$ value at compile time and do not generate
> a relocation entry to set a correct value of the gp register at runtime.
> As a result, if the flatbin loader offsets the start of the data
> section, the relative position change between the text and data sections
> compared to the compile time positions results in an incorrect gp value
> being used. This causes flatbin executables to crash.
>
> Avoid this problem by enabling CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
> automatically when CONFIG_RISCV is enabled and CONFIG_MMU is disabled.
>
> Signed-off-by: Damien Le Moal <[email protected]>
> Acked-by: Palmer Dabbelt <[email protected]>

Acked-by: Greg Ungerer <[email protected]>

Palmer do you want me to take this via my tree with 1/2 in the series,
or are you going to pick it up?

Regards
Greg


> ---
> arch/riscv/Kconfig | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> index 4515a10c5d22..add528eb9235 100644
> --- a/arch/riscv/Kconfig
> +++ b/arch/riscv/Kconfig
> @@ -33,6 +33,7 @@ config RISCV
> select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
> select ARCH_WANT_FRAME_POINTERS
> select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
> + select BINFMT_FLAT_NO_DATA_START_OFFSET if !MMU
> select CLONE_BACKWARDS
> select CLINT_TIMER if !MMU
> select COMMON_CLK
>