Skip to contentSkip to conversation

deal with GOT stuff for RISC-V

PATCH Superseded Oleksii Kurochko 4 participants 13 messages Mar 16, 2023 – Apr 19, 2023 RISC-V

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover deal with GOT stuff for RISC-V

The patch series introduces things to deal with GOT stuff whichwas faced during the work on [1].

Initially, the issue was with 'la' pseudo instruction, which transformed to 'aupic/l{w|d} GOT' instead of 'auipc/addi'.
The transformation dependson .option {nopic, pic} directive or compiler flags.

Right now, 'la' transforms to 'auipc/l{w|d}', which in case of cpu0_boot_stack[] will lead to the usage of _GLOBAL_OFFSET_TABLE_and addresses inside GOT sections will be relative to linker time addresses.

At least there are two reasons for that:
1. GCC compiler used in RISCV64 container is compiled with --enable-default-pie flag.
2. GCC spec file for the RISC-V architecture by default enabled -fpic:
[user@49295ae49cbe build]$ riscv64-linux-gnu-gcc -dumpspecs | grep -i pic

    --traditional-format %(subtarget_asm_debugging_spec) %{fno-pie|fno- PIE|fno-pic|fno-PIC:;:-fpic} %{march=*} %{mabi=*} %{mno-relax} %{mbig- endian} %{mlittle-endian} %(subtarget_asm_spec)%{misa-spec=*}

which means that -fpic is enabled if none of the following options are present on the command line: fno-pie, -fno-PIE, -fno-pic, -fno-PIC

To resolve that, it was added EMBEDDED_EXTRA_CFLAGS to RISCV's CFLAGS as it was done for other architecture.

To catch use cases when GOT things will be produced by something was createthe second patch of the patch series:
[xen/riscv: add explicit check that.got{.plt} is empty] which add .got&.got.plt sections to xen.lds.S and alsoadds asserts to check that the mentioned sections are empty otherwise, it will be produced a compilation error with the message that the sections aren't empty.

[1]:
https://lore.kernel.org/xen-devel/[email protected]/T/#t

---
Changes in V2:
* The patch [1] was refactored and lead to the patch [xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS].
* In addition to the patch [1] was created another patch [xen/riscv: add explicit check that .got{.plt} is empty] to be sure that .got{.plt} sections weren't produced.
---
Oleksii Kurochko (2):
xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS
xen/riscv: add explicit check that .got{.plt} is empty

 xen/arch/riscv/arch.mk   |  2 ++
 xen/arch/riscv/xen.lds.S | 13 +++++++++++++
 2 files changed, 15 insertions(+)
Signature · 1 line
2.39.2
#1 lore raw

PATCH v2 1/2 xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS

The patch is needed to keep all address of cpu0_boot_stack PC-relative.

Pseudoinstruction 'la' can be transformed to 'auipc/addi' or 'auipc/l{w|d}'. It depends on the .option directive: nopic and pic or compiler flags.

Right now, 'la' transforms to 'auipc/l{w|d}', which in case of cpu0_boot_stack[] will lead to the usage of _GLOBAL_OFFSET_TABLE_ where all addresses will be without counting that it might happen that linker address != load address ( so addresses inside got sections will be relative to linker time ).

It happens becuase the compiler from riscv64 docker compiled with --enable-default-pie:
[user@49295ae49cbe build]$ riscv64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=riscv64-linux-gnu-gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/riscv64-linux-gnu/12.2.0/lto-wrapper
Target: riscv64-linux-gnu
Configured with: /build/riscv64-linux-gnu-gcc/src/gcc-12.2.0/configure --prefix=/usr --program-prefix=riscv64-linux-gnu- --with-local- prefix=/usr/riscv64-linux-gnu --with-sysroot=/usr/riscv64-linux-gnu -- with-build-sysroot=/usr/riscv64-linux-gnu --libdir=/usr/lib -- libexecdir=/usr/lib --target=riscv64-linux-gnu --host=x86_64-pc-linux- gnu --build=x86_64-pc-linux-gnu --with-system-zlib --with-isl --with- linker-hash-style=gnu --disable-nls --disable-libunwind-exceptions -- disable-libstdcxx-pch --disable-libssp --disable-multilib --disable- werror --enable-languages=c,c++ --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-gnu-unique-object - -enable-linker-build-id --enable-lto --enable-plugin --enable-install- libiberty --enable-gnu-indirect-function --enable-default-pie --enable- checking=release
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (GCC)

Looking at gcc spec file for the RISC-V architecture:
[user@49295ae49cbe build]$ riscv64-linux-gnu-gcc -dumpspecs | grep -i pic
--traditional-format %(subtarget_asm_debugging_spec) %{fno-pie|fno- PIE|fno-pic|fno-PIC:;:-fpic} %{march=*} %{mabi=*} %{mno-relax} %{mbig- endian} %{mlittle-endian} %(subtarget_asm_spec)%{misa-spec=*}
which means that -fpic is enabled if none of the following options are present on the command line:
-fno-pie
-fno-PIE
-fno-pic
-fno-PIC

That's the reasons why 'la' is transformed to 'aupic/l{w|d} GOT' and not be dependent on the toolchain used.

---
Changes in V2:
* instead of changing 'la' to 'lla' to keep cpu0_boot_stack PC-relative it was updated CFLAGS with EMBEDDED_EXTRA_CFLAGS which contains -fno-PIE thereby 'la' will be transformed to 'auipc/addi' without GOT usage.
* update the commit message with additional details.

---
 xen/arch/riscv/arch.mk | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/riscv/arch.mk +2 -0
index 45fe858ee0..7448f759b4 100644
@@ -1,6 +1,8 @@
 ########################################
 # RISCV-specific definitions
 
+$(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS))
+
 CFLAGS-$(CONFIG_RISCV_64) += -mabi=lp64
 
 riscv-march-$(CONFIG_RISCV_ISA_RV64G) := rv64g
Signature · 1 line
2.39.2
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS

Quoted 50 lines · Oleksii Kurochko

The patch is needed to keep all address of cpu0_boot_stack PC-relative.

Pseudoinstruction 'la' can be transformed to 'auipc/addi' or 'auipc/l{w|d}'. It depends on the .option directive: nopic and pic or compiler flags.

Right now, 'la' transforms to 'auipc/l{w|d}', which in case of cpu0_boot_stack[] will lead to the usage of _GLOBAL_OFFSET_TABLE_ where all addresses will be without counting that it might happen that linker address != load address ( so addresses inside got sections will be relative to linker time ).

It happens becuase the compiler from riscv64 docker compiled with --enable-default-pie:
[user@49295ae49cbe build]$ riscv64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=riscv64-linux-gnu-gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/riscv64-linux-gnu/12.2.0/lto-wrapper
Target: riscv64-linux-gnu
Configured with: /build/riscv64-linux-gnu-gcc/src/gcc-12.2.0/configure --prefix=/usr --program-prefix=riscv64-linux-gnu- --with-local- prefix=/usr/riscv64-linux-gnu --with-sysroot=/usr/riscv64-linux-gnu -- with-build-sysroot=/usr/riscv64-linux-gnu --libdir=/usr/lib -- libexecdir=/usr/lib --target=riscv64-linux-gnu --host=x86_64-pc-linux- gnu --build=x86_64-pc-linux-gnu --with-system-zlib --with-isl --with- linker-hash-style=gnu --disable-nls --disable-libunwind-exceptions -- disable-libstdcxx-pch --disable-libssp --disable-multilib --disable- werror --enable-languages=c,c++ --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-gnu-unique-object - -enable-linker-build-id --enable-lto --enable-plugin --enable-install- libiberty --enable-gnu-indirect-function --enable-default-pie --enable- checking=release
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (GCC)

Looking at gcc spec file for the RISC-V architecture:
[user@49295ae49cbe build]$ riscv64-linux-gnu-gcc -dumpspecs | grep -i pic
--traditional-format %(subtarget_asm_debugging_spec) %{fno-pie|fno- PIE|fno-pic|fno-PIC:;:-fpic} %{march=*} %{mabi=*} %{mno-relax} %{mbig- endian} %{mlittle-endian} %(subtarget_asm_spec)%{misa-spec=*}
which means that -fpic is enabled if none of the following options are present on the command line:
-fno-pie
-fno-PIE
-fno-pic
-fno-PIC

Quoted 4 lines

That's the reasons why 'la' is transformed to 'aupic/l{w|d} GOT' and not be dependent on the toolchain used.

There may also want to be Suggested-by to both Andrew and me.

Jan

Acked-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS

In reply to Oleksii Kurochko
Quoted 51 lines · Oleksii Kurochko

The patch is needed to keep all address of cpu0_boot_stack PC-relative.

Pseudoinstruction 'la' can be transformed to 'auipc/addi' or 'auipc/l{w|d}'. It depends on the .option directive: nopic and pic or compiler flags.

Right now, 'la' transforms to 'auipc/l{w|d}', which in case of cpu0_boot_stack[] will lead to the usage of _GLOBAL_OFFSET_TABLE_ where all addresses will be without counting that it might happen that linker address != load address ( so addresses inside got sections will be relative to linker time ).

It happens becuase the compiler from riscv64 docker compiled with --enable-default-pie:
[user@49295ae49cbe build]$ riscv64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=riscv64-linux-gnu-gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/riscv64-linux-gnu/12.2.0/lto-wrapper
Target: riscv64-linux-gnu
Configured with: /build/riscv64-linux-gnu-gcc/src/gcc-12.2.0/configure --prefix=/usr --program-prefix=riscv64-linux-gnu- --with-local- prefix=/usr/riscv64-linux-gnu --with-sysroot=/usr/riscv64-linux-gnu -- with-build-sysroot=/usr/riscv64-linux-gnu --libdir=/usr/lib -- libexecdir=/usr/lib --target=riscv64-linux-gnu --host=x86_64-pc-linux- gnu --build=x86_64-pc-linux-gnu --with-system-zlib --with-isl --with- linker-hash-style=gnu --disable-nls --disable-libunwind-exceptions -- disable-libstdcxx-pch --disable-libssp --disable-multilib --disable- werror --enable-languages=c,c++ --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-gnu-unique-object - -enable-linker-build-id --enable-lto --enable-plugin --enable-install- libiberty --enable-gnu-indirect-function --enable-default-pie --enable- checking=release
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (GCC)

Looking at gcc spec file for the RISC-V architecture:
[user@49295ae49cbe build]$ riscv64-linux-gnu-gcc -dumpspecs | grep -i pic
--traditional-format %(subtarget_asm_debugging_spec) %{fno-pie|fno- PIE|fno-pic|fno-PIC:;:-fpic} %{march=*} %{mabi=*} %{mno-relax} %{mbig- endian} %{mlittle-endian} %(subtarget_asm_spec)%{misa-spec=*}
which means that -fpic is enabled if none of the following options are present on the command line:
-fno-pie
-fno-PIE
-fno-pic
-fno-PIC

Quoted 4 lines

That's the reasons why 'la' is transformed to 'aupic/l{w|d} GOT' and not be dependent on the toolchain used.

Alistair

Quoted 28 lines

---
Changes in V2:
* instead of changing 'la' to 'lla' to keep cpu0_boot_stack PC-relative it was updated CFLAGS with EMBEDDED_EXTRA_CFLAGS which contains -fno-PIE thereby 'la' will be transformed to 'auipc/addi' without GOT usage.
* update the commit message with additional details.

---
 xen/arch/riscv/arch.mk | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/riscv/arch.mk +2 -1
index 45fe858ee0..7448f759b4 100644
@@ -1,6 +1,8 @@
 ########################################
 # RISCV-specific definitions
 
+$(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS))
+
 CFLAGS-$(CONFIG_RISCV_64) += -mabi=lp64
 
 riscv-march-$(CONFIG_RISCV_ISA_RV64G) := rv64g
--

2.39.2

#4 lore raw

PATCH v2 2/2 xen/riscv: add explicit check that .got{.plt} is empty

In reply to Oleksii Kurochko

The GOT sections usage should be avoided in the hypervisor so to catch such use cases earlier when GOT things are produced the patch introduces .got and .got.plt sections and adds asserts that they're empty.

The sections won't be created until they remain empty otherwise the asserts would cause early failure.

---
Changes in V2:
* the patch was introduced in patch series v2.

---
 xen/arch/riscv/xen.lds.S | 13 +++++++++++++
 1 file changed, 13 insertions(+)
xen/arch/riscv/xen.lds.S +13 -0
index ca57cce75c..f299ea8422 100644
@@ -1,3 +1,4 @@
+#include <xen/lib.h>
 #include <xen/xen.lds.h>
 
 #undef ENTRY
@@ -123,6 +124,15 @@ SECTIONS
         *(SORT(.init_array.*))
         __ctors_end = .;
     } :text
+
+    .got : {
+        *(.got)
+    } : text
+
+    .got.plt : {
+        *(.got.plt)
+    } : text
+
     . = ALIGN(POINTER_ALIGN);
     __init_end = .;
 
@@ -156,3 +166,6 @@ SECTIONS
 
     ELF_DETAILS_SECTIONS
 }
+
+ASSERT(!SIZEOF(.got),      ".got non-empty")
+ASSERT(!SIZEOF(.got.plt),  ".got.plt non-empty")
Signature · 1 line
2.39.2
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] xen/riscv: add explicit check that .got{.plt} is empty

Quoted 9 lines · Oleksii Kurochko

The GOT sections usage should be avoided in the hypervisor so to catch such use cases earlier when GOT things are produced the patch introduces .got and .got.plt sections and adds asserts that they're empty.

The sections won't be created until they remain empty otherwise the asserts would cause early failure.

And again a Suggested-by to Andrew perhaps.

Jan

Acked-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] xen/riscv: add explicit check that .got{.plt} is empty

In reply to Oleksii Kurochko
Quoted 10 lines · Oleksii Kurochko

The GOT sections usage should be avoided in the hypervisor so to catch such use cases earlier when GOT things are produced the patch introduces .got and .got.plt sections and adds asserts that they're empty.

The sections won't be created until they remain empty otherwise the asserts would cause early failure.

Alistair

Quoted 43 lines

---
Changes in V2:
* the patch was introduced in patch series v2.

---
 xen/arch/riscv/xen.lds.S | 13 +++++++++++++
 1 file changed, 13 insertions(+)
xen/arch/riscv/xen.lds.S +13 -1
index ca57cce75c..f299ea8422 100644
@@ -1,3 +1,4 @@
+#include <xen/lib.h>
 #include <xen/xen.lds.h>
 
 #undef ENTRY
@@ -123,6 +124,15 @@ SECTIONS
         *(SORT(.init_array.*))
         __ctors_end = .;
     } :text
+
+    .got : {
+        *(.got)
+    } : text
+
+    .got.plt : {
+        *(.got.plt)
+    } : text
+
     . = ALIGN(POINTER_ALIGN);
     __init_end = .;
 
@@ -156,3 +166,6 @@ SECTIONS
 
     ELF_DETAILS_SECTIONS
 }
+
+ASSERT(!SIZEOF(.got),      ".got non-empty")
+ASSERT(!SIZEOF(.got.plt),  ".got.plt non-empty")
--

2.39.2

#8 lore raw
Quoted 6 lines · Andrew Cooper
Quoted 3 lines · Oleksii Kurochko

Oleksii Kurochko (2):
xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS xen/riscv: add explicit check that .got{.plt} is empty

I'm sorry, I failed to apply this ack while committing.

Jan

#9 lore raw
Quoted 7 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper
Quoted 3 lines · Oleksii Kurochko

Oleksii Kurochko (2):
xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS xen/riscv: add explicit check that .got{.plt} is empty

I'm sorry, I failed to apply this ack while committing.

Oh well.  The important thing is that it's in now.

~Andrew

#10 lore raw
In reply to Oleksii Kurochko
Quoted 7 lines · Oleksii Kurochko

Oleksii Kurochko (2):
xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS xen/riscv: add explicit check that .got{.plt} is empty

 xen/arch/riscv/arch.mk   |  2 ++
 xen/arch/riscv/xen.lds.S | 13 +++++++++++++
 2 files changed, 15 insertions(+)

Just to mention it in case you aren't aware: Hunting down the necessary acks is your responsibility, not one of the committers. You may want to ping Bob and Alistair (unless this response of mine is already enough of a ping). Provided of course the patches still apply as-is ...

Jan

author
#11 lore raw
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Oleksii Kurochko

Oleksii Kurochko (2):
  xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS xen/riscv: add explicit check that .got{.plt} is empty

 xen/arch/riscv/arch.mk   |  2 ++
 xen/arch/riscv/xen.lds.S | 13 +++++++++++++

 2 files changed, 15 insertions(+)
Quoted 8 lines

Just to mention it in case you aren't aware: Hunting down the necessary acks
is your responsibility, not one of the committers. You may want to ping Bob
and Alistair (unless this response of mine is already enough of a ping).
Provided of course the patches still apply as-is ...

Thanks. I'll take that into account.

I thought the only option I have wait for a response from a maintainer.

~ Oleksii

#12 lore raw
Quoted 18 lines · Oleksii
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Oleksii Kurochko

Oleksii Kurochko (2):
  xen/riscv: add EMBEDDED_EXTRA_CFLAGS to CFLAGS xen/riscv: add explicit check that .got{.plt} is empty

 xen/arch/riscv/arch.mk   |  2 ++
 xen/arch/riscv/xen.lds.S | 13 +++++++++++++

 2 files changed, 15 insertions(+)
Quoted 8 lines

Just to mention it in case you aren't aware: Hunting down the necessary acks
is your responsibility, not one of the committers. You may want to ping Bob
and Alistair (unless this response of mine is already enough of a ping).
Provided of course the patches still apply as-is ...

Quoted 3 lines

Thanks. I'll take that into account.

I thought the only option I have wait for a response from a maintainer.

Well, in principle yes. But pinging is appropriate after a reasonable amount of time (rule of thumb: a week, maybe two, depending on e.g. complexity).

Jan