diff mbox series

[v2,mips-next,3/4] MIPS: vmlinux.lds.S: catch bad .got, .plt and .rel.dyn at link time

Message ID 20210106200801.31993-3-alobakin@pm.me (mailing list archive)
State Superseded
Headers show
Series MIPS: vmlinux.lds.S sections fix & cleanup | expand

Commit Message

Alexander Lobakin Jan. 6, 2021, 8:08 p.m. UTC
Catch any symbols placed in .got, .got.plt, .plt, .rel.dyn
or .rela.dyn and check for these sections to be zero-sized
at link time.

At least two of them were noticed in real builds:

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

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

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

Reported-by: Nathan Chancellor <natechancellor@gmail.com> # .got
Suggested-by: Fangrui Song <maskray@google.com> # .rel.dyn
Signed-off-by: Alexander Lobakin <alobakin@pm.me>
---
 arch/mips/kernel/vmlinux.lds.S | 35 ++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

Comments

Fangrui Song Jan. 6, 2021, 8:21 p.m. UTC | #1
On Wed, Jan 6, 2021 at 12:08 PM Alexander Lobakin <alobakin@pm.me> wrote:
>
> Catch any symbols placed in .got, .got.plt, .plt, .rel.dyn
> or .rela.dyn and check for these sections to be zero-sized
> at link time.
>
> At least two of them were noticed in real builds:
>
> mips-alpine-linux-musl-ld: warning: orphan section `.rel.dyn'
> from `init/main.o' being placed in section `.rel.dyn'
>
> ld.lld: warning: <internal>:(.got) is being placed in '.got'
>
> Adopted from x86/kernel/vmlinux.lds.S.
>
> Reported-by: Nathan Chancellor <natechancellor@gmail.com> # .got
> Suggested-by: Fangrui Song <maskray@google.com> # .rel.dyn
> Signed-off-by: Alexander Lobakin <alobakin@pm.me>
> ---
>  arch/mips/kernel/vmlinux.lds.S | 35 ++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)
>
> diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
> index 5d6563970ab2..05eda9d9a7d5 100644
> --- a/arch/mips/kernel/vmlinux.lds.S
> +++ b/arch/mips/kernel/vmlinux.lds.S
> @@ -227,4 +227,39 @@ SECTIONS
>                 *(.pdr)
>                 *(.reginfo)
>         }
> +
> +       /*
> +        * Sections that should stay zero sized, which is safer to
> +        * explicitly check instead of blindly discarding.
> +        */
> +
> +       .got : {
> +               *(.got)
> +               *(.igot.*)
> +       }
> +       ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
> +
> +       .got.plt (INFO) : {
> +               *(.got.plt)
> +       }
> +       ASSERT(SIZEOF(.got.plt) == 0, "Unexpected GOT/PLT entries detected!")

(INFO) drops the SHF_ALLOC flag from the output section (It does not
mean "informational"). INFO is not need here.
The diff from 815d680771ae09080d2da83dac2647c08cdf99ce "x86/build:
Enforce an empty .got.plt section" is not needed.

> +       .plt : {
> +               *(.plt)
> +               *(.plt.*)
> +               *(.iplt)
> +       }
> +       ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
> +
> +       .rel.dyn : {
> +               *(.rel.*)
> +               *(.rel_*)
> +       }
> +       ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
> +
> +       .rela.dyn : {
> +               *(.rela.*)
> +               *(.rela_*)
> +       }
> +       ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
>  }

x86 has both .rel.dyn and .rela.dyn because i386 psABI uses REL while
x86-64 psABI uses RELA, but mips does not need .rela.dyn

> --
> 2.30.0
>
>
Nathan Chancellor Jan. 6, 2021, 8:23 p.m. UTC | #2
On Wed, Jan 06, 2021 at 08:08:29PM +0000, Alexander Lobakin wrote:
> Catch any symbols placed in .got, .got.plt, .plt, .rel.dyn
> or .rela.dyn and check for these sections to be zero-sized
> at link time.
> 
> At least two of them were noticed in real builds:
> 
> mips-alpine-linux-musl-ld: warning: orphan section `.rel.dyn'
> from `init/main.o' being placed in section `.rel.dyn'
> 
> ld.lld: warning: <internal>:(.got) is being placed in '.got'
> 
> Adopted from x86/kernel/vmlinux.lds.S.
> 
> Reported-by: Nathan Chancellor <natechancellor@gmail.com> # .got
> Suggested-by: Fangrui Song <maskray@google.com> # .rel.dyn
> Signed-off-by: Alexander Lobakin <alobakin@pm.me>
> ---
>  arch/mips/kernel/vmlinux.lds.S | 35 ++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)
> 
> diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
> index 5d6563970ab2..05eda9d9a7d5 100644
> --- a/arch/mips/kernel/vmlinux.lds.S
> +++ b/arch/mips/kernel/vmlinux.lds.S
> @@ -227,4 +227,39 @@ SECTIONS
>  		*(.pdr)
>  		*(.reginfo)
>  	}
> +
> +	/*
> +	 * Sections that should stay zero sized, which is safer to
> +	 * explicitly check instead of blindly discarding.
> +	 */
> +
> +	.got : {
> +		*(.got)
> +		*(.igot.*)
> +	}
> +	ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")

This assertion does trigger now.

$ make -skj"$(nproc)" ARCH=mips CROSS_COMPILE=mipsel-linux-gnu- LLVM=1 \
       O=out/mipsel distclean malta_kvm_guest_defconfig all
...
ld.lld: error: Unexpected GOT entries detected!
ld.lld: error: Unexpected GOT entries detected!
...

> +	.got.plt (INFO) : {
> +		*(.got.plt)
> +	}
> +	ASSERT(SIZEOF(.got.plt) == 0, "Unexpected GOT/PLT entries detected!")
> +
> +	.plt : {
> +		*(.plt)
> +		*(.plt.*)
> +		*(.iplt)
> +	}
> +	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
> +
> +	.rel.dyn : {
> +		*(.rel.*)
> +		*(.rel_*)
> +	}
> +	ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
> +
> +	.rela.dyn : {
> +		*(.rela.*)
> +		*(.rela_*)
> +	}
> +	ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
>  }
> -- 
> 2.30.0
> 
>
Alexander Lobakin Jan. 6, 2021, 8:28 p.m. UTC | #3
From: Nathan Chancellor <natechancellor@gmail.com>
Date: Wed, 6 Jan 2021 13:23:24 -0700

On Wed, Jan 06, 2021 at 08:08:29PM +0000, Alexander Lobakin wrote:
>> Catch any symbols placed in .got, .got.plt, .plt, .rel.dyn
>> or .rela.dyn and check for these sections to be zero-sized
>> at link time.
>>
>> At least two of them were noticed in real builds:
>>
>> mips-alpine-linux-musl-ld: warning: orphan section `.rel.dyn'
>> from `init/main.o' being placed in section `.rel.dyn'
>>
>> ld.lld: warning: <internal>:(.got) is being placed in '.got'
>>
>> Adopted from x86/kernel/vmlinux.lds.S.
>>
>> Reported-by: Nathan Chancellor <natechancellor@gmail.com> # .got
>> Suggested-by: Fangrui Song <maskray@google.com> # .rel.dyn
>> Signed-off-by: Alexander Lobakin <alobakin@pm.me>
>> ---
>>  arch/mips/kernel/vmlinux.lds.S | 35 ++++++++++++++++++++++++++++++++++
>>  1 file changed, 35 insertions(+)
>>
>> diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
>> index 5d6563970ab2..05eda9d9a7d5 100644
>> --- a/arch/mips/kernel/vmlinux.lds.S
>> +++ b/arch/mips/kernel/vmlinux.lds.S
>> @@ -227,4 +227,39 @@ SECTIONS
>>  		*(.pdr)
>>  		*(.reginfo)
>>  	}
>> +
>> +	/*
>> +	 * Sections that should stay zero sized, which is safer to
>> +	 * explicitly check instead of blindly discarding.
>> +	 */
>> +
>> +	.got : {
>> +		*(.got)
>> +		*(.igot.*)
>> +	}
>> +	ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
>
> This assertion does trigger now.
>
> $ make -skj"$(nproc)" ARCH=mips CROSS_COMPILE=mipsel-linux-gnu- LLVM=1 \
>        O=out/mipsel distclean malta_kvm_guest_defconfig all
> ...
> ld.lld: error: Unexpected GOT entries detected!
> ld.lld: error: Unexpected GOT entries detected!
> ...

Oops. I'll build my kernel with LLVM stack and dig into it deeper
tomorrow.

>> +	.got.plt (INFO) : {
>> +		*(.got.plt)
>> +	}
>> +	ASSERT(SIZEOF(.got.plt) == 0, "Unexpected GOT/PLT entries detected!")
>> +
>> +	.plt : {
>> +		*(.plt)
>> +		*(.plt.*)
>> +		*(.iplt)
>> +	}
>> +	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
>> +
>> +	.rel.dyn : {
>> +		*(.rel.*)
>> +		*(.rel_*)
>> +	}
>> +	ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
>> +
>> +	.rela.dyn : {
>> +		*(.rela.*)
>> +		*(.rela_*)
>> +	}
>> +	ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
>>  }
>> --
>> 2.30.0

Thanks,
Al
diff mbox series

Patch

diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S
index 5d6563970ab2..05eda9d9a7d5 100644
--- a/arch/mips/kernel/vmlinux.lds.S
+++ b/arch/mips/kernel/vmlinux.lds.S
@@ -227,4 +227,39 @@  SECTIONS
 		*(.pdr)
 		*(.reginfo)
 	}
+
+	/*
+	 * Sections that should stay zero sized, which is safer to
+	 * explicitly check instead of blindly discarding.
+	 */
+
+	.got : {
+		*(.got)
+		*(.igot.*)
+	}
+	ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
+
+	.got.plt (INFO) : {
+		*(.got.plt)
+	}
+	ASSERT(SIZEOF(.got.plt) == 0, "Unexpected GOT/PLT entries detected!")
+
+	.plt : {
+		*(.plt)
+		*(.plt.*)
+		*(.iplt)
+	}
+	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
+
+	.rel.dyn : {
+		*(.rel.*)
+		*(.rel_*)
+	}
+	ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
+
+	.rela.dyn : {
+		*(.rela.*)
+		*(.rela_*)
+	}
+	ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
 }