linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] x86, relocs: add function attributes to die()
@ 2016-09-03 14:50 Nicolas Iooss
  2016-09-03 15:16 ` Nilay Vaish
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Iooss @ 2016-09-03 14:50 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86
  Cc: linux-kernel, Nicolas Iooss

When building the kernel with clang and some warning flags, the compiler
reports the following warning:

    arch/x86/tools/relocs.c:979:6: warning: variable 'do_reloc' is used
    uninitialized whenever 'if' condition is false
    [-Wsometimes-uninitialized]
            if (!use_real_mode)
                ^~~~~~~~~~~~~~
    arch/x86/tools/relocs.c:991:14: note: uninitialized use occurs here
            walk_relocs(do_reloc);
                        ^~~~~~~~
    arch/x86/tools/relocs.c:979:2: note: remove the 'if' if its
    condition is always true
            if (!use_real_mode)
            ^~~~~~~~~~~~~~~~~~~
    arch/x86/tools/relocs.c:976:24: note: initialize the variable
    'do_reloc' to silence this warning
                            const char *symname);
                                                ^
                                                 = NULL

This is obviously a false positive: whenever the 'if' condition is
false, the program calls die(). Nevertheless the compiler did not know
this call makes the program quit because this function did not have a
noreturn attribute. Add it.

While at it, add a printf attribute too to die() and constify the format
parameter. This leads to some errors when compiling on x86_64:

    arch/x86/tools/relocs.c:460:5: error: format specifies type 'int'
    but the argument has type 'Elf64_Xword' (aka 'unsigned long')
    [-Werror,-Wformat]
                                    sec->shdr.sh_size);
                                    ^~~~~~~~~~~~~~~~~
    arch/x86/tools/relocs.c:464:5: error: format specifies type 'int'
    but the argument has type 'Elf64_Off' (aka 'unsigned long')
    [-Werror,-Wformat]
                                    sec->shdr.sh_offset, strerror(errno));
                                    ^~~~~~~~~~~~~~~~~~~

To support both 32-bit and 64-bit modes, add casts to long types and use
%lu and %ld to format the numbers.

Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
---
 arch/x86/tools/relocs.c        | 28 ++++++++++++++--------------
 arch/x86/tools/relocs.h        |  3 ++-
 arch/x86/tools/relocs_common.c |  2 +-
 3 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index 0c2fae8d929d..88d9595ee4de 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -397,8 +397,8 @@ static void read_shdrs(FILE *fp)
 		    ehdr.e_shnum);
 	}
 	if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
-		die("Seek to %d failed: %s\n",
-			ehdr.e_shoff, strerror(errno));
+		die("Seek to %lu failed: %s\n",
+			(unsigned long)ehdr.e_shoff, strerror(errno));
 	}
 	for (i = 0; i < ehdr.e_shnum; i++) {
 		struct section *sec = &secs[i];
@@ -431,12 +431,12 @@ static void read_strtabs(FILE *fp)
 		}
 		sec->strtab = malloc(sec->shdr.sh_size);
 		if (!sec->strtab) {
-			die("malloc of %d bytes for strtab failed\n",
-				sec->shdr.sh_size);
+			die("malloc of %lu bytes for strtab failed\n",
+				(unsigned long)sec->shdr.sh_size);
 		}
 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %d failed: %s\n",
-				sec->shdr.sh_offset, strerror(errno));
+			die("Seek to %ld failed: %s\n",
+				(long)sec->shdr.sh_offset, strerror(errno));
 		}
 		if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
 		    != sec->shdr.sh_size) {
@@ -456,12 +456,12 @@ static void read_symtabs(FILE *fp)
 		}
 		sec->symtab = malloc(sec->shdr.sh_size);
 		if (!sec->symtab) {
-			die("malloc of %d bytes for symtab failed\n",
-				sec->shdr.sh_size);
+			die("malloc of %lu bytes for symtab failed\n",
+				(unsigned long)sec->shdr.sh_size);
 		}
 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %d failed: %s\n",
-				sec->shdr.sh_offset, strerror(errno));
+			die("Seek to %ld failed: %s\n",
+				(long)sec->shdr.sh_offset, strerror(errno));
 		}
 		if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
 		    != sec->shdr.sh_size) {
@@ -489,12 +489,12 @@ static void read_relocs(FILE *fp)
 		}
 		sec->reltab = malloc(sec->shdr.sh_size);
 		if (!sec->reltab) {
-			die("malloc of %d bytes for relocs failed\n",
-				sec->shdr.sh_size);
+			die("malloc of %lu bytes for relocs failed\n",
+				(unsigned long)sec->shdr.sh_size);
 		}
 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %d failed: %s\n",
-				sec->shdr.sh_offset, strerror(errno));
+			die("Seek to %ld failed: %s\n",
+				(long)sec->shdr.sh_offset, strerror(errno));
 		}
 		if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
 		    != sec->shdr.sh_size) {
diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
index f59590645b68..6b7969719833 100644
--- a/arch/x86/tools/relocs.h
+++ b/arch/x86/tools/relocs.h
@@ -16,7 +16,8 @@
 #include <regex.h>
 #include <tools/le_byteshift.h>
 
-void die(char *fmt, ...);
+__attribute__((format(printf, 1, 2))) __attribute__((noreturn))
+void die(const char *fmt, ...);
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
diff --git a/arch/x86/tools/relocs_common.c b/arch/x86/tools/relocs_common.c
index acab636bcb34..30adb44eff79 100644
--- a/arch/x86/tools/relocs_common.c
+++ b/arch/x86/tools/relocs_common.c
@@ -1,6 +1,6 @@
 #include "relocs.h"
 
-void die(char *fmt, ...)
+void die(const char *fmt, ...)
 {
 	va_list ap;
 	va_start(ap, fmt);
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] x86, relocs: add function attributes to die()
  2016-09-03 14:50 [PATCH 1/1] x86, relocs: add function attributes to die() Nicolas Iooss
@ 2016-09-03 15:16 ` Nilay Vaish
  2016-09-03 15:51   ` Nicolas Iooss
  0 siblings, 1 reply; 6+ messages in thread
From: Nilay Vaish @ 2016-09-03 15:16 UTC (permalink / raw)
  To: Nicolas Iooss
  Cc: Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86, Linux Kernel list

On 3 September 2016 at 09:50, Nicolas Iooss <nicolas.iooss_linux@m4x.org> wrote:
>
>     arch/x86/tools/relocs.c:460:5: error: format specifies type 'int'
>     but the argument has type 'Elf64_Xword' (aka 'unsigned long')
>     [-Werror,-Wformat]
>                                     sec->shdr.sh_size);
>                                     ^~~~~~~~~~~~~~~~~
>     arch/x86/tools/relocs.c:464:5: error: format specifies type 'int'
>     but the argument has type 'Elf64_Off' (aka 'unsigned long')
>     [-Werror,-Wformat]
>                                     sec->shdr.sh_offset, strerror(errno));
>                                     ^~~~~~~~~~~~~~~~~~~
>
> To support both 32-bit and 64-bit modes, add casts to long types and use
> %lu and %ld to format the numbers.
>

Nicolas,  should not just changing the format specifiers fix the
problem?  How do those type casts help?

--
Nilay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] x86, relocs: add function attributes to die()
  2016-09-03 15:16 ` Nilay Vaish
@ 2016-09-03 15:51   ` Nicolas Iooss
  2016-09-04 16:51     ` [PATCH v2 " Nicolas Iooss
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Iooss @ 2016-09-03 15:51 UTC (permalink / raw)
  To: Nilay Vaish
  Cc: Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86, Linux Kernel list

On 03/09/16 17:16, Nilay Vaish wrote:
> On 3 September 2016 at 09:50, Nicolas Iooss <nicolas.iooss_linux@m4x.org> wrote:
>>
>>     arch/x86/tools/relocs.c:460:5: error: format specifies type 'int'
>>     but the argument has type 'Elf64_Xword' (aka 'unsigned long')
>>     [-Werror,-Wformat]
>>                                     sec->shdr.sh_size);
>>                                     ^~~~~~~~~~~~~~~~~
>>     arch/x86/tools/relocs.c:464:5: error: format specifies type 'int'
>>     but the argument has type 'Elf64_Off' (aka 'unsigned long')
>>     [-Werror,-Wformat]
>>                                     sec->shdr.sh_offset, strerror(errno));
>>                                     ^~~~~~~~~~~~~~~~~~~
>>
>> To support both 32-bit and 64-bit modes, add casts to long types and use
>> %lu and %ld to format the numbers.
>>
> 
> Nicolas,  should not just changing the format specifiers fix the
> problem?  How do those type casts help?

The problem is that I did not found a simple format which would make
both arch/x86/tools/relocs_32.o and arch/x86/tools/relocs_64.o build
without any warning. Here are the types:

* When compiling relocs_32.c, sec->shdr.sh_size is of type Elf32_Word
(unsigned int) and sec->shdr.sh_offset is Elf32_Off (unsigned int).
* When compiling relocs_64.c, sec->shdr.sh_size is Elf64_Xword (long
long unsigned int when the compiler is in 32-bit mode, long unsigned int
in 64-bit mode) and sec->shdr.sh_offset is Elf64_Off (same real type as
sec->shdr.sh_size).

I though that casting everything to long integers was fine but now I
have realized this does not take into account compiling relocs_64.c on a
32-bit host system (where HOSTCC compiles in 32-bit mode and long
integers are 32-bit wide).

A possible solution would be using format definitions from <inittypes.h>
like PRIu32, PRIu64... in relocs.c depending on the file being compiled
(relocs_32.c or relocs_64.c). What would you think of such a solution?

-- Nicolas

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/1] x86, relocs: add function attributes to die()
  2016-09-03 15:51   ` Nicolas Iooss
@ 2016-09-04 16:51     ` Nicolas Iooss
  2016-09-05 15:28       ` Nilay Vaish
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Iooss @ 2016-09-04 16:51 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86
  Cc: linux-kernel, Nilay Vaish, Nicolas Iooss

When building the kernel with clang and some warning flags, the compiler
reports the following warning:

    arch/x86/tools/relocs.c:979:6: warning: variable 'do_reloc' is used
    uninitialized whenever 'if' condition is false
    [-Wsometimes-uninitialized]
            if (!use_real_mode)
                ^~~~~~~~~~~~~~
    arch/x86/tools/relocs.c:991:14: note: uninitialized use occurs here
            walk_relocs(do_reloc);
                        ^~~~~~~~
    arch/x86/tools/relocs.c:979:2: note: remove the 'if' if its
    condition is always true
            if (!use_real_mode)
            ^~~~~~~~~~~~~~~~~~~
    arch/x86/tools/relocs.c:976:24: note: initialize the variable
    'do_reloc' to silence this warning
                            const char *symname);
                                                ^
                                                 = NULL

This is obviously a false positive: whenever the 'if' condition is
false, the program calls die(). Nevertheless the compiler did not know
this call makes the program quit because this function did not have a
noreturn attribute. Add it.

While at it, add a printf attribute too to die() and constify the format
parameter. This leads to some errors when compiling on x86_64:

    arch/x86/tools/relocs.c:460:5: error: format specifies type 'int'
    but the argument has type 'Elf64_Xword' (aka 'unsigned long')
    [-Werror,-Wformat]
                                    sec->shdr.sh_size);
                                    ^~~~~~~~~~~~~~~~~
    arch/x86/tools/relocs.c:464:5: error: format specifies type 'int'
    but the argument has type 'Elf64_Off' (aka 'unsigned long')
    [-Werror,-Wformat]
                                    sec->shdr.sh_offset, strerror(errno));
                                    ^~~~~~~~~~~~~~~~~~~

When relocs.c is included by relocs_32.c, sec->shdr.sh_size and
sec->shdr.sh_offset are 32-bit unsigned integers. When the file is
included by relocs_64.c, these expressions are 64-bit unsigned integers.

Introduce a PRIuELF macro to define the right format to use when
printing these expressions.

Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
---
 arch/x86/tools/relocs.c        | 14 +++++++-------
 arch/x86/tools/relocs.h        |  3 ++-
 arch/x86/tools/relocs_32.c     |  3 +++
 arch/x86/tools/relocs_64.c     |  3 +++
 arch/x86/tools/relocs_common.c |  2 +-
 5 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index 0c2fae8d929d..4cad603b8d58 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -397,7 +397,7 @@ static void read_shdrs(FILE *fp)
 		    ehdr.e_shnum);
 	}
 	if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
-		die("Seek to %d failed: %s\n",
+		die("Seek to %"PRIuELF" failed: %s\n",
 			ehdr.e_shoff, strerror(errno));
 	}
 	for (i = 0; i < ehdr.e_shnum; i++) {
@@ -431,11 +431,11 @@ static void read_strtabs(FILE *fp)
 		}
 		sec->strtab = malloc(sec->shdr.sh_size);
 		if (!sec->strtab) {
-			die("malloc of %d bytes for strtab failed\n",
+			die("malloc of %"PRIuELF" bytes for strtab failed\n",
 				sec->shdr.sh_size);
 		}
 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %d failed: %s\n",
+			die("Seek to %"PRIuELF" failed: %s\n",
 				sec->shdr.sh_offset, strerror(errno));
 		}
 		if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
@@ -456,11 +456,11 @@ static void read_symtabs(FILE *fp)
 		}
 		sec->symtab = malloc(sec->shdr.sh_size);
 		if (!sec->symtab) {
-			die("malloc of %d bytes for symtab failed\n",
+			die("malloc of %"PRIuELF" bytes for symtab failed\n",
 				sec->shdr.sh_size);
 		}
 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %d failed: %s\n",
+			die("Seek to %"PRIuELF" failed: %s\n",
 				sec->shdr.sh_offset, strerror(errno));
 		}
 		if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
@@ -489,11 +489,11 @@ static void read_relocs(FILE *fp)
 		}
 		sec->reltab = malloc(sec->shdr.sh_size);
 		if (!sec->reltab) {
-			die("malloc of %d bytes for relocs failed\n",
+			die("malloc of %"PRIuELF" bytes for relocs failed\n",
 				sec->shdr.sh_size);
 		}
 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %d failed: %s\n",
+			die("Seek to %"PRIuELF" failed: %s\n",
 				sec->shdr.sh_offset, strerror(errno));
 		}
 		if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
index f59590645b68..6b7969719833 100644
--- a/arch/x86/tools/relocs.h
+++ b/arch/x86/tools/relocs.h
@@ -16,7 +16,8 @@
 #include <regex.h>
 #include <tools/le_byteshift.h>
 
-void die(char *fmt, ...);
+__attribute__((format(printf, 1, 2))) __attribute__((noreturn))
+void die(const char *fmt, ...);
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
diff --git a/arch/x86/tools/relocs_32.c b/arch/x86/tools/relocs_32.c
index b2ade2bb4162..8024ec473e6a 100644
--- a/arch/x86/tools/relocs_32.c
+++ b/arch/x86/tools/relocs_32.c
@@ -14,4 +14,7 @@
 #define ELF_ST_BIND(o)		ELF32_ST_BIND(o)
 #define ELF_ST_VISIBILITY(o)	ELF32_ST_VISIBILITY(o)
 
+/* printf format for Elf32_Off */
+#define PRIuELF PRIu32
+
 #include "relocs.c"
diff --git a/arch/x86/tools/relocs_64.c b/arch/x86/tools/relocs_64.c
index 56b61b743c4c..2cf4de5c9d99 100644
--- a/arch/x86/tools/relocs_64.c
+++ b/arch/x86/tools/relocs_64.c
@@ -14,4 +14,7 @@
 #define ELF_ST_BIND(o)          ELF64_ST_BIND(o)
 #define ELF_ST_VISIBILITY(o)    ELF64_ST_VISIBILITY(o)
 
+/* printf format for Elf64_Off */
+#define PRIuELF PRIu64
+
 #include "relocs.c"
diff --git a/arch/x86/tools/relocs_common.c b/arch/x86/tools/relocs_common.c
index acab636bcb34..30adb44eff79 100644
--- a/arch/x86/tools/relocs_common.c
+++ b/arch/x86/tools/relocs_common.c
@@ -1,6 +1,6 @@
 #include "relocs.h"
 
-void die(char *fmt, ...)
+void die(const char *fmt, ...)
 {
 	va_list ap;
 	va_start(ap, fmt);
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/1] x86, relocs: add function attributes to die()
  2016-09-04 16:51     ` [PATCH v2 " Nicolas Iooss
@ 2016-09-05 15:28       ` Nilay Vaish
  2016-10-23 16:47         ` Nicolas Iooss
  0 siblings, 1 reply; 6+ messages in thread
From: Nilay Vaish @ 2016-09-05 15:28 UTC (permalink / raw)
  To: Nicolas Iooss
  Cc: Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86, Linux Kernel list

On 4 September 2016 at 11:51, Nicolas Iooss <nicolas.iooss_linux@m4x.org> wrote:
> When building the kernel with clang and some warning flags, the compiler
> reports the following warning:
>
>     arch/x86/tools/relocs.c:979:6: warning: variable 'do_reloc' is used
>     uninitialized whenever 'if' condition is false
>     [-Wsometimes-uninitialized]
>             if (!use_real_mode)
>                 ^~~~~~~~~~~~~~
>     arch/x86/tools/relocs.c:991:14: note: uninitialized use occurs here
>             walk_relocs(do_reloc);
>                         ^~~~~~~~
>     arch/x86/tools/relocs.c:979:2: note: remove the 'if' if its
>     condition is always true
>             if (!use_real_mode)
>             ^~~~~~~~~~~~~~~~~~~
>     arch/x86/tools/relocs.c:976:24: note: initialize the variable
>     'do_reloc' to silence this warning
>                             const char *symname);
>                                                 ^
>                                                  = NULL
>
> This is obviously a false positive: whenever the 'if' condition is
> false, the program calls die(). Nevertheless the compiler did not know
> this call makes the program quit because this function did not have a
> noreturn attribute. Add it.
>
> While at it, add a printf attribute too to die() and constify the format
> parameter. This leads to some errors when compiling on x86_64:
>
>     arch/x86/tools/relocs.c:460:5: error: format specifies type 'int'
>     but the argument has type 'Elf64_Xword' (aka 'unsigned long')
>     [-Werror,-Wformat]
>                                     sec->shdr.sh_size);
>                                     ^~~~~~~~~~~~~~~~~
>     arch/x86/tools/relocs.c:464:5: error: format specifies type 'int'
>     but the argument has type 'Elf64_Off' (aka 'unsigned long')
>     [-Werror,-Wformat]
>                                     sec->shdr.sh_offset, strerror(errno));
>                                     ^~~~~~~~~~~~~~~~~~~
>
> When relocs.c is included by relocs_32.c, sec->shdr.sh_size and
> sec->shdr.sh_offset are 32-bit unsigned integers. When the file is
> included by relocs_64.c, these expressions are 64-bit unsigned integers.
>
> Introduce a PRIuELF macro to define the right format to use when
> printing these expressions.
>
> Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
> ---
>  arch/x86/tools/relocs.c        | 14 +++++++-------
>  arch/x86/tools/relocs.h        |  3 ++-
>  arch/x86/tools/relocs_32.c     |  3 +++
>  arch/x86/tools/relocs_64.c     |  3 +++
>  arch/x86/tools/relocs_common.c |  2 +-
>  5 files changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
> index 0c2fae8d929d..4cad603b8d58 100644
> --- a/arch/x86/tools/relocs.c
> +++ b/arch/x86/tools/relocs.c
> @@ -397,7 +397,7 @@ static void read_shdrs(FILE *fp)
>                     ehdr.e_shnum);
>         }
>         if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
> -               die("Seek to %d failed: %s\n",
> +               die("Seek to %"PRIuELF" failed: %s\n",
>                         ehdr.e_shoff, strerror(errno));
>         }
>         for (i = 0; i < ehdr.e_shnum; i++) {
> @@ -431,11 +431,11 @@ static void read_strtabs(FILE *fp)
>                 }
>                 sec->strtab = malloc(sec->shdr.sh_size);
>                 if (!sec->strtab) {
> -                       die("malloc of %d bytes for strtab failed\n",
> +                       die("malloc of %"PRIuELF" bytes for strtab failed\n",
>                                 sec->shdr.sh_size);
>                 }
>                 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
> -                       die("Seek to %d failed: %s\n",
> +                       die("Seek to %"PRIuELF" failed: %s\n",
>                                 sec->shdr.sh_offset, strerror(errno));
>                 }
>                 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
> @@ -456,11 +456,11 @@ static void read_symtabs(FILE *fp)
>                 }
>                 sec->symtab = malloc(sec->shdr.sh_size);
>                 if (!sec->symtab) {
> -                       die("malloc of %d bytes for symtab failed\n",
> +                       die("malloc of %"PRIuELF" bytes for symtab failed\n",
>                                 sec->shdr.sh_size);
>                 }
>                 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
> -                       die("Seek to %d failed: %s\n",
> +                       die("Seek to %"PRIuELF" failed: %s\n",
>                                 sec->shdr.sh_offset, strerror(errno));
>                 }
>                 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
> @@ -489,11 +489,11 @@ static void read_relocs(FILE *fp)
>                 }
>                 sec->reltab = malloc(sec->shdr.sh_size);
>                 if (!sec->reltab) {
> -                       die("malloc of %d bytes for relocs failed\n",
> +                       die("malloc of %"PRIuELF" bytes for relocs failed\n",
>                                 sec->shdr.sh_size);
>                 }
>                 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
> -                       die("Seek to %d failed: %s\n",
> +                       die("Seek to %"PRIuELF" failed: %s\n",
>                                 sec->shdr.sh_offset, strerror(errno));
>                 }
>                 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
> diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
> index f59590645b68..6b7969719833 100644
> --- a/arch/x86/tools/relocs.h
> +++ b/arch/x86/tools/relocs.h
> @@ -16,7 +16,8 @@
>  #include <regex.h>
>  #include <tools/le_byteshift.h>
>
> -void die(char *fmt, ...);
> +__attribute__((format(printf, 1, 2))) __attribute__((noreturn))
> +void die(const char *fmt, ...);
>
>  #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
>
> diff --git a/arch/x86/tools/relocs_32.c b/arch/x86/tools/relocs_32.c
> index b2ade2bb4162..8024ec473e6a 100644
> --- a/arch/x86/tools/relocs_32.c
> +++ b/arch/x86/tools/relocs_32.c
> @@ -14,4 +14,7 @@
>  #define ELF_ST_BIND(o)         ELF32_ST_BIND(o)
>  #define ELF_ST_VISIBILITY(o)   ELF32_ST_VISIBILITY(o)
>
> +/* printf format for Elf32_Off */
> +#define PRIuELF PRIu32
> +
>  #include "relocs.c"
> diff --git a/arch/x86/tools/relocs_64.c b/arch/x86/tools/relocs_64.c
> index 56b61b743c4c..2cf4de5c9d99 100644
> --- a/arch/x86/tools/relocs_64.c
> +++ b/arch/x86/tools/relocs_64.c
> @@ -14,4 +14,7 @@
>  #define ELF_ST_BIND(o)          ELF64_ST_BIND(o)
>  #define ELF_ST_VISIBILITY(o)    ELF64_ST_VISIBILITY(o)
>
> +/* printf format for Elf64_Off */
> +#define PRIuELF PRIu64
> +
>  #include "relocs.c"
> diff --git a/arch/x86/tools/relocs_common.c b/arch/x86/tools/relocs_common.c
> index acab636bcb34..30adb44eff79 100644
> --- a/arch/x86/tools/relocs_common.c
> +++ b/arch/x86/tools/relocs_common.c
> @@ -1,6 +1,6 @@
>  #include "relocs.h"
>
> -void die(char *fmt, ...)
> +void die(const char *fmt, ...)
>  {
>         va_list ap;
>         va_start(ap, fmt);
> --
> 2.9.3
>

I have to admit that this is the first time I am looking at these
format specifier macros from inttypes.h.  I looked at that file.  Your
usage seems correct to me and should achieve the desired outcome.

Reviewed-by: Nilay Vaish <nilayvaish@gmail.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/1] x86, relocs: add function attributes to die()
  2016-09-05 15:28       ` Nilay Vaish
@ 2016-10-23 16:47         ` Nicolas Iooss
  0 siblings, 0 replies; 6+ messages in thread
From: Nicolas Iooss @ 2016-10-23 16:47 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86
  Cc: Nilay Vaish, Linux Kernel list

Hello,

A few weeks ago I sent the patch below and got a review, but this patch
has not been applied yet in linux-next. Does it cause a problem I need
to fix?

Thanks,
Nicolas

On 05/09/16 17:28, Nilay Vaish wrote:
> On 4 September 2016 at 11:51, Nicolas Iooss <nicolas.iooss_linux@m4x.org> wrote:
>> When building the kernel with clang and some warning flags, the compiler
>> reports the following warning:
>>
>>     arch/x86/tools/relocs.c:979:6: warning: variable 'do_reloc' is used
>>     uninitialized whenever 'if' condition is false
>>     [-Wsometimes-uninitialized]
>>             if (!use_real_mode)
>>                 ^~~~~~~~~~~~~~
>>     arch/x86/tools/relocs.c:991:14: note: uninitialized use occurs here
>>             walk_relocs(do_reloc);
>>                         ^~~~~~~~
>>     arch/x86/tools/relocs.c:979:2: note: remove the 'if' if its
>>     condition is always true
>>             if (!use_real_mode)
>>             ^~~~~~~~~~~~~~~~~~~
>>     arch/x86/tools/relocs.c:976:24: note: initialize the variable
>>     'do_reloc' to silence this warning
>>                             const char *symname);
>>                                                 ^
>>                                                  = NULL
>>
>> This is obviously a false positive: whenever the 'if' condition is
>> false, the program calls die(). Nevertheless the compiler did not know
>> this call makes the program quit because this function did not have a
>> noreturn attribute. Add it.
>>
>> While at it, add a printf attribute too to die() and constify the format
>> parameter. This leads to some errors when compiling on x86_64:
>>
>>     arch/x86/tools/relocs.c:460:5: error: format specifies type 'int'
>>     but the argument has type 'Elf64_Xword' (aka 'unsigned long')
>>     [-Werror,-Wformat]
>>                                     sec->shdr.sh_size);
>>                                     ^~~~~~~~~~~~~~~~~
>>     arch/x86/tools/relocs.c:464:5: error: format specifies type 'int'
>>     but the argument has type 'Elf64_Off' (aka 'unsigned long')
>>     [-Werror,-Wformat]
>>                                     sec->shdr.sh_offset, strerror(errno));
>>                                     ^~~~~~~~~~~~~~~~~~~
>>
>> When relocs.c is included by relocs_32.c, sec->shdr.sh_size and
>> sec->shdr.sh_offset are 32-bit unsigned integers. When the file is
>> included by relocs_64.c, these expressions are 64-bit unsigned integers.
>>
>> Introduce a PRIuELF macro to define the right format to use when
>> printing these expressions.
>>
>> Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
>> ---
>>  arch/x86/tools/relocs.c        | 14 +++++++-------
>>  arch/x86/tools/relocs.h        |  3 ++-
>>  arch/x86/tools/relocs_32.c     |  3 +++
>>  arch/x86/tools/relocs_64.c     |  3 +++
>>  arch/x86/tools/relocs_common.c |  2 +-
>>  5 files changed, 16 insertions(+), 9 deletions(-)
>>
>> diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
>> index 0c2fae8d929d..4cad603b8d58 100644
>> --- a/arch/x86/tools/relocs.c
>> +++ b/arch/x86/tools/relocs.c
>> @@ -397,7 +397,7 @@ static void read_shdrs(FILE *fp)
>>                     ehdr.e_shnum);
>>         }
>>         if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
>> -               die("Seek to %d failed: %s\n",
>> +               die("Seek to %"PRIuELF" failed: %s\n",
>>                         ehdr.e_shoff, strerror(errno));
>>         }
>>         for (i = 0; i < ehdr.e_shnum; i++) {
>> @@ -431,11 +431,11 @@ static void read_strtabs(FILE *fp)
>>                 }
>>                 sec->strtab = malloc(sec->shdr.sh_size);
>>                 if (!sec->strtab) {
>> -                       die("malloc of %d bytes for strtab failed\n",
>> +                       die("malloc of %"PRIuELF" bytes for strtab failed\n",
>>                                 sec->shdr.sh_size);
>>                 }
>>                 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
>> -                       die("Seek to %d failed: %s\n",
>> +                       die("Seek to %"PRIuELF" failed: %s\n",
>>                                 sec->shdr.sh_offset, strerror(errno));
>>                 }
>>                 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
>> @@ -456,11 +456,11 @@ static void read_symtabs(FILE *fp)
>>                 }
>>                 sec->symtab = malloc(sec->shdr.sh_size);
>>                 if (!sec->symtab) {
>> -                       die("malloc of %d bytes for symtab failed\n",
>> +                       die("malloc of %"PRIuELF" bytes for symtab failed\n",
>>                                 sec->shdr.sh_size);
>>                 }
>>                 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
>> -                       die("Seek to %d failed: %s\n",
>> +                       die("Seek to %"PRIuELF" failed: %s\n",
>>                                 sec->shdr.sh_offset, strerror(errno));
>>                 }
>>                 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
>> @@ -489,11 +489,11 @@ static void read_relocs(FILE *fp)
>>                 }
>>                 sec->reltab = malloc(sec->shdr.sh_size);
>>                 if (!sec->reltab) {
>> -                       die("malloc of %d bytes for relocs failed\n",
>> +                       die("malloc of %"PRIuELF" bytes for relocs failed\n",
>>                                 sec->shdr.sh_size);
>>                 }
>>                 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
>> -                       die("Seek to %d failed: %s\n",
>> +                       die("Seek to %"PRIuELF" failed: %s\n",
>>                                 sec->shdr.sh_offset, strerror(errno));
>>                 }
>>                 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
>> diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
>> index f59590645b68..6b7969719833 100644
>> --- a/arch/x86/tools/relocs.h
>> +++ b/arch/x86/tools/relocs.h
>> @@ -16,7 +16,8 @@
>>  #include <regex.h>
>>  #include <tools/le_byteshift.h>
>>
>> -void die(char *fmt, ...);
>> +__attribute__((format(printf, 1, 2))) __attribute__((noreturn))
>> +void die(const char *fmt, ...);
>>
>>  #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
>>
>> diff --git a/arch/x86/tools/relocs_32.c b/arch/x86/tools/relocs_32.c
>> index b2ade2bb4162..8024ec473e6a 100644
>> --- a/arch/x86/tools/relocs_32.c
>> +++ b/arch/x86/tools/relocs_32.c
>> @@ -14,4 +14,7 @@
>>  #define ELF_ST_BIND(o)         ELF32_ST_BIND(o)
>>  #define ELF_ST_VISIBILITY(o)   ELF32_ST_VISIBILITY(o)
>>
>> +/* printf format for Elf32_Off */
>> +#define PRIuELF PRIu32
>> +
>>  #include "relocs.c"
>> diff --git a/arch/x86/tools/relocs_64.c b/arch/x86/tools/relocs_64.c
>> index 56b61b743c4c..2cf4de5c9d99 100644
>> --- a/arch/x86/tools/relocs_64.c
>> +++ b/arch/x86/tools/relocs_64.c
>> @@ -14,4 +14,7 @@
>>  #define ELF_ST_BIND(o)          ELF64_ST_BIND(o)
>>  #define ELF_ST_VISIBILITY(o)    ELF64_ST_VISIBILITY(o)
>>
>> +/* printf format for Elf64_Off */
>> +#define PRIuELF PRIu64
>> +
>>  #include "relocs.c"
>> diff --git a/arch/x86/tools/relocs_common.c b/arch/x86/tools/relocs_common.c
>> index acab636bcb34..30adb44eff79 100644
>> --- a/arch/x86/tools/relocs_common.c
>> +++ b/arch/x86/tools/relocs_common.c
>> @@ -1,6 +1,6 @@
>>  #include "relocs.h"
>>
>> -void die(char *fmt, ...)
>> +void die(const char *fmt, ...)
>>  {
>>         va_list ap;
>>         va_start(ap, fmt);
>> --
>> 2.9.3
>>
> 
> I have to admit that this is the first time I am looking at these
> format specifier macros from inttypes.h.  I looked at that file.  Your
> usage seems correct to me and should achieve the desired outcome.
> 
> Reviewed-by: Nilay Vaish <nilayvaish@gmail.com>
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-10-23 16:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-03 14:50 [PATCH 1/1] x86, relocs: add function attributes to die() Nicolas Iooss
2016-09-03 15:16 ` Nilay Vaish
2016-09-03 15:51   ` Nicolas Iooss
2016-09-04 16:51     ` [PATCH v2 " Nicolas Iooss
2016-09-05 15:28       ` Nilay Vaish
2016-10-23 16:47         ` Nicolas Iooss

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).