From db7c38aebd531f7e964d0bb97a439b2a26dcf86c Mon Sep 17 00:00:00 2001 From: Kito Cheng Date: Thu, 26 Sep 2024 17:46:39 +0800 Subject: [PATCH 1/6] Introduce new relocation for landing pad The R_RISCV_LPAD relocation can be used for PLT entry generation and also for linker relaxation. Additionally, we defined a new mapping symbol type to help users understand the function signature for the corresponding function. The addend value is the label value, and it will point to the mapping symbol placed at the beginning of the function. e.g. ```asm foo: # void foo(void) $sFvvE: lpad 123 # R_RISCV_LPAD $sFvvE + 123 ``` We propose two linker relaxations for the landing pad. The first is removing the entire landing pad, which can be used when symbols have local visibility, and the address is not taken by any other reference. The second is a landing pad scheme conversion, designed for backward compatibility (or as a workaround) for legacy programs that may use functions without declarations. --- riscv-elf.adoc | 84 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/riscv-elf.adoc b/riscv-elf.adoc index edcd1e0..2d67299 100644 --- a/riscv-elf.adoc +++ b/riscv-elf.adoc @@ -548,7 +548,9 @@ Description:: Additional information about the relocation <| S - P .2+| 65 .2+| TLSDESC_CALL .2+| Static | .2+| Annotate call to TLS descriptor resolver function, `%tlsdesc_call(address of %tlsdesc_hi)`, for relaxation purposes only <| -.2+| 66-190 .2+| *Reserved* .2+| - | .2+| Reserved for future standard use +.2+| 66 .2+| LPAD .2+| Static | .2+| Annotates the landing pad instruction inserted at the beginning of the function. The addend indicates the label value of the landing pad, and the symbol value is the address of the mapping symbol for the function signature, which will have the same address as the function. + <| +.2+| 67-190 .2+| *Reserved* .2+| - | .2+| Reserved for future standard use <| .2+| 191 .2+| VENDOR .2+| Static | .2+| Paired with a vendor-specific relocation and must be placed immediately before it, indicates which vendor owns the relocation. <| @@ -1582,6 +1584,7 @@ A number of symbols, named mapping symbols, describe the boundaries. | $x. | $x .2+| Start of a sequence of instructions with extension. | $x. +| $s | Marker for the landing pad instruction. This should only be used with the function signature-based scheme and should be placed only at the beginning of the function. |=== The mapping symbol should set the type to `STT_NOTYPE`, binding to `STB_LOCAL`, @@ -2317,6 +2320,85 @@ instructions. It is recommended to initialize `jvt` CSR immediately after csrw jvt, a0 ---- +==== Landing Pad Relaxation + + Target Relocation::: R_RISCV_LPAD + + Description:: This relaxation type can relax lpad instruction into a none, + which removed the lpad instruciton. + This relaxation type can be performe even without `R_RISCV_RELAX`, + but the linker should pad nop instruciton to the same length of the original + instruction sequence. + + Condition:: The associated function of this lpad must have local visibility, and + it must not be referenced by any relocation other than `R_RISCV_CALL` and + `R_RISCV_CALL_PLT`. + This relaxation can also be performed when the function has global visibility, + if the symbol does not have a corresponding PLT entry and is not referenced by + the GOT or by any relocation other than `R_RISCV_CALL` and `R_RISCV_CALL_PLT`. + + Relaxation:: + - Lpad instruciton associated with `R_RISCV_LPAD` can be removed. + - Lpad instruciton associated with `R_RISCV_LPAD` can be replaced with nop + instruction if the relacation isn't paired with `R_RISCV_RELAX`. + + Example:: ++ +-- +Relaxation candidate: +[,asm] +---- + lpad 0x123 # R_RISCV_LPAD, R_RISCV_RELAX +---- + +Relaxation result: +[,asm] +---- + # No instruction +---- +Can be relaxed into `nop` if no `R_RISCV_RELAX` is paired with `R_RISCV_LPAD`. +[,asm] +---- + nop +---- +-- + +==== Landing Pad Scheme Relaxation + + Target Relocation::: R_RISCV_LPAD + + Description:: This relaxation type allows an `lpad` instruction to be relaxed + into `lpad 0`, which is a universal landing pad that ignores the label value + comparison. This relaxation is used when the label value is not computed + correctly. + + Condition:: This relaxation can be performed without `R_RISCV_RELAX`, and + should not be enabled by default. The user must explicitly enable this + relaxation, and it should only be applied during static linking. + + Relaxation:: + - Lpad instruction associated with `R_RISCV_LPAD` will be replaced with + `lpad 0`. + + Example:: ++ +-- +Relaxation candidate: +[,asm] +---- + lpad 0x123 # R_RISCV_LPAD +---- + +Relaxation result: +[,asm] +---- + lpad 0 +---- +-- + +NOTE: This relaxation is designed to be compatible with legacy programs that + may not declare the function signature correctly. + [bibliography] == References From 0726ba119c834d460cbab96a0f2d06b67dc51916 Mon Sep 17 00:00:00 2001 From: Kito Cheng Date: Fri, 18 Oct 2024 16:36:21 +0800 Subject: [PATCH 2/6] Fix typo --- riscv-elf.adoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/riscv-elf.adoc b/riscv-elf.adoc index 2d67299..9d574da 100644 --- a/riscv-elf.adoc +++ b/riscv-elf.adoc @@ -2011,7 +2011,7 @@ NOTE: Tag_RISCV_x3_reg_usage is treated as 0 if it is not present. Relaxation:: - The `auipc` instruction associated with `R_RISCV_GOT_HI20` can be - removed if the symbol is absolute. + removes if the symbol is absolute. - The instruction or instructions associated with `R_RISCV_PCREL_LO12_I` can be rewritten to either `c.li` or `addi` to materialize the symbol's @@ -2338,8 +2338,8 @@ instructions. It is recommended to initialize `jvt` CSR immediately after the GOT or by any relocation other than `R_RISCV_CALL` and `R_RISCV_CALL_PLT`. Relaxation:: - - Lpad instruciton associated with `R_RISCV_LPAD` can be removed. - - Lpad instruciton associated with `R_RISCV_LPAD` can be replaced with nop + - Lpad instruction associated with `R_RISCV_LPAD` can be removed. + - Lpad instruction associated with `R_RISCV_LPAD` can be replaced with nop instruction if the relacation isn't paired with `R_RISCV_RELAX`. Example:: From 1e21e4257699276ae52aaf90ecceece8effe75c8 Mon Sep 17 00:00:00 2001 From: Kito Cheng Date: Fri, 18 Oct 2024 16:37:01 +0800 Subject: [PATCH 3/6] Revise 'Landing Pad Relaxation' Rephase to make it clearly about it can remove instruction. --- riscv-elf.adoc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/riscv-elf.adoc b/riscv-elf.adoc index 9d574da..138b82b 100644 --- a/riscv-elf.adoc +++ b/riscv-elf.adoc @@ -2324,10 +2324,15 @@ instructions. It is recommended to initialize `jvt` CSR immediately after Target Relocation::: R_RISCV_LPAD + Description:: This relaxation type allows the `lpad` instruction to be removed. + However, if `R_RISCV_RELAX` is not present, the `lpad` instruction can only be + replaced with a sequence of `nop` instructions of the same length as the + original instruction. + Description:: This relaxation type can relax lpad instruction into a none, - which removed the lpad instruciton. - This relaxation type can be performe even without `R_RISCV_RELAX`, - but the linker should pad nop instruciton to the same length of the original + which removed the lpad instruction. + This relaxation type can be performed even without `R_RISCV_RELAX`, + but the linker should pad nop instruction to the same length of the original instruction sequence. Condition:: The associated function of this lpad must have local visibility, and From 5d43b5780494b922e6cd0a6cdb32f0c4d4c71ba6 Mon Sep 17 00:00:00 2001 From: Kito Cheng Date: Fri, 18 Oct 2024 16:38:08 +0800 Subject: [PATCH 4/6] Revise 'Landing Pad Scheme Relaxation' - Drop the restriction of static link - Emphasis must be applied to all `R_RISCV_LPAD` - GNU property and PLT entries must adjust too. --- riscv-elf.adoc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/riscv-elf.adoc b/riscv-elf.adoc index 138b82b..47bad13 100644 --- a/riscv-elf.adoc +++ b/riscv-elf.adoc @@ -2379,11 +2379,14 @@ Can be relaxed into `nop` if no `R_RISCV_RELAX` is paired with `R_RISCV_LPAD`. Condition:: This relaxation can be performed without `R_RISCV_RELAX`, and should not be enabled by default. The user must explicitly enable this - relaxation, and it should only be applied during static linking. + relaxation. Additionally, if this relaxation is applied, it must be applied + consistently to all `R_RISCV_LPAD` relocations in the entire binary. Relaxation:: - Lpad instruction associated with `R_RISCV_LPAD` will be replaced with `lpad 0`. + - The GNU property must be adjusted to reflect the use of this relaxation. + - The format of the PLT entries must also be adjusted accordingly. Example:: + From 32688be38f8de70c74322272a11774a4f902543a Mon Sep 17 00:00:00 2001 From: Kito Cheng Date: Fri, 18 Oct 2024 16:43:33 +0800 Subject: [PATCH 5/6] Add Note to 'Landing Pad Scheme Relaxation' --- riscv-elf.adoc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/riscv-elf.adoc b/riscv-elf.adoc index 47bad13..77dd6f1 100644 --- a/riscv-elf.adoc +++ b/riscv-elf.adoc @@ -2407,6 +2407,11 @@ Relaxation result: NOTE: This relaxation is designed to be compatible with legacy programs that may not declare the function signature correctly. +NOTE: Dependent shared libraries will not undergo the corresponding +transformation. Therefore, if this Landing Pad Scheme Relaxation is used in a +dynamically linked environment, ensure that all dependent shared libraries are +rebuilt with the corresponding version. + [bibliography] == References From 02546dea2621f2a4850cd9a6920bc6f5bfd32495 Mon Sep 17 00:00:00 2001 From: Kito Cheng Date: Fri, 18 Oct 2024 16:53:39 +0800 Subject: [PATCH 6/6] Relaxation condition updated based on symbol export to dynamic symbol table - Updated the relaxation condition to apply only when the symbol is not exported to the dynamic symbol table. --- riscv-elf.adoc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/riscv-elf.adoc b/riscv-elf.adoc index 77dd6f1..701c1a1 100644 --- a/riscv-elf.adoc +++ b/riscv-elf.adoc @@ -2335,12 +2335,10 @@ instructions. It is recommended to initialize `jvt` CSR immediately after but the linker should pad nop instruction to the same length of the original instruction sequence. - Condition:: The associated function of this lpad must have local visibility, and - it must not be referenced by any relocation other than `R_RISCV_CALL` and - `R_RISCV_CALL_PLT`. - This relaxation can also be performed when the function has global visibility, - if the symbol does not have a corresponding PLT entry and is not referenced by - the GOT or by any relocation other than `R_RISCV_CALL` and `R_RISCV_CALL_PLT`. + Condition:: This relaxation can only be applied if the symbol is **NOT** + exported to the dynamic symbol table and is only referenced by `R_RISCV_CALL` + or `R_RISCV_CALL_PLT` relocations. If the symbol is exported or referenced by + other relocations, relaxation cannot be performed. Relaxation:: - Lpad instruction associated with `R_RISCV_LPAD` can be removed.