Bug 23884 - error: ‘__elf32_msize’ specifies less restrictive attribute than its target ‘elf32_fsize’: ‘const’ [-Werror=missing-attributes]
Summary: error: ‘__elf32_msize’ specifies less restrictive attribute than its target ‘...
Status: RESOLVED FIXED
Alias: None
Product: elfutils
Classification: Unclassified
Component: libelf (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-15 08:48 UTC by Martin Liska
Modified: 2018-11-16 08:31 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2018-11-15 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Liska 2018-11-15 08:48:25 UTC
It's a new warning that comes with recent GCC 9:

$ make V=1
make --no-print-directory all-recursive
Making all in config
make[2]: Nothing to be done for 'all'.
Making all in m4
make[2]: Nothing to be done for 'all'.
Making all in lib
make[2]: Nothing to be done for 'all'.
Making all in libelf
gcc -D_GNU_SOURCE -DHAVE_CONFIG_H -DLOCALEDIR='"/usr/local/share/locale"' -I. -I..  -I. -I. -I../lib -I..  -std=gnu99 -Wall -Wshadow -Wformat=2 -Wold-style-definition -Wstrict-prototypes -Wlogical-op -Wduplicated-cond -Wnull-dereference -Wimplicit-fallthrough=5 -Werror -Wunused -Wextra -Wstack-usage=262144    -D_FORTIFY_SOURCE=2 -g -O2 -MT elf32_fsize.o -MD -MP -MF .deps/elf32_fsize.Tpo -c -o elf32_fsize.o elf32_fsize.c
In file included from ../config.h:134,
                 from elf32_fsize.c:31:
libelfP.h:47:30: error: ‘__elf32_msize’ specifies less restrictive attribute than its target ‘elf32_fsize’: ‘const’ [-Werror=missing-attributes]
   47 | #define __elfw2_(Bits, Name) __elf##Bits##_##Name
      |                              ^~~~~
../lib/eu-config.h:60:26: note: in definition of macro ‘strong_alias’
   60 |   extern __typeof (name) aliasname __attribute__ ((alias (#name)));
      |                          ^~~~~~~~~
elf32_fsize.c:68:1: note: in expansion of macro ‘local_strong_alias’
   68 | local_strong_alias (elfw2(LIBELFBITS, fsize), __elfw2(LIBELFBITS, msize))
      | ^~~~~~~~~~~~~~~~~~
libelfP.h:52:29: note: in expansion of macro ‘__elfw2_’
   52 | #define __elfw2(Bits, Name) __elfw2_(Bits, Name)
      |                             ^~~~~~~~
elf32_fsize.c:68:47: note: in expansion of macro ‘__elfw2’
   68 | local_strong_alias (elfw2(LIBELFBITS, fsize), __elfw2(LIBELFBITS, msize))
      |                                               ^~~~~~~
In file included from elf32_fsize.c:35:
libelfP.h:48:28: note: ‘__elf32_msize’ target declared here
   48 | #define elfw2_(Bits, Name) elf##Bits##_##Name
      |                            ^~~
libelfP.h:53:27: note: in expansion of macro ‘elfw2_’
   53 | #define elfw2(Bits, Name) elfw2_(Bits, Name)
      |                           ^~~~~~
elf32_fsize.c:43:1: note: in expansion of macro ‘elfw2’
   43 | elfw2(LIBELFBITS, fsize) (Elf_Type type, size_t count, unsigned int version)
      | ^~~~~
cc1: all warnings being treated as errors
make[2]: *** [Makefile:786: elf32_fsize.o] Error 1
make[1]: *** [Makefile:485: all-recursive] Error 1
make: *** [Makefile:401: all] Error 2
Comment 1 Mark Wielaard 2018-11-15 16:03:51 UTC
This is weird. Where does the const come from?
Neither the declaration of fsize nor msize have a const.

It is as if gcc has determined that fsize could be const (maybe because it looks up the return value in a static table?) and then because we use __typeof__ to specify the type think the __typeof__ should include the const?
Comment 2 Mark Wielaard 2018-11-15 16:16:56 UTC
No, of course gcc is right.

fsize is declared in libelf.h as:

/* Return size of array of COUNT elements of the type denoted by TYPE
   in the external representation.  The binary class is taken from ELF.
   The result is based on version VERSION of the ELF standard.  */
extern size_t elf32_fsize (Elf_Type __type, size_t __count,
                           unsigned int __version)
       __const_attribute__;
/* Similar but this time the binary calls is ELFCLASS64.  */
extern size_t elf64_fsize (Elf_Type __type, size_t __count,
                           unsigned int __version)
       __const_attribute__;

But msize is declared in libelfP.h as:

/* The libelf API does not have such a function but it is still useful.
   Get the memory size for the given type.

   These functions cannot be marked internal since they are aliases
   of the export elfXX_fsize functions.*/
extern size_t __elf32_msize (Elf_Type __type, size_t __count,
                             unsigned int __version);
extern size_t __elf64_msize (Elf_Type __type, size_t __count,
                             unsigned int __version);

So the obvious fix is:

diff --git a/libelf/libelfP.h b/libelf/libelfP.h
index fa6d55d..9f3e8e9 100644
--- a/libelf/libelfP.h
+++ b/libelf/libelfP.h
@@ -461,9 +461,9 @@ extern Elf_Type __libelf_data_type (Elf *elf, int sh_type, G
    These functions cannot be marked internal since they are aliases
    of the export elfXX_fsize functions.*/
 extern size_t __elf32_msize (Elf_Type __type, size_t __count,
-                            unsigned int __version);
+                            unsigned int __version) __const_attribute__;
 extern size_t __elf64_msize (Elf_Type __type, size_t __count,
-                            unsigned int __version);
+                            unsigned int __version) __const_attribute__;
 
 
 /* Create Elf descriptor from memory image.  */
Comment 3 Mark Wielaard 2018-11-16 08:31:45 UTC
commit a01938d584b91e747167bb4b3f30ec300c4d6e43
Author: Mark Wielaard <mark@klomp.org>
Date:   Fri Nov 16 09:27:00 2018 +0100

    libelf: Mark both fsize and msize with const attribute.
    
    GCC9 -Wmissing-attributes pointed out that although we alias the fsize
    and msize functions only fsize was marked as const. Fix by also marking
    the msize definition as const.
    
    https://sourceware.org/bugzilla/show_bug.cgi?id=23884
    
    Signed-off-by: Mark Wielaard <mark@klomp.org>