x86, relocs: add printf attribute to die()
PATCH v4 1/1 x86, relocs: add printf attribute to die()
Adding such an attribute helps to detect errors in the format string at build time. After doing this, the compiler complains about some issues:
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.
Add casts to unsigned long long, which length is always 64-bit, and use %llu to format sec->shdr.sh_size and sec->shdr.sh_offset in relocs.c.
While at it, constify the format attribute of die().
--- arch/x86/tools/relocs.c | 31 +++++++++++++++++-------------- arch/x86/tools/relocs.h | 3 ++- arch/x86/tools/relocs_common.c | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-)
arch/x86/tools/relocs.c +17 -14
@@ -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 %llu failed: %s\n" , + (unsigned long long )ehdr.e_shoff,strerror (errno)); }for (i =0 ; i < ehdr.e_shnum; i++) {struct section *sec = &secs[i];
@@ -431,12 +431,13 @@ 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 %llu bytes for strtab failed\n" , + (unsigned long 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 %llu failed: %s\n" , + (unsigned long long )sec->shdr.sh_offset, +strerror (errno)); }if (fread (sec->strtab,1 , sec->shdr.sh_size, fp) != sec->shdr.sh_size) {
@@ -456,12 +457,13 @@ 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 %llu bytes for symtab failed\n" , + (unsigned long 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 %llu failed: %s\n" , + (unsigned long long )sec->shdr.sh_offset, +strerror (errno)); }if (fread (sec->symtab,1 , sec->shdr.sh_size, fp) != sec->shdr.sh_size) {
@@ -489,12 +491,13 @@ 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 %llu bytes for relocs failed\n" , + (unsigned long 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 %llu failed: %s\n" , + (unsigned long long )sec->shdr.sh_offset, +strerror (errno)); }if (fread (sec->reltab,1 , sec->shdr.sh_size, fp) != sec->shdr.sh_size) {
arch/x86/tools/relocs.h +2 -1
@@ -16,7 +16,8 @@#include <regex.h> #include <tools/le_byteshift.h> -void die (char *fmt, ...) __attribute__((noreturn)); +void die (const char *fmt, ...) __attribute__((noreturn)) + __attribute__((format (printf,1 ,2 )));#define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0]))
arch/x86/tools/relocs_common.c +1 -1
@@ -1,6 +1,6 @@#include "relocs.h" -void die (char *fmt, ...) +void die (const char *fmt, ...) {va_list ap;va_start (ap, fmt);
Signature · 1 line
2.11.1
Quoted 35 lines · Nicolas Iooss
Adding such an attribute helps to detect errors in the format string at build time. After doing this, the compiler complains about some issues:
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.cis included byrelocs_32.c,sec->shdr.sh_sizeandsec->shdr.sh_offsetare 32-bit unsigned integers. When the file is included byrelocs_64.c, these expressions are 64-bit unsigned integers.Add casts to unsigned long long, which length is always 64-bit, and use %llu to format
sec->shdr.sh_sizeandsec->shdr.sh_offsetinrelocs.c.While at it, constify the format attribute of
die().Signed-off-by Nicolas Iooss <[email protected]>--- arch/x86/tools/relocs.c | 31 +++++++++++++++++-------------- arch/x86/tools/relocs.h | 3 ++- arch/x86/tools/relocs_common.c | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-)arch/x86/tools/relocs.c +0
-0
arch/x86/tools/relocs.c static void read_shdrs(FILE *fp) · line 401
Quoted 7 lines
arch/x86/tools/relocs.c +2
-2@@ -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 %llu failed: %s\n" , + (unsigned long long )ehdr.e_shoff,strerror (errno));
Isn't "(u64)" shorter to write?
Thanks,
Ingo
Quoted 33 lines · Ingo Molnar
Quoted 30 lines · Nicolas Iooss
Adding such an attribute helps to detect errors in the format string at build time. After doing this, the compiler complains about some issues:
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.cis included byrelocs_32.c,sec->shdr.sh_sizeandsec->shdr.sh_offsetare 32-bit unsigned integers. When the file is included byrelocs_64.c, these expressions are 64-bit unsigned integers.Add casts to unsigned long long, which length is always 64-bit, and use %llu to format
sec->shdr.sh_sizeandsec->shdr.sh_offsetinrelocs.c.While at it, constify the format attribute of
die().Signed-off-by Nicolas Iooss <[email protected]>--- arch/x86/tools/relocs.c | 31 +++++++++++++++++-------------- arch/x86/tools/relocs.h | 3 ++- arch/x86/tools/relocs_common.c | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-)
Quoted 14 lines
arch/x86/tools/relocs.c static void read_shdrs(FILE *fp) · line 401
Quoted 12 lines
arch/x86/tools/relocs.c +2
-2@@ -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 %llu failed: %s\n" , + (unsigned long long )ehdr.e_shoff,strerror (errno));Isn't "(u64)" shorter to write?
u64 does not seem to be defined in this file right now. Adding "#include <linux/types.h>" defines u64 and __u64 in the following way:
- "typedef uint64_t u64;" from tools/include/linux/types.h
- "typedef unsigned long long __u64;" from /usr/include/asm-generic/int-ll64.h
uint64_t is unsigned long on x86-64 and gcc complains when using %llu on such a type, so using a cast to u64 forces using PRIu64 too.
Nevertheless "(__u64)" is shorter than "(unsigned long long)" and seems to work fine in my quick tests because it is always unsigned long long (on both x86-32 and x86-64). Would you prefer to use this cast?
Thanks,
Nicolas
Quoted 54 lines · Nicolas Iooss
Quoted 33 lines · Ingo Molnar
Quoted 30 lines · Nicolas Iooss
Adding such an attribute helps to detect errors in the format string at build time. After doing this, the compiler complains about some issues:
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.cis included byrelocs_32.c,sec->shdr.sh_sizeandsec->shdr.sh_offsetare 32-bit unsigned integers. When the file is included byrelocs_64.c, these expressions are 64-bit unsigned integers.Add casts to unsigned long long, which length is always 64-bit, and use %llu to format
sec->shdr.sh_sizeandsec->shdr.sh_offsetinrelocs.c.While at it, constify the format attribute of
die().Signed-off-by Nicolas Iooss <[email protected]>--- arch/x86/tools/relocs.c | 31 +++++++++++++++++-------------- arch/x86/tools/relocs.h | 3 ++- arch/x86/tools/relocs_common.c | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-)Quoted 14 lines
arch/x86/tools/relocs.c static void read_shdrs(FILE *fp) · line 401
Quoted 12 lines
arch/x86/tools/relocs.c +2
-2@@ -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 %llu failed: %s\n" , + (unsigned long long )ehdr.e_shoff,strerror (errno));Isn't "(u64)" shorter to write?
u64 does not seem to be defined in this file right now. Adding "#include <
linux/types.h>" defines u64 and__u64in the following way:
- "typedefuint64_tu64;" fromtools/include/linux/types.h
- "typedef unsigned long long__u64;" from/usr/include/asm-generic/int-ll64.h
Quoted 7 lines
uint64_tis unsigned long on x86-64 and gcc complains when using %llu on such a type, so using a cast to u64 forces using PRIu64 too.Nevertheless "(
__u64)" is shorter than "(unsigned long long)" and seems to work fine in my quick tests because it is always unsigned long long (on both x86-32 and x86-64). Would you prefer to use this cast?
Hello,
After the question I had on the 4th revision of this patch, I have not received any reply or comment for three weeks. What should I do in order to get this merged?
Thanks,
Nicolas
Hello,
The patch I sent a few months ago still applies cleanly on top of 4.12-rc1. What should I do in order to get it merged in 4.13?
In case you no longer have my initial email, the patch has been archived in https://patchwork.kernel.org/patch/9604651/ .
Thanks,
Nicolas
Quoted 71 lines · Nicolas Iooss
Quoted 54 lines · Nicolas Iooss
Quoted 33 lines · Ingo Molnar
Quoted 30 lines · Nicolas Iooss
Adding such an attribute helps to detect errors in the format string at build time. After doing this, the compiler complains about some issues:
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.cis included byrelocs_32.c,sec->shdr.sh_sizeandsec->shdr.sh_offsetare 32-bit unsigned integers. When the file is included byrelocs_64.c, these expressions are 64-bit unsigned integers.Add casts to unsigned long long, which length is always 64-bit, and use %llu to format
sec->shdr.sh_sizeandsec->shdr.sh_offsetinrelocs.c.While at it, constify the format attribute of
die().Signed-off-by Nicolas Iooss <[email protected]>--- arch/x86/tools/relocs.c | 31 +++++++++++++++++-------------- arch/x86/tools/relocs.h | 3 ++- arch/x86/tools/relocs_common.c | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-)Quoted 14 lines
arch/x86/tools/relocs.c static void read_shdrs(FILE *fp) · line 401
Quoted 12 lines
arch/x86/tools/relocs.c +2
-2@@ -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 %llu failed: %s\n" , + (unsigned long long )ehdr.e_shoff,strerror (errno));Isn't "(u64)" shorter to write?
u64 does not seem to be defined in this file right now. Adding "#include <
linux/types.h>" defines u64 and__u64in the following way:
- "typedefuint64_tu64;" fromtools/include/linux/types.h
- "typedef unsigned long long__u64;" from/usr/include/asm-generic/int-ll64.hQuoted 7 lines
uint64_tis unsigned long on x86-64 and gcc complains when using %llu on such a type, so using a cast to u64 forces using PRIu64 too.Nevertheless "(
__u64)" is shorter than "(unsigned long long)" and seems to work fine in my quick tests because it is always unsigned long long (on both x86-32 and x86-64). Would you prefer to use this cast?Hello,
After the question I had on the 4th revision of this patch, I have not received any reply or comment for three weeks. What should I do in order to get this merged?Thanks,
Nicolas
Quoted 7 lines · Nicolas Iooss
Hello,
The patch I sent a few months ago still applies cleanly on top of 4.12-rc1. What should I do in order to get it merged in 4.13?
In case you no longer have my initial email, the patch has been archived in https://patchwork.kernel.org/patch/9604651/ .
Avoid top postings, please.
Looking into patch I see the following
1) the constify part is better to be a separate patch;
2) shouldn't be specifier '%ju' applied?
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
Isn't "(u64)" shorter to write?
u64 does not seem to be defined in this file right now. Adding "#include <
linux/types.h>" defines u64 and__u64in the following way:
- "typedefuint64_tu64;" fromtools/include/linux/types.h
- "typedef unsigned long long__u64;" from/usr/include/asm-generic/int-ll64.h
uint64_tis unsigned long on x86-64 and gcc complains when using %llu on such a type, so using a cast to u64 forces using PRIu64 too.
Signature · 2 lines
With Best Regards, Andy Shevchenko