Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

From: H. Peter Anvin
Date: Fri Jun 25 2021 - 12:19:58 EST


This is a user space build time tool.

You can use PRIu32/64 or cast to unsigned long long; it's not like the performance for this case is going to matter one iota.

On June 25, 2021 7:12:36 AM PDT, Borislav Petkov <bp@xxxxxxxxx> wrote:
>On Fri, Jun 25, 2021 at 02:06:59PM +0200, Greg Kroah-Hartman wrote:
>> Casting seems to be the only way to make this "quiet" that I can
>tell.
>>
>> Unless someone else has a good idea?
>
>Hmm, so in Documentation/core-api/printk-formats.rst we say that for
>printk() with different size types, we should "use a format specifier
>of
>its largest possible type and explicitly cast to it."
>
>And that kinda sounds ok to me because we don't potentially lose
>through
>the casting.
>
>IOW, I guess something like this below.
>
>---
>diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
>index 04c5a44b9682..42b0f425a2c7 100644
>--- a/arch/x86/tools/relocs.c
>+++ b/arch/x86/tools/relocs.c
>@@ -389,7 +389,7 @@ static void read_ehdr(FILE *fp)
> Elf_Shdr shdr;
>
> 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));
>
> if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
> die("Cannot read initial ELF section header: %s\n",
>strerror(errno));
>@@ -412,17 +412,17 @@ static void read_shdrs(FILE *fp)
>
> secs = calloc(shnum, sizeof(struct section));
> if (!secs) {
>- die("Unable to allocate %d section headers\n",
>+ die("Unable to allocate %ld section headers\n",
> 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 < shnum; i++) {
> struct section *sec = &secs[i];
> if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
>- die("Cannot read ELF section headers %d/%d: %s\n",
>+ die("Cannot read ELF section headers %d/%ld: %s\n",
> i, shnum, strerror(errno));
> sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
> sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
>@@ -450,12 +450,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 %lu failed: %s\n",
>+ (unsigned long)sec->shdr.sh_offset, strerror(errno));
> }
> if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
> != sec->shdr.sh_size) {
>@@ -475,12 +475,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 %lu failed: %s\n",
>+ (unsigned long)sec->shdr.sh_offset, strerror(errno));
> }
> if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
> != sec->shdr.sh_size) {
>@@ -508,12 +508,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 %lu failed: %s\n",
>+ (unsigned 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 43c83c0fd22c..4c49c82446eb 100644
>--- a/arch/x86/tools/relocs.h
>+++ b/arch/x86/tools/relocs.h
>@@ -17,6 +17,7 @@
> #include <regex.h>
> #include <tools/le_byteshift.h>
>
>+__attribute__((__format__(printf, 1, 2)))
> void die(char *fmt, ...) __attribute__((noreturn));
>
> #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.