Re: build failure caused by RUNTIME_CONST()

From: Linus Torvalds
Date: Fri Aug 02 2024 - 21:19:46 EST


On Fri, 2 Aug 2024 at 15:10, Oleg Nesterov <oleg@xxxxxxxxxx> wrote:
>
> $ ld -v
> GNU ld version 2.25-17.fc23

Yeah, we document that we support building with ld-2.25. And I went
and looked into the binutils-gdb repo, and it looks like this whole
automatic start/stop symbol thing was introduced in 2.29.

> I can try to polish my hack, but as I said I don't understand this magic,
> and I can only test the build on x86.

Your hack doesn't look horrific to me - or at least no more horrific
than lds files always look.

I ended up with a slightly different approach, only because I'm
(probably entirely in vain) hoping that we might aim to use this
"standard" format of start/stop symbols, so I introduced it as some
kind of simple "NAMED_SECTION()" macro instead.

So this patch seems to work for me, and looks somewhat reasonable (if
people actually start using this and want to use different alignments,
we might have to make that alignment an argument in the future, but
let's go for a really simple macro interface for now).

Does it build and work for you with your old linker too?

Linus
From eb4ad08d935b5081f4c998543623fabfd2162e07 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 2 Aug 2024 18:12:06 -0700
Subject: [PATCH] runtime constants: deal with old decrepit linkers
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The runtime constants linker script depended on documented linker
behavior [1]:

"If an output section’s name is the same as the input section’s name
and is representable as a C identifier, then the linker will
automatically PROVIDE two symbols: __start_SECNAME and __stop_SECNAME,
where SECNAME is the name of the section. These indicate the start
address and end address of the output section respectively"

to just automatically define the symbol names for the bounds of the
runtime constant arrays.

It turns out that this isn't actually something we can rely on, with old
linkers not generating these automatic symbols. It looks to have been
introduced in binutils-2.29 back in 2017, and we still support building
with versions all the way back to binutils-2.25 (from 2015).

And yes, Oleg actually seems to be using such ancient versions of
binutils.

So instead of depending on the implicit symbols from "section names
match and are representable C identifiers", just do this all manually.
It's not like it causes us any extra pain, we already have to do that
for all the other sections that we use that often have special
characters in them.

Reported-by: Oleg Nesterov <oleg@xxxxxxxxxx>
Link: https://sourceware.org/binutils/docs/ld/Input-Section-Example.html [1]
Link: https://lore.kernel.org/all/20240802114518.GA20924@xxxxxxxxxx/
Signed-off-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
---
include/asm-generic/vmlinux.lds.h | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index ad6afc5c4918..f6a99e5ab439 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -911,13 +911,11 @@
#define CON_INITCALL \
BOUNDED_SECTION_POST_LABEL(.con_initcall.init, __con_initcall, _start, _end)

-#define RUNTIME_NAME(t,x) runtime_##t##_##x
+#define NAMED_SECTION(name) \
+ name : AT(ADDR(name) - LOAD_OFFSET) ALIGN(8) \
+ { BOUNDED_SECTION_PRE_LABEL(name, name, __start_, __stop_) }

-#define RUNTIME_CONST(t,x) \
- . = ALIGN(8); \
- RUNTIME_NAME(t,x) : AT(ADDR(RUNTIME_NAME(t,x)) - LOAD_OFFSET) { \
- *(RUNTIME_NAME(t,x)); \
- }
+#define RUNTIME_CONST(t,x) NAMED_SECTION(runtime_##t##_##x)

/* Alignment must be consistent with (kunit_suite *) in include/kunit/test.h */
#define KUNIT_TABLE() \
--
2.46.0.rc1.5.g1eba42a021