Skip to contentSkip to conversation

x86/build: Simplify patterns for unwanted section

PATCH In review Fangrui Song 2 participants 5 messages Feb 14, 2024 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/build: Simplify patterns for unwanted section

A s390 patch modeling its --orphan-handling= after x86 [1] sparked my motivation to simplify patterns. Commit 5354e84598f2 ("x86/build: Add asserts for unwanted sections") added asserts that certain input sections must be absent or empty. The patterns can be simplified.

For dynamic relocations,

*(.rela.*) is sufficient to match all dynamic relocations synthesized by GNU ld and LLD. .rela_* is unnecessary. --emit-relocs may create .rela_* sections for section names prefixed with _, but they are not matched by linker scripts.

.plt instead of .plt.* is sufficient to match synthesized PLT entries.

.igot and .igot.plt are for non-preemptible STT_GNU_IFUNC in GNU ld (LLD just uses .got), which the kernel does not use. In addition, if .igot or .igot.plt is ever non-empty, there will be .rela.* dynamic relocations leading to an assert failure anyway.

[1]: https://lore.kernel.org/all/[email protected]/

---
 arch/x86/boot/compressed/vmlinux.lds.S | 6 +++---
 arch/x86/kernel/vmlinux.lds.S          | 8 ++++----
 2 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/boot/compressed/vmlinux.lds.S +3 -3
index 083ec6d7722a..9f288f67972a 100644
@@ -104,17 +104,17 @@ SECTIONS
 	ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
 
 	.plt : {
-		*(.plt) *(.plt.*)
+		*(.plt)
 	}
 	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
 
 	.rel.dyn : {
-		*(.rel.*) *(.rel_*)
+		*(.rel.*)
 	}
 	ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
 
 	.rela.dyn : {
-		*(.rela.*) *(.rela_*)
+		*(.rela.*)
 	}
 	ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
 }
arch/x86/kernel/vmlinux.lds.S +4 -4
index a349dbfc6d5a..b3da7b81d2b3 100644
@@ -463,22 +463,22 @@ SECTIONS
 	 * explicitly check instead of blindly discarding.
 	 */
 	.got : {
-		*(.got) *(.igot.*)
+		*(.got)
 	}
 	ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
 
 	.plt : {
-		*(.plt) *(.plt.*) *(.iplt)
+		*(.plt)
 	}
 	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
 
 	.rel.dyn : {
-		*(.rel.*) *(.rel_*)
+		*(.rel.*)
 	}
 	ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
 
 	.rela.dyn : {
-		*(.rela.*) *(.rela_*)
+		*(.rela.*)
 	}
 	ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
 }
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
#1 lore raw
Quoted 5 lines · Fangrui Song

A s390 patch modeling its --orphan-handling= after x86 [1] sparked my motivation to simplify patterns. Commit 5354e84598f2 ("x86/build: Add asserts for unwanted sections") added asserts that certain input sections must be absent or empty. The patterns can be simplified.

Quoted 8 lines

For dynamic relocations,

*(.rela.*) is sufficient to match all dynamic relocations synthesized by GNU ld and LLD. .rela_* is unnecessary. --emit-relocs may create .rela_* sections for section names prefixed with _, but they are not matched by linker scripts.

.plt instead of .plt.* is sufficient to match synthesized PLT entries.

Do you mean ".plt.foo" matches ".plt" ?

Quoted 4 lines

.igot and .igot.plt are for non-preemptible STT_GNU_IFUNC in GNU ld (LLD just uses .got), which the kernel does not use. In addition, if .igot or .igot.plt is ever non-empty, there will be .rela.* dynamic relocations leading to an assert failure anyway.

I think at the time I was dealing with avoid multiple warnings out of the linker, as I was getting orphan warnings in addition to the non-empty warnings.

Quoted 4 lines

[1]: https://lore.kernel.org/all/[email protected]/

Is anything harmed by leaving all of this as-is?

-Kees

Quoted 64 lines
---
 arch/x86/boot/compressed/vmlinux.lds.S | 6 +++---
 arch/x86/kernel/vmlinux.lds.S          | 8 ++++----
 2 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/boot/compressed/vmlinux.lds.S +3 -3
index 083ec6d7722a..9f288f67972a 100644
@@ -104,17 +104,17 @@ SECTIONS
 	ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
 
 	.plt : {
-		*(.plt) *(.plt.*)
+		*(.plt)
 	}
 	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
 
 	.rel.dyn : {
-		*(.rel.*) *(.rel_*)
+		*(.rel.*)
 	}
 	ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
 
 	.rela.dyn : {
-		*(.rela.*) *(.rela_*)
+		*(.rela.*)
 	}
 	ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
 }
arch/x86/kernel/vmlinux.lds.S +4 -4
index a349dbfc6d5a..b3da7b81d2b3 100644
@@ -463,22 +463,22 @@ SECTIONS
 	 * explicitly check instead of blindly discarding.
 	 */
 	.got : {
-		*(.got) *(.igot.*)
+		*(.got)
 	}
 	ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
 
 	.plt : {
-		*(.plt) *(.plt.*) *(.iplt)
+		*(.plt)
 	}
 	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
 
 	.rel.dyn : {
-		*(.rel.*) *(.rel_*)
+		*(.rel.*)
 	}
 	ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
 
 	.rela.dyn : {
-		*(.rela.*) *(.rela_*)
+		*(.rela.*)
 	}
 	ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
 }
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Signature · 1 line
Kees Cook
#2 lore raw
Quoted 9 lines · Kees Cook
Quoted 7 lines · Fangrui Song

A s390 patch modeling its --orphan-handling= after x86 [1] sparked my motivation to simplify patterns. Commit 5354e84598f2 ("x86/build: Add asserts for unwanted sections") added asserts that certain input sections must be absent or empty. The patterns can be simplified.

For dynamic relocations,

Quoted 8 lines
Quoted 6 lines

*(.rela.*) is sufficient to match all dynamic relocations synthesized by GNU ld and LLD. .rela_* is unnecessary. --emit-relocs may create .rela_* sections for section names prefixed with _, but they are not matched by linker scripts.

.plt instead of .plt.* is sufficient to match synthesized PLT entries.

Do you mean ".plt.foo" matches ".plt" ?

I mean we just need .plt : { *(.plt) } , not .plt : { *(.plt) *(.plt.*) }.

The linker synthesized section for PLT entries is .plt, not suffixed.

Quoted 10 lines
Quoted 4 lines

.igot and .igot.plt are for non-preemptible STT_GNU_IFUNC in GNU ld (LLD just uses .got), which the kernel does not use. In addition, if .igot or .igot.plt is ever non-empty, there will be .rela.* dynamic relocations leading to an assert failure anyway.

I think at the time I was dealing with avoid multiple warnings out of the linker, as I was getting orphan warnings in addition to the non-empty warnings.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

[1]: https://lore.kernel.org/all/[email protected]/

Is anything harmed by leaving all of this as-is?

-Kees

No harm. But ports adopting --orphan-handling= (like s390) may copy the unneeded .rela_* .
When people read .rela_*, they might think whether the kernel does anything special that
.rela_* needs to be matched.

Quoted 67 lines
Quoted 62 lines
---
 arch/x86/boot/compressed/vmlinux.lds.S | 6 +++---
 arch/x86/kernel/vmlinux.lds.S          | 8 ++++----
 2 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/boot/compressed/vmlinux.lds.S +3 -3
index 083ec6d7722a..9f288f67972a 100644
@@ -104,17 +104,17 @@ SECTIONS
      ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
 
      .plt : {
-             *(.plt) *(.plt.*)
+             *(.plt)
      }
      ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
 
      .rel.dyn : {
-             *(.rel.*) *(.rel_*)
+             *(.rel.*)
      }
      ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
 
      .rela.dyn : {
-             *(.rela.*) *(.rela_*)
+             *(.rela.*)
      }
      ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
 }
arch/x86/kernel/vmlinux.lds.S +4 -5
index a349dbfc6d5a..b3da7b81d2b3 100644
@@ -463,22 +463,22 @@ SECTIONS
       * explicitly check instead of blindly discarding.
       */
      .got : {
-             *(.got) *(.igot.*)
+             *(.got)
      }
      ASSERT(SIZEOF(.got) == 0, "Unexpected GOT entries detected!")
 
      .plt : {
-             *(.plt) *(.plt.*) *(.iplt)
+             *(.plt)
      }
      ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
 
      .rel.dyn : {
-             *(.rel.*) *(.rel_*)
+             *(.rel.*)
      }
      ASSERT(SIZEOF(.rel.dyn) == 0, "Unexpected run-time relocations (.rel) detected!")
 
      .rela.dyn : {
-             *(.rela.*) *(.rela_*)
+             *(.rela.*)
      }
      ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
 }
--
Quoted 2 lines

2.43.0.687.g38aa6559b0-goog

--
Kees Cook

Signature · 1 line
宋方睿
#3 lore raw
Quoted 15 lines · Fangrui Song
Quoted 14 lines · Kees Cook
Quoted 12 lines · Fangrui Song

A s390 patch modeling its --orphan-handling= after x86 [1] sparked my motivation to simplify patterns. Commit 5354e84598f2 ("x86/build: Add asserts for unwanted sections") added asserts that certain input sections must be absent or empty. The patterns can be simplified.

For dynamic relocations,

*(.rela.*) is sufficient to match all dynamic relocations synthesized by GNU ld and LLD. .rela_* is unnecessary. --emit-relocs may create .rela_* sections for section names prefixed with _, but they are not matched by linker scripts.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

.plt instead of .plt.* is sufficient to match synthesized PLT entries.

Do you mean ".plt.foo" matches ".plt" ?

I mean we just need .plt : { *(.plt) } , not .plt : { *(.plt) *(.plt.*) }.

But then, for example, if it gets generated, .plt.got ends up being reported as an orphan...

Quoted 5 lines

The linker synthesized section for PLT entries is .plt, not suffixed.

Quoted 2 lines
Quoted 2 lines

.igot and .igot.plt are for non-preemptible STT_GNU_IFUNC in GNU ld (LLD just uses .got), which the kernel does not use. In addition, if .igot or

Right, the issue has been getting totally weird sections emitted by the linker. If you're saying you'd rather those get reported as orphan sections instead of being validated for being zero sized, and that works for all the architectures, then okay.

Quoted 14 lines
Quoted 14 lines
Quoted 2 lines

.igot.plt is ever non-empty, there will be .rela.* dynamic relocations leading to an assert failure anyway.

I think at the time I was dealing with avoid multiple warnings out of the linker, as I was getting orphan warnings in addition to the non-empty warnings.

Quoted 4 lines

[1]: https://lore.kernel.org/all/[email protected]/

Is anything harmed by leaving all of this as-is?

Quoted 7 lines
Quoted 1 line

-Kees

No harm. But ports adopting --orphan-handling= (like s390) may copy the unneeded .rela_* .
When people read .rela_*, they might think whether the kernel does anything special that
.rela_* needs to be matched.

I added these because the were being generated. See commit d1c0272bc1c0 ("x86/boot/compressed: Remove, discard, or assert for unwanted sections")

I don't want to suddenly start generating warnings for older/broken linkers. (i.e. a change like this needs really careful testing, and that needs to be detailed in the commit log.)

-Kees

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 29 lines · Kees Cook
Quoted 15 lines · Fangrui Song
Quoted 14 lines · Kees Cook
Quoted 12 lines · Fangrui Song

A s390 patch modeling its --orphan-handling= after x86 [1] sparked my motivation to simplify patterns. Commit 5354e84598f2 ("x86/build: Add asserts for unwanted sections") added asserts that certain input sections must be absent or empty. The patterns can be simplified.

For dynamic relocations,

*(.rela.*) is sufficient to match all dynamic relocations synthesized by GNU ld and LLD. .rela_* is unnecessary. --emit-relocs may create .rela_* sections for section names prefixed with _, but they are not matched by linker scripts.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

.plt instead of .plt.* is sufficient to match synthesized PLT entries.

Do you mean ".plt.foo" matches ".plt" ?

I mean we just need .plt : { *(.plt) } , not .plt : { *(.plt) *(.plt.*) }.

But then, for example, if it gets generated, .plt.got ends up being reported as an orphan...

Quoted 3 lines

The linker synthesized section for PLT entries is .plt, not suffixed.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

.igot and .igot.plt are for non-preemptible STT_GNU_IFUNC in GNU ld (LLD just uses .got), which the kernel does not use. In addition, if .igot or

Right, the issue has been getting totally weird sections emitted by the linker. If you're saying you'd rather those get reported as orphan sections instead of being validated for being zero sized, and that works for all the architectures, then okay.

Thanks. I trust my judgement here:)

Quoted 22 lines
Quoted 14 lines
Quoted 14 lines
Quoted 2 lines

.igot.plt is ever non-empty, there will be .rela.* dynamic relocations leading to an assert failure anyway.

I think at the time I was dealing with avoid multiple warnings out of the linker, as I was getting orphan warnings in addition to the non-empty warnings.

Quoted 4 lines

[1]: https://lore.kernel.org/all/[email protected]/

Is anything harmed by leaving all of this as-is?

Quoted 7 lines
Quoted 1 line

-Kees

No harm. But ports adopting --orphan-handling= (like s390) may copy the unneeded .rela_* .
When people read .rela_*, they might think whether the kernel does anything special that
.rela_* needs to be matched.

Quoted 8 lines

I added these because the were being generated. See commit d1c0272bc1c0 ("x86/boot/compressed: Remove, discard, or assert for unwanted sections")

I don't want to suddenly start generating warnings for older/broken linkers. (i.e. a change like this needs really careful testing, and that needs to be detailed in the commit log.)

-Kees

I saw this commit and still believe .rela_* is unnecessary for all supported binutils versions.
GNU ld 2.25 does not support --orphan-handling=error (2015-09 feature), but it manages to link vmlinux without any warning.

/tmp/binutils-2.25/out/release/ld/ld-new -m elf_x86_64 -z noexecstack --emit-relocs --discard-none -z max-page-size=0x200000 --build-id=sha1 --script=./arch/x86/kernel/vmlinux.lds -o vmlinux --whole-archive vmlinux.o .vmlinux.export.o init/version-timestamp.o --no-whole-archive --start-group --end-group .tmp_vmlinux.kallsyms2.o

Quoted 2 lines

--
Kees Cook

Signature · 1 line
宋方睿