Use CHAR_BIT instead of NBBY in libctf

Message ID 20190604182754.22061-1-tromey@adacore.com
State New
Headers show
Series
  • Use CHAR_BIT instead of NBBY in libctf
Related show

Commit Message

Tom Tromey June 4, 2019, 6:27 p.m.
On x86-64 Fedora 29, I tried to build a mingw-hosted gdb that targets
ppc-linux.  You can do this with:

    ../binutils-gdb/configure --host=i686-w64-mingw32 --target=ppc-linux \
        --disable-{binutils,gas,gold,gprof,ld}

The build failed with these errors in libctf:

In file included from ../../binutils-gdb/libctf/ctf-create.c:20:
../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_encoded':
../../binutils-gdb/libctf/ctf-create.c:803:59: error: 'NBBY' undeclared (first use in this function)
   dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
                                                           ^~~~
../../binutils-gdb/libctf/ctf-impl.h:254:42: note: in definition of macro 'P2ROUNDUP'
 #define P2ROUNDUP(x, align)  (-(-(x) & -(align)))
                                          ^~~~~
../../binutils-gdb/libctf/ctf-create.c:803:59: note: each undeclared identifier is reported only once for each function it appears in
   dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
                                                           ^~~~
../../binutils-gdb/libctf/ctf-impl.h:254:42: note: in definition of macro 'P2ROUNDUP'
 #define P2ROUNDUP(x, align)  (-(-(x) & -(align)))
                                          ^~~~~
../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_slice':
../../binutils-gdb/libctf/ctf-create.c:862:59: error: 'NBBY' undeclared (first use in this function)
   dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
                                                           ^~~~
../../binutils-gdb/libctf/ctf-impl.h:254:42: note: in definition of macro 'P2ROUNDUP'
 #define P2ROUNDUP(x, align)  (-(-(x) & -(align)))
                                          ^~~~~
../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_member_offset':
../../binutils-gdb/libctf/ctf-create.c:1341:21: error: 'NBBY' undeclared (first use in this function)
      off += lsize * NBBY;
                     ^~~~
../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_type':
../../binutils-gdb/libctf/ctf-create.c:1822:16: warning: unknown conversion type character 'z' in format [-Wformat=]
   ctf_dprintf ("Conflict for type %s against ID %lx: "
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../binutils-gdb/libctf/ctf-create.c:1823:35: note: format string is defined here
         "union size differs, old %zi, new %zi\n",
                                   ^
../../binutils-gdb/libctf/ctf-create.c:1822:16: warning: unknown conversion type character 'z' in format [-Wformat=]
   ctf_dprintf ("Conflict for type %s against ID %lx: "
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../binutils-gdb/libctf/ctf-create.c:1823:44: note: format string is defined here
         "union size differs, old %zi, new %zi\n",
                                            ^
../../binutils-gdb/libctf/ctf-create.c:1822:16: warning: too many arguments for format [-Wformat-extra-args]
   ctf_dprintf ("Conflict for type %s against ID %lx: "
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This patch fixes the actual errors in here.  I did not try to fix the
printf warnings, though I think someone ought to.

Ok?

libctf/ChangeLog
2019-06-04  Tom Tromey  <tromey@adacore.com>

	* ctf-create.c (ctf_add_encoded, ctf_add_slice)
	(ctf_add_member_offset): Use CHAR_BIT, not NBBY.
---
 libctf/ChangeLog    |  5 +++++
 libctf/ctf-create.c | 14 ++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)

-- 
2.20.1

Comments

Nick Alcock June 4, 2019, 7:19 p.m. | #1
On 4 Jun 2019, Tom Tromey uttered the following:

> On x86-64 Fedora 29, I tried to build a mingw-hosted gdb that targets

> ppc-linux.  You can do this with:

>

>     ../binutils-gdb/configure --host=i686-w64-mingw32 --target=ppc-linux \

>         --disable-{binutils,gas,gold,gprof,ld}

>

> The build failed with these errors in libctf:

>

> In file included from ../../binutils-gdb/libctf/ctf-create.c:20:

> ../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_encoded':

> ../../binutils-gdb/libctf/ctf-create.c:803:59: error: 'NBBY' undeclared (first use in this function)

>    dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);


Hm, something else I thought was portable that actually isn't.

> ../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_type':

> ../../binutils-gdb/libctf/ctf-create.c:1822:16: warning: unknown conversion type character 'z' in format [-Wformat=]

>    ctf_dprintf ("Conflict for type %s against ID %lx: "

>                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I suppose we could explicitly cast size_t arguments instead of using
%zi. I can implement that if you think it's sensible. (Ironically, it's
not that long ago that I ripped those casts out and moved to %zi
everywhere...)

> This patch fixes the actual errors in here.  I did not try to fix the

> printf warnings, though I think someone ought to.


I'll fix those.

> Ok?


Looks good to me (<limits.h> is pulled in via ctf-impl.h so CHAR_BIT is
always in scope).

Thanks for the patch!
Tom Tromey June 4, 2019, 8:10 p.m. | #2
>>>>> "Nick" == Nick Alcock <nick.alcock@oracle.com> writes:


>> ../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_type':

>> ../../binutils-gdb/libctf/ctf-create.c:1822:16: warning: unknown conversion type character 'z' in format [-Wformat=]

>> ctf_dprintf ("Conflict for type %s against ID %lx: "

>> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Nick> I suppose we could explicitly cast size_t arguments instead of using
Nick> %zi. I can implement that if you think it's sensible. (Ironically, it's
Nick> not that long ago that I ripped those casts out and moved to %zi
Nick> everywhere...)

FWIW I am not sure if this is really an ongoing problem or whether it's
gcc being conservative.  However, apparently at least some versions of
Windows don't support %z.

BFD defines various BFD_PRI* macros that you can use if you want to try
for the full approach.  I suppose this could be extended to cover "%z".

Casting is fine with me, though of course it can fail if there is
overflow, so some thought is required.

Tom
Nick Alcock June 5, 2019, 10:45 a.m. | #3
On 4 Jun 2019, Tom Tromey uttered the following:

>>>>>> "Nick" == Nick Alcock <nick.alcock@oracle.com> writes:

>

>>> ../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_type':

>>> ../../binutils-gdb/libctf/ctf-create.c:1822:16: warning: unknown conversion type character 'z' in format [-Wformat=]

>>> ctf_dprintf ("Conflict for type %s against ID %lx: "

>>> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

>

> Nick> I suppose we could explicitly cast size_t arguments instead of using

> Nick> %zi. I can implement that if you think it's sensible. (Ironically, it's

> Nick> not that long ago that I ripped those casts out and moved to %zi

> Nick> everywhere...)

>

> FWIW I am not sure if this is really an ongoing problem or whether it's

> gcc being conservative.  However, apparently at least some versions of

> Windows don't support %z.


Yeah, I found references elsewhere to other allegedly-C99 compilers not
supporting %z too. I'll switch back to just casting: these are all only
debugging messages anyway. (Because a well-behaved library shouldn't be
printing stuff unless you turn on a debugging option.)

> BFD defines various BFD_PRI* macros that you can use if you want to try

> for the full approach.  I suppose this could be extended to cover "%z".

>

> Casting is fine with me, though of course it can fail if there is

> overflow, so some thought is required.


These are all for things like type IDs, for which the file format
imposes a 2^32 maximum anyway. So we should always be safe, since GNU
software assumes long is at least that long.

-- 
NULL && (void)

Patch

diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c
index 3beed887122..356d51295f5 100644
--- a/libctf/ctf-create.c
+++ b/libctf/ctf-create.c
@@ -800,7 +800,8 @@  ctf_add_encoded (ctf_file_t *fp, uint32_t flag,
     return CTF_ERR;		/* errno is set for us.  */
 
   dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
-  dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
+  dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
+				 / CHAR_BIT);
   dtd->dtd_u.dtu_enc = *ep;
 
   return type;
@@ -859,7 +860,8 @@  ctf_add_slice (ctf_file_t *fp, uint32_t flag, ctf_id_t ref,
     return CTF_ERR;		/* errno is set for us.  */
 
   dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
-  dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
+  dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
+				 / CHAR_BIT);
   dtd->dtd_u.dtu_slice.cts_type = ref;
   dtd->dtd_u.dtu_slice.cts_bits = ep->cte_bits;
   dtd->dtd_u.dtu_slice.cts_offset = ep->cte_offset;
@@ -1338,7 +1340,7 @@  ctf_add_member_offset (ctf_file_t *fp, ctf_id_t souid, const char *name,
 	  if (ctf_type_encoding (fp, ltype, &linfo) == 0)
 	    off += linfo.cte_bits;
 	  else if ((lsize = ctf_type_size (fp, ltype)) > 0)
-	    off += lsize * NBBY;
+	    off += lsize * CHAR_BIT;
 
 	  /* Round up the offset of the end of the last member to
 	     the next byte boundary, convert 'off' to bytes, and
@@ -1349,9 +1351,9 @@  ctf_add_member_offset (ctf_file_t *fp, ctf_id_t souid, const char *name,
 	     packing if the new member is a bit-field, but we're
 	     the "compiler" and ANSI says we can do as we choose.  */
 
-	  off = roundup (off, NBBY) / NBBY;
+	  off = roundup (off, CHAR_BIT) / CHAR_BIT;
 	  off = roundup (off, MAX (malign, 1));
-	  dmd->dmd_offset = off * NBBY;
+	  dmd->dmd_offset = off * CHAR_BIT;
 	  ssize = off + msize;
 	}
       else
@@ -1360,7 +1362,7 @@  ctf_add_member_offset (ctf_file_t *fp, ctf_id_t souid, const char *name,
 
 	  dmd->dmd_offset = bit_offset;
 	  ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL);
-	  ssize = MAX (ssize, ((signed) bit_offset / NBBY) + msize);
+	  ssize = MAX (ssize, ((signed) bit_offset / CHAR_BIT) + msize);
 	}
     }
   else