[Bug c/96221] New: Constructor attribute priority is ignored if additional prototypes omit attribute

srk31 at srcf dot ucam.org gcc-bugzilla@gcc.gnu.org
Thu Jul 16 15:16:06 GMT 2020


            Bug ID: 96221
           Summary: Constructor attribute priority is ignored if
                    additional prototypes omit attribute
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: srk31 at srcf dot ucam.org
  Target Milestone: ---

Created attachment 48882
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48882&action=edit
Minimal test case

I was surprised to find that my __attribute__((constructor(101))) on a function
was not giving it priority over another constructor whose attribute used a
higher number. It turns out this was owing to an additional prototype that did
not mention the function as a constructor.

This caused a really subtle initialization bug in my code, and was tough to
track down. At the least, I would expect a warning when the information is

I haven't tried on 8.4, but didn't see any relevant changelog entries.

Note that this is not a duplicate of 87592, which is about when the priority is
specified multiple times.

$ cat test.c
// comment these for correct behaviour, i.e. 1 then 2
static void do_init1(void);
static void do_init2(void);

static void do_init2(void) __attribute__((constructor(102)));
static void do_init2(void)
        printf("Init 2!\n");

static void do_init1(void) __attribute__((constructor(101)));
static void do_init1(void)
        printf("Init 1!\n");

int main(void)
        return 0;

$ cc -save-temps    test.c   -o test
$ ./test
Init 2!
Init 1!
$ grep -B2 init_array test.s
        .size   do_init2, .-do_init2
        .section        .init_array,"aw"
        .size   do_init1, .-do_init1
        .section        .init_array

The attribute's priority argument is being discarded. It should be visible in
the section name (".init_array.0101" etc.). If the extra prototypes are
removed, the programmer-intended behaviour is restored.

More information about the Gcc-bugs mailing list