Skip to contentSkip to conversation

modpost: fix RISC-V randconfig section mismatch false positives

PATCH New Jia Wang 1 participant 3 messages Jul 16

Subsystems (2)
lore
Share & tools
lore

Jia Wang

author
#0 lore raw

cover modpost: fix RISC-V randconfig section mismatch false positives

This series fixes two modpost false-positive section mismatch cases seen with RISC-V randconfig builds.

The first patch filters compiler-generated GCOV symbols and RISC-V local branch/PC-relative relocations when they point at init or exit sections. These references can come from instrumentation or local compiler/linker details rather than from real kernel section lifetime dependencies.

The second patch handles RISC-V relative exception table entries as R_RISCV_ADD32/R_RISCV_SUB32 relocation pairs. modpost already skipped the SUB32 half, but still checked the ADD32 symbol directly. After a relocatable link, that symbol can name an unrelated local debug symbol and trigger false .debug* section mismatch diagnostics from these __ex_table relocation pairs.

The issues are reproducible with both GCC 14 and GCC 15. They were reproduced or validated with:

- rv32 randconfig, KCONFIG_SEED=277518, CONFIG_GCOV_PROFILE_ALL=y, CONFIG_KCOV_INSTRUMENT_ALL=y, CONFIG_DEBUG_INFO_BTF=y

- rv32 randconfig, KCONFIG_SEED=4721,
CONFIG_MMU=y, CONFIG_DEBUG_INFO=y

With the fixes applied, the corresponding RISC-V randconfig builds complete successfully. Other unrelated modpost warnings may still remain.

---
Jia Wang (2):
modpost: ignore generated GCOV and RISC-V local relocs
modpost: handle RISC-V relative extable relocations

 scripts/mod/modpost.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 150 insertions(+), 7 deletions(-)

---
base-commit: 58717b2a1365d06c8c64b72aa948541b53fe31eb
change-id: 20260716-modpost-30d5d28d9d64

Best regards,

Signature · 1 line
Jia Wang <[email protected]>
author
#1 lore raw

PATCH 1/2 modpost: ignore generated GCOV and RISC-V local relocs

GCOV_PROFILE_ALL can make GCC emit constructor and metadata symbols that reference init or exit sections. modpost may then report section mismatches through generated symbols such as _sub_I_* and __gcov_.*, even though those references do not describe a normal section lifetime dependency in kernel code.

RISC-V also uses local branch and PC-relative relocation pairs that can resolve through compiler local or mapping symbols near init or exit code. Treating those local relocations as ordinary lifetime references creates a large number of bogus section mismatch warnings.

Teach modpost to keep the raw target symbol for generated-symbol checks and pass the relocation type into the mismatch filter. Ignore GCOV constructor and metadata symbols, compiler mapping symbols, and RISC-V local branch or PC-relative relocations when they target init or exit sections.

This was seen with an rv32 randconfig using KCONFIG_SEED=277518 with CONFIG_GCOV_PROFILE_ALL=y, CONFIG_KCOV_INSTRUMENT_ALL=y and CONFIG_DEBUG_INFO_BTF=y, where modpost emitted a large number of bogus .text to .init.text section mismatch warnings through generated GCOV and local RISC-V symbols.

This keeps named symbol section mismatch diagnostics while avoiding the warning flood from compiler-generated references.

---
 scripts/mod/modpost.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 71 insertions(+), 7 deletions(-)
scripts/mod/modpost.c +71 -7
index a7b72a81d248..6869822446bd 100644
@@ -951,8 +951,50 @@ static const struct sectioncheck *section_mismatch(
  *   refsymname = *.constprop.*
  *
  **/
+#ifndef R_RISCV_BRANCH
+#define R_RISCV_BRANCH		16
+#endif
+
+#ifndef R_RISCV_PCREL_HI20
+#define R_RISCV_PCREL_HI20	23
+#endif
+
+#ifndef R_RISCV_PCREL_LO12_I
+#define R_RISCV_PCREL_LO12_I	24
+#endif
+
+#ifndef R_RISCV_PCREL_LO12_S
+#define R_RISCV_PCREL_LO12_S	25
+#endif
+
+#ifndef R_RISCV_RVC_BRANCH
+#define R_RISCV_RVC_BRANCH	44
+#endif
+
+#ifndef R_RISCV_RVC_JUMP
+#define R_RISCV_RVC_JUMP	45
+#endif
+
+static bool is_gcov_generated_symbol(const char *sym)
+{
+	return strstarts(sym, "_sub_I_") ||
+	       strstarts(sym, "__gcov_.");
+}
+
+static bool is_riscv_local_reloc(unsigned int r_type)
+{
+	return r_type == R_RISCV_BRANCH ||
+	       r_type == R_RISCV_RVC_BRANCH ||
+	       r_type == R_RISCV_RVC_JUMP ||
+	       r_type == R_RISCV_PCREL_HI20 ||
+	       r_type == R_RISCV_PCREL_LO12_I ||
+	       r_type == R_RISCV_PCREL_LO12_S;
+}
+
 static int secref_whitelist(const char *fromsec, const char *fromsym,
-			    const char *tosec, const char *tosym)
+			    const char *tosec, const char *tosym,
+			    const char *raw_tosym, unsigned int r_type,
+			    unsigned int e_machine)
 {
 	/* Check for pattern 1 */
 	if (match(tosec, PATTERNS(ALL_INIT_DATA_SECTIONS)) &&
@@ -981,6 +1023,21 @@ static int secref_whitelist(const char *fromsec, const char *fromsym,
 	if (match(tosym, PATTERNS("__init_begin", "_sinittext", "_einittext")))
 		return 0;
 
+	/*
+	 * Compiler-generated local labels and GCOV constructors can produce
+	 * relocations that are later reported as section mismatches against
+	 * nearby init/exit symbols. Do not use those generated symbols as
+	 * evidence of a real section lifetime dependency.
+	 */
+	if (match(tosec, PATTERNS(ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS)) &&
+	    (is_mapping_symbol(raw_tosym) || is_gcov_generated_symbol(fromsym)))
+		return 0;
+
+	if (e_machine == EM_RISCV &&
+	    is_riscv_local_reloc(r_type) &&
+	    match(tosec, PATTERNS(ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS)))
+		return 0;
+
 	/* Check for pattern 5 */
 	if (match(fromsec, PATTERNS(ALL_TEXT_SECTIONS)) &&
 	    match(tosec, PATTERNS(ALL_INIT_SECTIONS)) &&
@@ -1025,21 +1082,25 @@ static void default_mismatch_handler(const char *modname, struct elf_info *elf,
 				     const struct sectioncheck* const mismatch,
 				     Elf_Sym *tsym,
 				     unsigned int fsecndx, const char *fromsec, Elf_Addr faddr,
-				     const char *tosec, Elf_Addr taddr)
+				     const char *tosec, Elf_Addr taddr,
+				     unsigned int r_type)
 {
 	Elf_Sym *from;
 	const char *tosym;
 	const char *fromsym;
 	char taddr_str[16];
+	const char *raw_tosym;
 
 	from = find_fromsym(elf, faddr, fsecndx);
 	fromsym = sym_name(elf, from);
 
+	raw_tosym = sym_name(elf, tsym);
 	tsym = find_tosym(elf, taddr, tsym);
 	tosym = sym_name(elf, tsym);
 
 	/* check whitelist - we may ignore it */
-	if (!secref_whitelist(fromsec, fromsym, tosec, tosym))
+	if (!secref_whitelist(fromsec, fromsym, tosec, tosym, raw_tosym,
+			      r_type, elf->hdr->e_machine))
 		return;
 
 	sec_mismatch_count++;
@@ -1152,7 +1213,8 @@ static void check_export_symbol(struct module *mod, struct elf_info *elf,
 static void check_section_mismatch(struct module *mod, struct elf_info *elf,
 				   Elf_Sym *sym,
 				   unsigned int fsecndx, const char *fromsec,
-				   Elf_Addr faddr, Elf_Addr taddr)
+				   Elf_Addr faddr, Elf_Addr taddr,
+				   unsigned int r_type)
 {
 	const char *tosec = sec_name(elf, get_secindex(elf, sym));
 	const struct sectioncheck *mismatch;
@@ -1168,7 +1230,7 @@ static void check_section_mismatch(struct module *mod, struct elf_info *elf,
 
 	default_mismatch_handler(mod->name, elf, mismatch, sym,
 				 fsecndx, fromsec, faddr,
-				 tosec, taddr);
+				 tosec, taddr, r_type);
 }
 
 static Elf_Addr addend_386_rel(uint32_t *location, unsigned int r_type)
@@ -1382,7 +1444,8 @@ static void section_rela(struct module *mod, struct elf_info *elf,
 		}
 
 		check_section_mismatch(mod, elf, tsym,
-				       fsecndx, fromsec, r_offset, taddr);
+				       fsecndx, fromsec, r_offset, taddr,
+				       r_type);
 	}
 }
 
@@ -1419,7 +1482,8 @@ static void section_rel(struct module *mod, struct elf_info *elf,
 		}
 
 		check_section_mismatch(mod, elf, tsym,
-				       fsecndx, fromsec, r_offset, taddr);
+				       fsecndx, fromsec, r_offset, taddr,
+				       r_type);
 	}
 }
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/2 modpost: handle RISC-V relative extable relocations

In reply to Jia Wang

RISC-V relative exception tables emit an R_RISCV_ADD32 and R_RISCV_SUB32 relocation pair for each field. modpost already ignores the SUB32 half, but still checks the ADD32 symbol section directly.

After a relocatable link, that ADD32 symbol can name an unrelated local debug symbol. This makes modpost report that __ex_table references non-executable .debug* sections even though the original exception table entry points at text.

Recognize the RISC-V ADD32/SUB32 pair for __ex_table. Ignore ADD32 entries that have been resolved to debug symbols, and otherwise validate the resolved relative target address against executable sections.

This was seen with an rv32 randconfig using KCONFIG_SEED=4721 with CONFIG_MMU=y and CONFIG_DEBUG_INFO=y. The resulting vmlinux.o contains R_RISCV_ADD32/R_RISCV_SUB32 relocation pairs in __ex_table, and the fixed modpost run completes without the false .debug* section mismatch diagnostics.

---
 scripts/mod/modpost.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
scripts/mod/modpost.c +79 -0
index 6869822446bd..04a12eebb381 100644
@@ -1356,6 +1356,10 @@ static Elf_Addr addend_mips_rel(uint32_t *location, unsigned int r_type)
 #define EM_RISCV		243
 #endif
 
+#ifndef R_RISCV_ADD32
+#define R_RISCV_ADD32		35
+#endif
+
 #ifndef R_RISCV_SUB32
 #define R_RISCV_SUB32		39
 #endif
@@ -1406,6 +1410,76 @@ static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info,
 	*r_sym = ELF_R_SYM(r_info);
 }
 
+static bool section_contains_addr(struct elf_info *elf, unsigned int secndx,
+				  Elf_Addr addr)
+{
+	Elf_Shdr *sechdr;
+
+	if (secndx >= elf->num_sections)
+		return false;
+
+	sechdr = &elf->sechdrs[secndx];
+
+	return addr >= sechdr->sh_addr &&
+	       addr - sechdr->sh_addr < sechdr->sh_size;
+}
+
+static bool addr_is_executable(struct elf_info *elf, Elf_Addr addr)
+{
+	unsigned int i;
+
+	for (i = 1; i < elf->num_sections; i++) {
+		if ((elf->sechdrs[i].sh_flags & SHF_EXECINSTR) &&
+		    section_contains_addr(elf, i, addr))
+			return true;
+	}
+
+	return false;
+}
+
+static bool is_debug_section(const char *secname)
+{
+	return match(secname, PATTERNS(".debug*", ".zdebug*"));
+}
+
+static bool riscv_extable_rela_valid(struct elf_info *elf,
+				     const Elf_Rela *start,
+				     const Elf_Rela *stop,
+				     const Elf_Rela *rela,
+				     Elf_Addr r_offset,
+				     Elf_Sym *add_sym)
+{
+	Elf_Addr add_addr, sub_addr, target_addr;
+	unsigned int r_type, r_sym;
+	const Elf_Rela *pair;
+	Elf_Sym *sub_sym;
+
+	/*
+	 * RISC-V relative exception tables emit an ADD32/SUB32 pair for each
+	 * field. After ld -r, the ADD32 symbol may name an unrelated local
+	 * debug symbol. Otherwise, validate the resolved relative target.
+	 */
+	for (pair = start; pair < stop; pair++) {
+		get_rel_type_and_sym(elf, pair->r_info, &r_type, &r_sym);
+		if (TO_NATIVE(pair->r_offset) == r_offset &&
+		    r_type == R_RISCV_SUB32)
+			break;
+	}
+
+	if (pair == stop)
+		return false;
+
+	sub_sym = elf->symtab_start + r_sym;
+	if (is_debug_section(sec_name(elf, get_secindex(elf, add_sym))))
+		return true;
+
+	add_addr = add_sym->st_value + TO_NATIVE(rela->r_addend);
+	sub_addr = sub_sym->st_value + TO_NATIVE(pair->r_addend);
+	target_addr = r_offset + add_addr - sub_addr;
+
+	return addr_is_executable(elf, target_addr);
+}
+
 static void section_rela(struct module *mod, struct elf_info *elf,
 			 unsigned int fsecndx, const char *fromsec,
 			 const Elf_Rela *start, const Elf_Rela *stop)
@@ -1428,6 +1502,11 @@ static void section_rela(struct module *mod, struct elf_info *elf,
 			if (!strcmp("__ex_table", fromsec) &&
 			    r_type == R_RISCV_SUB32)
 				continue;
+			if (!strcmp("__ex_table", fromsec) &&
+			    r_type == R_RISCV_ADD32 &&
+			    riscv_extable_rela_valid(elf, start, stop, rela,
+						     r_offset, tsym))
+				continue;
 			break;
 		case EM_LOONGARCH:
 			switch (r_type) {
Signature · 1 line
2.34.1