Skip to contentSkip to conversation

modpost: move __attribute__((format(printf, 2, 3))) to modpost.h

PATCH New Masahiro Yamada 1 participant 4 messages Dec 3, 2023 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 modpost: move __attribute__((format(printf, 2, 3))) to modpost.h

This attribute must be added to the function declaration in a header for comprehensive checking of all the callsites.

Fixes 6d9a89ea4b06 ("kbuild: declare the modpost error functions as printf like")

---

(no changes since v1)

 scripts/mod/modpost.c | 3 +--
 scripts/mod/modpost.h | 3 ++-
 2 files changed, 3 insertions(+), 3 deletions(-)
scripts/mod/modpost.c +1 -2
index cb6406f485a9..ca0a90158f85 100644
@@ -60,8 +60,7 @@ static unsigned int nr_unresolved;
 
 #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
 
-void __attribute__((format(printf, 2, 3)))
-modpost_log(enum loglevel loglevel, const char *fmt, ...)
+void modpost_log(enum loglevel loglevel, const char *fmt, ...)
 {
 	va_list arglist;
 
scripts/mod/modpost.h +2 -1
index 69baf014da4f..9fe974dc1a52 100644
@@ -197,7 +197,8 @@ enum loglevel {
 	LOG_FATAL
 };
 
-void modpost_log(enum loglevel loglevel, const char *fmt, ...);
+void __attribute__((format(printf, 2, 3)))
+modpost_log(enum loglevel loglevel, const char *fmt, ...);
 
 /*
  * warn - show the given message, then let modpost continue running, still
Signature · 1 line
2.40.1
#1 lore raw

PATCH v2 2/4 modpost: inform compilers that fatal() never returns

The function fatal() never returns because modpost_log() calls exit(1) when LOG_FATAL is passed.

Inform compilers of this fact so that unreachable code flow can be identified at compile time.

---

Changes in v2:
- Use noreturn attribute together with alias

 scripts/mod/modpost.c | 3 +++
 scripts/mod/modpost.h | 5 ++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
scripts/mod/modpost.c +3 -0
index ca0a90158f85..c13bc9095df3 100644
@@ -90,6 +90,9 @@ void modpost_log(enum loglevel loglevel, const char *fmt, ...)
 		error_occurred = true;
 }
 
+void __attribute__((alias("modpost_log")))
+modpost_log_noret(enum loglevel loglevel, const char *fmt, ...);
+
 static inline bool strends(const char *str, const char *postfix)
 {
 	if (strlen(str) < strlen(postfix))
scripts/mod/modpost.h +4 -1
index 9fe974dc1a52..835cababf1b0 100644
@@ -200,6 +200,9 @@ enum loglevel {
 void __attribute__((format(printf, 2, 3)))
 modpost_log(enum loglevel loglevel, const char *fmt, ...);
 
+void __attribute__((format(printf, 2, 3), noreturn))
+modpost_log_noret(enum loglevel loglevel, const char *fmt, ...);
+
 /*
  * warn - show the given message, then let modpost continue running, still
  *        allowing modpost to exit successfully. This should be used when
@@ -215,4 +218,4 @@ modpost_log(enum loglevel loglevel, const char *fmt, ...);
  */
 #define warn(fmt, args...)	modpost_log(LOG_WARN, fmt, ##args)
 #define error(fmt, args...)	modpost_log(LOG_ERROR, fmt, ##args)
-#define fatal(fmt, args...)	modpost_log(LOG_FATAL, fmt, ##args)
+#define fatal(fmt, args...)	modpost_log_noret(LOG_FATAL, fmt, ##args)
Signature · 1 line
2.40.1
#2 lore raw

PATCH v2 3/4 modpost: remove unneeded initializer in section_rel()

In reply to Masahiro Yamada

This initializer was added to avoid -Wmaybe-uninitialized (gcc) or -Wsometimes-uninitialized (clang) warning.

Now that compilers recognize fatal() never returns, it is unneeded.

---

Changes in v2:
- Split into a separate patch

 scripts/mod/modpost.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/mod/modpost.c +1 -1
index c13bc9095df3..3233946fa5f6 100644
@@ -1421,7 +1421,7 @@ static void section_rel(struct module *mod, struct elf_info *elf,
 
 	for (rel = start; rel < stop; rel++) {
 		Elf_Sym *tsym;
-		Elf_Addr taddr = 0, r_offset;
+		Elf_Addr taddr, r_offset;
 		unsigned int r_type, r_sym;
 		void *loc;
 
Signature · 1 line
2.40.1
#3 lore raw

PATCH v2 4/4 modpost: remove unreachable code after fatal()

In reply to Masahiro Yamada

Now compilers can recognize fatal() never returns.

While GCC 4.5 dropped support for -Wunreachable-code, Clang is capable of detecting the unreachable code.

  $ make HOSTCC=clang HOSTCFLAGS=-Wunreachable-code-return
      [snip]
    HOSTCC  scripts/mod/modpost.o
  scripts/mod/modpost.c:520:11: warning: 'return' will never be executed [-Wunreachable-code-return]
                          return 0;
                                 ^
  scripts/mod/modpost.c:477:10: warning: 'return' will never be executed [-Wunreachable-code-return]
                  return 0;
                         ^
  2 warnings generated.

---

Changes in v2:
- Change the patch order
- Update the commit description and add warning example.

 scripts/mod/modpost.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
scripts/mod/modpost.c +3 -6
index 3233946fa5f6..e2bc180cecc8 100644
@@ -476,11 +476,9 @@ static int parse_elf(struct elf_info *info, const char *filename)
 		fatal("%s: not relocatable object.", filename);
 
 	/* Check if file offset is correct */
-	if (hdr->e_shoff > info->size) {
+	if (hdr->e_shoff > info->size)
 		fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
 		      (unsigned long)hdr->e_shoff, filename, info->size);
-		return 0;
-	}
 
 	if (hdr->e_shnum == SHN_UNDEF) {
 		/*
@@ -518,12 +516,11 @@ static int parse_elf(struct elf_info *info, const char *filename)
 		const char *secname;
 		int nobits = sechdrs[i].sh_type == SHT_NOBITS;
 
-		if (!nobits && sechdrs[i].sh_offset > info->size) {
+		if (!nobits && sechdrs[i].sh_offset > info->size)
 			fatal("%s is truncated. sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
 			      filename, (unsigned long)sechdrs[i].sh_offset,
 			      sizeof(*hdr));
-			return 0;
-		}
+
 		secname = secstrings + sechdrs[i].sh_name;
 		if (strcmp(secname, ".modinfo") == 0) {
 			if (nobits)
Signature · 1 line
2.40.1