2021-01-07 12:35:36

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH v4 mips-next 0/7] MIPS: vmlinux.lds.S sections fixes & cleanup

This series hunts the problems discovered after manual enabling of
ARCH_WANT_LD_ORPHAN_WARN. Notably:
- adds the missing PAGE_ALIGNED_DATA() section affecting VDSO
placement (marked for stable);
- properly stops .eh_frame section generation.

Compile and runtime tested on MIPS32R2 CPS board with no issues
using two different toolkits:
- Binutils 2.35.1, GCC 10.2.0;
- LLVM stack 11.0.0.

Since v3 [2]:
- fix the third patch as GNU stack emits .rel.dyn into VDSO for
some reason if .cfi_sections is specified.

Since v2 [1]:
- stop discarding .eh_frame and just prevent it from generating
(Kees);
- drop redundant sections assertions (Fangrui);
- place GOT table in .text instead of asserting as it's not empty
when building with LLVM (Nathan);
- catch compound literals in generic definitions when building with
LD_DEAD_CODE_DATA_ELIMINATION (Kees);
- collect two Reviewed-bys (Kees).

Since v1 [0]:
- catch .got entries too as LLD may produce it (Nathan);
- check for unwanted sections to be zero-sized instead of
discarding (Fangrui).

[0] https://lore.kernel.org/linux-mips/[email protected]
[1] https://lore.kernel.org/linux-mips/[email protected]
[2] https://lore.kernel.org/linux-mips/[email protected]

Alexander Lobakin (7):
MIPS: vmlinux.lds.S: add missing PAGE_ALIGNED_DATA() section
MIPS: vmlinux.lds.S: add ".gnu.attributes" to DISCARDS
MIPS: properly stop .eh_frame generation
MIPS: vmlinux.lds.S: catch bad .rel.dyn at link time
MIPS: vmlinux.lds.S: explicitly declare .got table
vmlinux.lds.h: catch compound literals into data and BSS
MIPS: select ARCH_WANT_LD_ORPHAN_WARN

arch/mips/Kconfig | 1 +
arch/mips/include/asm/asm.h | 18 ++++++++++++++++++
arch/mips/kernel/vmlinux.lds.S | 15 ++++++++++++++-
include/asm-generic/vmlinux.lds.h | 6 +++---
4 files changed, 36 insertions(+), 4 deletions(-)

--
2.30.0



2021-01-07 12:38:40

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH v4 mips-next 1/7] MIPS: vmlinux.lds.S: add missing PAGE_ALIGNED_DATA() section

MIPS uses its own declaration of rwdata, and thus it should be kept
in sync with the asm-generic one. Currently PAGE_ALIGNED_DATA() is
missing from the linker script, which emits the following ld
warnings:

mips-alpine-linux-musl-ld: warning: orphan section
`.data..page_aligned' from `arch/mips/kernel/vdso.o' being placed
in section `.data..page_aligned'
mips-alpine-linux-musl-ld: warning: orphan section
`.data..page_aligned' from `arch/mips/vdso/vdso-image.o' being placed
in section `.data..page_aligned'

Add the necessary declaration, so the mentioned structures will be
placed in vmlinux as intended:

ffffffff80630580 D __end_once
ffffffff80630580 D __start___dyndbg
ffffffff80630580 D __start_once
ffffffff80630580 D __stop___dyndbg
ffffffff80634000 d mips_vdso_data
ffffffff80638000 d vdso_data
ffffffff80638580 D _gp
ffffffff8063c000 T __init_begin
ffffffff8063c000 D _edata
ffffffff8063c000 T _sinittext

->

ffffffff805a4000 D __end_init_task
ffffffff805a4000 D __nosave_begin
ffffffff805a4000 D __nosave_end
ffffffff805a4000 d mips_vdso_data
ffffffff805a8000 d vdso_data
ffffffff805ac000 D mmlist_lock
ffffffff805ac080 D tasklist_lock

Fixes: ebb5e78cc634 ("MIPS: Initial implementation of a VDSO")
Cc: [email protected] # 4.4+
Signed-off-by: Alexander Lobakin <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
---
arch/mips/kernel/vmlinux.lds.S | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
index 5e97e9d02f98..83e27a181206 100644
--- a/arch/mips/kernel/vmlinux.lds.S
+++ b/arch/mips/kernel/vmlinux.lds.S
@@ -90,6 +90,7 @@ SECTIONS

INIT_TASK_DATA(THREAD_SIZE)
NOSAVE_DATA
+ PAGE_ALIGNED_DATA(PAGE_SIZE)
CACHELINE_ALIGNED_DATA(1 << CONFIG_MIPS_L1_CACHE_SHIFT)
READ_MOSTLY_DATA(1 << CONFIG_MIPS_L1_CACHE_SHIFT)
DATA_DATA
--
2.30.0


2021-01-07 12:39:05

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH v4 mips-next 2/7] MIPS: vmlinux.lds.S: add ".gnu.attributes" to DISCARDS

Discard GNU attributes (MIPS FP type, GNU Hash etc.) at link time
as kernel doesn't use it at all.
Solves a dozen of the following ld warnings (one per every file):

mips-alpine-linux-musl-ld: warning: orphan section `.gnu.attributes'
from `arch/mips/kernel/head.o' being placed in section
`.gnu.attributes'
mips-alpine-linux-musl-ld: warning: orphan section `.gnu.attributes'
from `init/main.o' being placed in section `.gnu.attributes'

Signed-off-by: Alexander Lobakin <[email protected]>
---
arch/mips/kernel/vmlinux.lds.S | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
index 83e27a181206..16468957cba2 100644
--- a/arch/mips/kernel/vmlinux.lds.S
+++ b/arch/mips/kernel/vmlinux.lds.S
@@ -221,6 +221,7 @@ SECTIONS
/* ABI crap starts here */
*(.MIPS.abiflags)
*(.MIPS.options)
+ *(.gnu.attributes)
*(.options)
*(.pdr)
*(.reginfo)
--
2.30.0


2021-01-07 13:22:42

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH v4 mips-next 4/7] MIPS: vmlinux.lds.S: catch bad .rel.dyn at link time

Catch any symbols placed in .rel.dyn and check for these sections
to be zero-sized at link time.
Eliminates following ld warning:

mips-alpine-linux-musl-ld: warning: orphan section `.rel.dyn'
from `init/main.o' being placed in section `.rel.dyn'

Adopted from x86/kernel/vmlinux.lds.S.

Suggested-by: Fangrui Song <[email protected]>
Signed-off-by: Alexander Lobakin <[email protected]>
---
arch/mips/kernel/vmlinux.lds.S | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
index 0f4e46ea4458..0f736d60d43e 100644
--- a/arch/mips/kernel/vmlinux.lds.S
+++ b/arch/mips/kernel/vmlinux.lds.S
@@ -226,4 +226,15 @@ SECTIONS
*(.pdr)
*(.reginfo)
}
+
+ /*
+ * Sections that should stay zero sized, which is safer to
+ * explicitly check instead of blindly discarding.
+ */
+
+ .rel.dyn : {
+ *(.rel.*)
+ *(.rel_*)
+ }
+ ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
}
--
2.30.0


2021-01-07 13:23:07

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH v4 mips-next 5/7] MIPS: vmlinux.lds.S: explicitly declare .got table

LLVM stack generates GOT table when building the kernel:

ld.lld: warning: <internal>:(.got) is being placed in '.got'

According to the debug assertions, it's not zero-sized and thus
can't be handled the same way as .rel.dyn (like it's done for x86).
Use the ARM/ARM64 path here and place it at the end of .text section.

Reported-by: Nathan Chancellor <[email protected]>
Signed-off-by: Alexander Lobakin <[email protected]>
---
arch/mips/kernel/vmlinux.lds.S | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
index 0f736d60d43e..4709959f6985 100644
--- a/arch/mips/kernel/vmlinux.lds.S
+++ b/arch/mips/kernel/vmlinux.lds.S
@@ -69,6 +69,7 @@ SECTIONS
*(.text.*)
*(.fixup)
*(.gnu.warning)
+ *(.got)
} :text = 0
_etext = .; /* End of text section */

--
2.30.0


2021-01-07 13:23:39

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH v4 mips-next 6/7] vmlinux.lds.h: catch compound literals into data and BSS

When building kernel with LD_DEAD_CODE_DATA_ELIMINATION, LLVM stack
generates separate sections for compound literals, just like in case
with enabled LTO [0]:

ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
(.data..compoundliteral.14) is being placed in
'.data..compoundliteral.14'
ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
(.data..compoundliteral.15) is being placed in
'.data..compoundliteral.15'
ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
(.data..compoundliteral.16) is being placed in
'.data..compoundliteral.16'
ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
(.data..compoundliteral.17) is being placed in
'.data..compoundliteral.17'

[...]

Handle this by adding the related sections to generic definitions
as suggested by Sami [0].

[0] https://lore.kernel.org/lkml/[email protected]

Suggested-by: Kees Cook <[email protected]>
Signed-off-by: Alexander Lobakin <[email protected]>
---
include/asm-generic/vmlinux.lds.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index b2b3d81b1535..5f2f5b1db84f 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -95,10 +95,10 @@
*/
#ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
#define TEXT_MAIN .text .text.[0-9a-zA-Z_]*
-#define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..LPBX*
+#define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..L* .data..compoundliteral*
#define SDATA_MAIN .sdata .sdata.[0-9a-zA-Z_]*
-#define RODATA_MAIN .rodata .rodata.[0-9a-zA-Z_]*
-#define BSS_MAIN .bss .bss.[0-9a-zA-Z_]*
+#define RODATA_MAIN .rodata .rodata.[0-9a-zA-Z_]* .rodata..L*
+#define BSS_MAIN .bss .bss.[0-9a-zA-Z_]* .bss..compoundliteral*
#define SBSS_MAIN .sbss .sbss.[0-9a-zA-Z_]*
#else
#define TEXT_MAIN .text
--
2.30.0


2021-01-07 13:23:51

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH v4 mips-next 7/7] MIPS: select ARCH_WANT_LD_ORPHAN_WARN

Now, after that all the sections are explicitly described and
declared in vmlinux.lds.S, we can enable ld orphan warnings to
prevent from missing any new sections in future.

Signed-off-by: Alexander Lobakin <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
---
arch/mips/Kconfig | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
index d68df1febd25..d3e64cc0932b 100644
--- a/arch/mips/Kconfig
+++ b/arch/mips/Kconfig
@@ -18,6 +18,7 @@ config MIPS
select ARCH_USE_QUEUED_SPINLOCKS
select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
select ARCH_WANT_IPC_PARSE_VERSION
+ select ARCH_WANT_LD_ORPHAN_WARN
select BUILDTIME_TABLE_SORT
select CLONE_BACKWARDS
select CPU_NO_EFFICIENT_FFS if (TARGET_ISA_REV < 1)
--
2.30.0


2021-01-07 21:51:10

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 4/7] MIPS: vmlinux.lds.S: catch bad .rel.dyn at link time

On Thu, Jan 07, 2021 at 01:20:33PM +0000, Alexander Lobakin wrote:
> Catch any symbols placed in .rel.dyn and check for these sections
> to be zero-sized at link time.
> Eliminates following ld warning:
>
> mips-alpine-linux-musl-ld: warning: orphan section `.rel.dyn'
> from `init/main.o' being placed in section `.rel.dyn'
>
> Adopted from x86/kernel/vmlinux.lds.S.
>
> Suggested-by: Fangrui Song <[email protected]>
> Signed-off-by: Alexander Lobakin <[email protected]>

Reviewed-by: Kees Cook <[email protected]>

--
Kees Cook

2021-01-07 21:52:38

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 5/7] MIPS: vmlinux.lds.S: explicitly declare .got table

On Thu, Jan 07, 2021 at 01:20:49PM +0000, Alexander Lobakin wrote:
> LLVM stack generates GOT table when building the kernel:
>
> ld.lld: warning: <internal>:(.got) is being placed in '.got'
>
> According to the debug assertions, it's not zero-sized and thus
> can't be handled the same way as .rel.dyn (like it's done for x86).
> Use the ARM/ARM64 path here and place it at the end of .text section.
>
> Reported-by: Nathan Chancellor <[email protected]>
> Signed-off-by: Alexander Lobakin <[email protected]>

Reviewed-by: Kees Cook <[email protected]>

--
Kees Cook

2021-01-07 21:53:09

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 6/7] vmlinux.lds.h: catch compound literals into data and BSS

On Thu, Jan 07, 2021 at 01:20:55PM +0000, Alexander Lobakin wrote:
> When building kernel with LD_DEAD_CODE_DATA_ELIMINATION, LLVM stack
> generates separate sections for compound literals, just like in case
> with enabled LTO [0]:
>
> ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
> (.data..compoundliteral.14) is being placed in
> '.data..compoundliteral.14'
> ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
> (.data..compoundliteral.15) is being placed in
> '.data..compoundliteral.15'
> ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
> (.data..compoundliteral.16) is being placed in
> '.data..compoundliteral.16'
> ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
> (.data..compoundliteral.17) is being placed in
> '.data..compoundliteral.17'
>
> [...]
>
> Handle this by adding the related sections to generic definitions
> as suggested by Sami [0].
>
> [0] https://lore.kernel.org/lkml/[email protected]
>
> Suggested-by: Kees Cook <[email protected]>
> Signed-off-by: Alexander Lobakin <[email protected]>

Suggested-by: Sami Tolvanen <[email protected]>
Reviewed-by: Kees Cook <[email protected]>

--
Kees Cook

2021-01-07 21:53:33

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 0/7] MIPS: vmlinux.lds.S sections fixes & cleanup

On Thu, Jan 07, 2021 at 12:33:38PM +0000, Alexander Lobakin wrote:
> This series hunts the problems discovered after manual enabling of
> ARCH_WANT_LD_ORPHAN_WARN. Notably:
> - adds the missing PAGE_ALIGNED_DATA() section affecting VDSO
> placement (marked for stable);
> - properly stops .eh_frame section generation.
>
> Compile and runtime tested on MIPS32R2 CPS board with no issues
> using two different toolkits:
> - Binutils 2.35.1, GCC 10.2.0;
> - LLVM stack 11.0.0.

Nice! Thanks for hunting down the corner cases. :)

--
Kees Cook

2021-01-08 21:13:10

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 1/7] MIPS: vmlinux.lds.S: add missing PAGE_ALIGNED_DATA() section

On Thu, Jan 07, 2021 at 12:34:47PM +0000, Alexander Lobakin wrote:
> MIPS uses its own declaration of rwdata, and thus it should be kept
> in sync with the asm-generic one. Currently PAGE_ALIGNED_DATA() is
> missing from the linker script, which emits the following ld
> warnings:
>
> mips-alpine-linux-musl-ld: warning: orphan section
> `.data..page_aligned' from `arch/mips/kernel/vdso.o' being placed
> in section `.data..page_aligned'
> mips-alpine-linux-musl-ld: warning: orphan section
> `.data..page_aligned' from `arch/mips/vdso/vdso-image.o' being placed
> in section `.data..page_aligned'
>
> Add the necessary declaration, so the mentioned structures will be
> placed in vmlinux as intended:
>
> ffffffff80630580 D __end_once
> ffffffff80630580 D __start___dyndbg
> ffffffff80630580 D __start_once
> ffffffff80630580 D __stop___dyndbg
> ffffffff80634000 d mips_vdso_data
> ffffffff80638000 d vdso_data
> ffffffff80638580 D _gp
> ffffffff8063c000 T __init_begin
> ffffffff8063c000 D _edata
> ffffffff8063c000 T _sinittext
>
> ->
>
> ffffffff805a4000 D __end_init_task
> ffffffff805a4000 D __nosave_begin
> ffffffff805a4000 D __nosave_end
> ffffffff805a4000 d mips_vdso_data
> ffffffff805a8000 d vdso_data
> ffffffff805ac000 D mmlist_lock
> ffffffff805ac080 D tasklist_lock
>
> Fixes: ebb5e78cc634 ("MIPS: Initial implementation of a VDSO")
> Cc: [email protected] # 4.4+
> Signed-off-by: Alexander Lobakin <[email protected]>
> Reviewed-by: Kees Cook <[email protected]>

Reviewed-by: Nathan Chancellor <[email protected]>

> ---
> arch/mips/kernel/vmlinux.lds.S | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
> index 5e97e9d02f98..83e27a181206 100644
> --- a/arch/mips/kernel/vmlinux.lds.S
> +++ b/arch/mips/kernel/vmlinux.lds.S
> @@ -90,6 +90,7 @@ SECTIONS
>
> INIT_TASK_DATA(THREAD_SIZE)
> NOSAVE_DATA
> + PAGE_ALIGNED_DATA(PAGE_SIZE)
> CACHELINE_ALIGNED_DATA(1 << CONFIG_MIPS_L1_CACHE_SHIFT)
> READ_MOSTLY_DATA(1 << CONFIG_MIPS_L1_CACHE_SHIFT)
> DATA_DATA
> --
> 2.30.0
>
>

2021-01-08 21:15:15

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 2/7] MIPS: vmlinux.lds.S: add ".gnu.attributes" to DISCARDS

On Thu, Jan 07, 2021 at 12:34:53PM +0000, Alexander Lobakin wrote:
> Discard GNU attributes (MIPS FP type, GNU Hash etc.) at link time
> as kernel doesn't use it at all.
> Solves a dozen of the following ld warnings (one per every file):
>
> mips-alpine-linux-musl-ld: warning: orphan section `.gnu.attributes'
> from `arch/mips/kernel/head.o' being placed in section
> `.gnu.attributes'
> mips-alpine-linux-musl-ld: warning: orphan section `.gnu.attributes'
> from `init/main.o' being placed in section `.gnu.attributes'
>
> Signed-off-by: Alexander Lobakin <[email protected]>

Reviewed-by: Nathan Chancellor <[email protected]>

> ---
> arch/mips/kernel/vmlinux.lds.S | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
> index 83e27a181206..16468957cba2 100644
> --- a/arch/mips/kernel/vmlinux.lds.S
> +++ b/arch/mips/kernel/vmlinux.lds.S
> @@ -221,6 +221,7 @@ SECTIONS
> /* ABI crap starts here */
> *(.MIPS.abiflags)
> *(.MIPS.options)
> + *(.gnu.attributes)
> *(.options)
> *(.pdr)
> *(.reginfo)
> --
> 2.30.0
>
>

2021-01-08 21:20:18

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 4/7] MIPS: vmlinux.lds.S: catch bad .rel.dyn at link time

On Thu, Jan 07, 2021 at 01:20:33PM +0000, Alexander Lobakin wrote:
> Catch any symbols placed in .rel.dyn and check for these sections
> to be zero-sized at link time.
> Eliminates following ld warning:
>
> mips-alpine-linux-musl-ld: warning: orphan section `.rel.dyn'
> from `init/main.o' being placed in section `.rel.dyn'
>
> Adopted from x86/kernel/vmlinux.lds.S.
>
> Suggested-by: Fangrui Song <[email protected]>
> Signed-off-by: Alexander Lobakin <[email protected]>

Reviewed-by: Nathan Chancellor <[email protected]>

> ---
> arch/mips/kernel/vmlinux.lds.S | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
> index 0f4e46ea4458..0f736d60d43e 100644
> --- a/arch/mips/kernel/vmlinux.lds.S
> +++ b/arch/mips/kernel/vmlinux.lds.S
> @@ -226,4 +226,15 @@ SECTIONS
> *(.pdr)
> *(.reginfo)
> }
> +
> + /*
> + * Sections that should stay zero sized, which is safer to
> + * explicitly check instead of blindly discarding.
> + */
> +
> + .rel.dyn : {
> + *(.rel.*)
> + *(.rel_*)
> + }
> + ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
> }
> --
> 2.30.0
>
>

2021-01-08 21:20:27

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 5/7] MIPS: vmlinux.lds.S: explicitly declare .got table

On Thu, Jan 07, 2021 at 01:20:49PM +0000, Alexander Lobakin wrote:
> LLVM stack generates GOT table when building the kernel:
>
> ld.lld: warning: <internal>:(.got) is being placed in '.got'
>
> According to the debug assertions, it's not zero-sized and thus
> can't be handled the same way as .rel.dyn (like it's done for x86).
> Use the ARM/ARM64 path here and place it at the end of .text section.
>
> Reported-by: Nathan Chancellor <[email protected]>
> Signed-off-by: Alexander Lobakin <[email protected]>

Reviewed-by: Nathan Chancellor <[email protected]>

> ---
> arch/mips/kernel/vmlinux.lds.S | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
> index 0f736d60d43e..4709959f6985 100644
> --- a/arch/mips/kernel/vmlinux.lds.S
> +++ b/arch/mips/kernel/vmlinux.lds.S
> @@ -69,6 +69,7 @@ SECTIONS
> *(.text.*)
> *(.fixup)
> *(.gnu.warning)
> + *(.got)
> } :text = 0
> _etext = .; /* End of text section */
>
> --
> 2.30.0
>
>

2021-01-08 21:22:10

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 7/7] MIPS: select ARCH_WANT_LD_ORPHAN_WARN

On Thu, Jan 07, 2021 at 01:21:02PM +0000, Alexander Lobakin wrote:
> Now, after that all the sections are explicitly described and
> declared in vmlinux.lds.S, we can enable ld orphan warnings to
> prevent from missing any new sections in future.
>
> Signed-off-by: Alexander Lobakin <[email protected]>
> Reviewed-by: Kees Cook <[email protected]>

Reviewed-by: Nathan Chancellor <[email protected]>

> ---
> arch/mips/Kconfig | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
> index d68df1febd25..d3e64cc0932b 100644
> --- a/arch/mips/Kconfig
> +++ b/arch/mips/Kconfig
> @@ -18,6 +18,7 @@ config MIPS
> select ARCH_USE_QUEUED_SPINLOCKS
> select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
> select ARCH_WANT_IPC_PARSE_VERSION
> + select ARCH_WANT_LD_ORPHAN_WARN
> select BUILDTIME_TABLE_SORT
> select CLONE_BACKWARDS
> select CPU_NO_EFFICIENT_FFS if (TARGET_ISA_REV < 1)
> --
> 2.30.0
>
>

2021-01-08 21:22:46

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 6/7] vmlinux.lds.h: catch compound literals into data and BSS

On Thu, Jan 07, 2021 at 01:20:55PM +0000, Alexander Lobakin wrote:
> When building kernel with LD_DEAD_CODE_DATA_ELIMINATION, LLVM stack
> generates separate sections for compound literals, just like in case
> with enabled LTO [0]:
>
> ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
> (.data..compoundliteral.14) is being placed in
> '.data..compoundliteral.14'
> ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
> (.data..compoundliteral.15) is being placed in
> '.data..compoundliteral.15'
> ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
> (.data..compoundliteral.16) is being placed in
> '.data..compoundliteral.16'
> ld.lld: warning: drivers/built-in.a(mtd/nand/spi/gigadevice.o):
> (.data..compoundliteral.17) is being placed in
> '.data..compoundliteral.17'
>
> [...]
>
> Handle this by adding the related sections to generic definitions
> as suggested by Sami [0].
>
> [0] https://lore.kernel.org/lkml/[email protected]
>
> Suggested-by: Kees Cook <[email protected]>
> Signed-off-by: Alexander Lobakin <[email protected]>

Reviewed-by: Nathan Chancellor <[email protected]>

> ---
> include/asm-generic/vmlinux.lds.h | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> index b2b3d81b1535..5f2f5b1db84f 100644
> --- a/include/asm-generic/vmlinux.lds.h
> +++ b/include/asm-generic/vmlinux.lds.h
> @@ -95,10 +95,10 @@
> */
> #ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
> #define TEXT_MAIN .text .text.[0-9a-zA-Z_]*
> -#define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..LPBX*
> +#define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..L* .data..compoundliteral*
> #define SDATA_MAIN .sdata .sdata.[0-9a-zA-Z_]*
> -#define RODATA_MAIN .rodata .rodata.[0-9a-zA-Z_]*
> -#define BSS_MAIN .bss .bss.[0-9a-zA-Z_]*
> +#define RODATA_MAIN .rodata .rodata.[0-9a-zA-Z_]* .rodata..L*
> +#define BSS_MAIN .bss .bss.[0-9a-zA-Z_]* .bss..compoundliteral*
> #define SBSS_MAIN .sbss .sbss.[0-9a-zA-Z_]*
> #else
> #define TEXT_MAIN .text
> --
> 2.30.0
>
>

2021-01-09 12:30:49

by Thomas Bogendoerfer

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 0/7] MIPS: vmlinux.lds.S sections fixes & cleanup

On Thu, Jan 07, 2021 at 12:33:38PM +0000, Alexander Lobakin wrote:
> This series hunts the problems discovered after manual enabling of
> ARCH_WANT_LD_ORPHAN_WARN. Notably:
> - adds the missing PAGE_ALIGNED_DATA() section affecting VDSO
> placement (marked for stable);
> - properly stops .eh_frame section generation.
>
> Compile and runtime tested on MIPS32R2 CPS board with no issues
> using two different toolkits:
> - Binutils 2.35.1, GCC 10.2.0;
> - LLVM stack 11.0.0.
>
> Since v3 [2]:
> - fix the third patch as GNU stack emits .rel.dyn into VDSO for
> some reason if .cfi_sections is specified.
>
> Since v2 [1]:
> - stop discarding .eh_frame and just prevent it from generating
> (Kees);
> - drop redundant sections assertions (Fangrui);
> - place GOT table in .text instead of asserting as it's not empty
> when building with LLVM (Nathan);
> - catch compound literals in generic definitions when building with
> LD_DEAD_CODE_DATA_ELIMINATION (Kees);
> - collect two Reviewed-bys (Kees).
>
> Since v1 [0]:
> - catch .got entries too as LLD may produce it (Nathan);
> - check for unwanted sections to be zero-sized instead of
> discarding (Fangrui).
>
> [0] https://lore.kernel.org/linux-mips/[email protected]
> [1] https://lore.kernel.org/linux-mips/[email protected]
> [2] https://lore.kernel.org/linux-mips/[email protected]
>
> Alexander Lobakin (7):
> MIPS: vmlinux.lds.S: add missing PAGE_ALIGNED_DATA() section
> MIPS: vmlinux.lds.S: add ".gnu.attributes" to DISCARDS
> MIPS: properly stop .eh_frame generation
> MIPS: vmlinux.lds.S: catch bad .rel.dyn at link time
> MIPS: vmlinux.lds.S: explicitly declare .got table
> vmlinux.lds.h: catch compound literals into data and BSS
> MIPS: select ARCH_WANT_LD_ORPHAN_WARN

this breaks my builds:

LD vmlinux.o
MODPOST vmlinux.symvers
MODINFO modules.builtin.modinfo
GEN modules.builtin
LD .tmp_vmlinux.kallsyms1
mips64-linux-gnu-ld: Unexpected run-time relocations (.rel) detected!


$ mips64-linux-gnu-ld --version
GNU ld version 2.27-3.fc24

$ mips64-linux-gnu-gcc --version
mips64-linux-gnu-gcc (GCC) 6.1.1 20160621 (Red Hat Cross 6.1.1-2)

Thomas.

--
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea. [ RFC1925, 2.3 ]

2021-01-09 17:25:30

by Alexander Lobakin

[permalink] [raw]
Subject: Re: [PATCH v4 mips-next 0/7] MIPS: vmlinux.lds.S sections fixes & cleanup

From: Thomas Bogendoerfer <[email protected]>
Date: Sat, 9 Jan 2021 12:12:59 +0100

> On Thu, Jan 07, 2021 at 12:33:38PM +0000, Alexander Lobakin wrote:
>> This series hunts the problems discovered after manual enabling of
>> ARCH_WANT_LD_ORPHAN_WARN. Notably:
>> - adds the missing PAGE_ALIGNED_DATA() section affecting VDSO
>> placement (marked for stable);
>> - properly stops .eh_frame section generation.
>>
>> Compile and runtime tested on MIPS32R2 CPS board with no issues
>> using two different toolkits:
>> - Binutils 2.35.1, GCC 10.2.0;
>> - LLVM stack 11.0.0.
>>
>> Since v3 [2]:
>> - fix the third patch as GNU stack emits .rel.dyn into VDSO for
>> some reason if .cfi_sections is specified.
>>
>> Since v2 [1]:
>> - stop discarding .eh_frame and just prevent it from generating
>> (Kees);
>> - drop redundant sections assertions (Fangrui);
>> - place GOT table in .text instead of asserting as it's not empty
>> when building with LLVM (Nathan);
>> - catch compound literals in generic definitions when building with
>> LD_DEAD_CODE_DATA_ELIMINATION (Kees);
>> - collect two Reviewed-bys (Kees).
>>
>> Since v1 [0]:
>> - catch .got entries too as LLD may produce it (Nathan);
>> - check for unwanted sections to be zero-sized instead of
>> discarding (Fangrui).
>>
>> [0] https://lore.kernel.org/linux-mips/[email protected]
>> [1] https://lore.kernel.org/linux-mips/[email protected]
>> [2] https://lore.kernel.org/linux-mips/[email protected]
>>
>> Alexander Lobakin (7):
>> MIPS: vmlinux.lds.S: add missing PAGE_ALIGNED_DATA() section
>> MIPS: vmlinux.lds.S: add ".gnu.attributes" to DISCARDS
>> MIPS: properly stop .eh_frame generation
>> MIPS: vmlinux.lds.S: catch bad .rel.dyn at link time
>> MIPS: vmlinux.lds.S: explicitly declare .got table
>> vmlinux.lds.h: catch compound literals into data and BSS
>> MIPS: select ARCH_WANT_LD_ORPHAN_WARN
>
> this breaks my builds:
>
> LD vmlinux.o
> MODPOST vmlinux.symvers
> MODINFO modules.builtin.modinfo
> GEN modules.builtin
> LD .tmp_vmlinux.kallsyms1
> mips64-linux-gnu-ld: Unexpected run-time relocations (.rel) detected!

I think they should be handled as it's done for ARM64 [0]. Will do v5
soon.

[0] https://elixir.bootlin.com/linux/v5.11-rc2/source/arch/arm64/kernel/vmlinux.lds.S#L219

> $ mips64-linux-gnu-ld --version
> GNU ld version 2.27-3.fc24
>
> $ mips64-linux-gnu-gcc --version
> mips64-linux-gnu-gcc (GCC) 6.1.1 20160621 (Red Hat Cross 6.1.1-2)
>
> Thomas.

Thanks,
Alex

> --
> Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
> good idea. [ RFC1925, 2.3 ]