All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb+git@google.com>
To: linux-kernel@vger.kernel.org
Cc: x86@kernel.org, Huacai Chen <chenhuacai@kernel.org>,
	Ard Biesheuvel <ardb@kernel.org>,
	 Josh Poimboeuf <jpoimboe@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	 Tiezhu Yang <yangtiezhu@loongson.cn>
Subject: [PATCH] objtool: Use idiomatic section name for relocatable rodata under PIE
Date: Tue, 18 Feb 2025 10:25:39 +0100	[thread overview]
Message-ID: <20250218092538.1903204-2-ardb+git@google.com> (raw)

From: Ard Biesheuvel <ardb@kernel.org>

When running in PIE mode, the compiler will emit const global objects
into .data.rel.ro rather than into .rodata if those objects contain
statically initialized fields carrying addresses that are subject to
runtime relocation (e.g., function pointers).

This is needed so that the user space runtime linker can identify which
parts of the executable image need to be writable initially, but can be
converted into read-only before the image starts executing.

This distinction does not matter for the kernel, but when using the
compiler in PIE mode (such as when building for LoongArch), those
.data.rel.ro sections need to be treated as .rodata as well.

It also means that manually placed const global objects that contain
absolute addresses (such as the non-JIT BPF jump table) need to be
emitted into .data.rel.ro too so that the linker does not complain about
conflicting permissions.

Cc: Josh Poimboeuf <jpoimboe@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
Please consider this approach instead of the ..rodata hack - thanks.

 include/asm-generic/vmlinux.lds.h       | 2 +-
 include/linux/compiler.h                | 6 +++++-
 tools/objtool/check.c                   | 7 +++----
 tools/objtool/include/objtool/special.h | 2 +-
 4 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 91a7e824ed8b..337d3336e175 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -457,7 +457,7 @@ defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPELLER_CLANG)
 	. = ALIGN((align));						\
 	.rodata           : AT(ADDR(.rodata) - LOAD_OFFSET) {		\
 		__start_rodata = .;					\
-		*(.rodata) *(.rodata.*) *(..rodata.*)			\
+		*(.rodata) *(.rodata.*) *(.data.rel.ro*)		\
 		SCHED_DATA						\
 		RO_AFTER_INIT_DATA	/* Read only after init */	\
 		. = ALIGN(8);						\
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 3d013f1412e0..27024a128a6a 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -110,7 +110,11 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 /* Unreachable code */
 #ifdef CONFIG_OBJTOOL
 /* Annotate a C jump table to allow objtool to follow the code flow */
-#define __annotate_jump_table __section("..rodata.c_jump_table")
+#ifndef __pie__
+#define __annotate_jump_table __section(".rodata.c_jump_table")
+#else
+#define __annotate_jump_table __section(".data.rel.ro.c_jump_table")
+#endif
 #else /* !CONFIG_OBJTOOL */
 #define __annotate_jump_table
 #endif /* CONFIG_OBJTOOL */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 1398ffc20b16..898d0cee4565 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2471,14 +2471,13 @@ static void mark_rodata(struct objtool_file *file)
 	 *
 	 * - .rodata: can contain GCC switch tables
 	 * - .rodata.<func>: same, if -fdata-sections is being used
-	 * - ..rodata.c_jump_table: contains C annotated jump tables
+	 * - .data.rel.ro: same when using -fPIE codegen
 	 *
 	 * .rodata.str1.* sections are ignored; they don't contain jump tables.
 	 */
 	for_each_sec(file, sec) {
-		if ((!strncmp(sec->name, ".rodata", 7) ||
-		    !strncmp(sec->name, "..rodata", 8)) &&
-		    !strstr(sec->name, ".str1.")) {
+		if ((!strncmp(sec->name, ".rodata", 7) && !strstr(sec->name, ".str1.")) ||
+		    !strncmp(sec->name, ".data.rel.ro", 12)) {
 			sec->rodata = true;
 			found = true;
 		}
diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/include/objtool/special.h
index 34acf4ae5fab..e049679bb17b 100644
--- a/tools/objtool/include/objtool/special.h
+++ b/tools/objtool/include/objtool/special.h
@@ -10,7 +10,7 @@
 #include <objtool/check.h>
 #include <objtool/elf.h>
 
-#define C_JUMP_TABLE_SECTION "..rodata.c_jump_table"
+#define C_JUMP_TABLE_SECTION ".data.rel.ro.c_jump_table"
 
 struct special_alt {
 	struct list_head list;
-- 
2.48.1.601.g30ceb7b040-goog


             reply	other threads:[~2025-02-18  9:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-18  9:25 Ard Biesheuvel [this message]
2025-02-18  9:29 ` [PATCH] objtool: Use idiomatic section name for relocatable rodata under PIE Ard Biesheuvel
2025-02-19  0:53   ` Tiezhu Yang
2025-02-18 17:38 ` Josh Poimboeuf
2025-02-18 17:44   ` Ard Biesheuvel
2025-02-18 18:31     ` Josh Poimboeuf
2025-02-18 19:36       ` Ard Biesheuvel
2025-02-18 20:18         ` Josh Poimboeuf
2025-02-19  9:52       ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250218092538.1903204-2-ardb+git@google.com \
    --to=ardb+git@google.com \
    --cc=ardb@kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=x86@kernel.org \
    --cc=yangtiezhu@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.