public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* ctor/dtor priorities ignored when function is pre-declared
@ 2021-10-28 11:25 Rasmus Villemoes
  2021-10-29  7:08 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Rasmus Villemoes @ 2021-10-28 11:25 UTC (permalink / raw)
  To: gcc, Olivier Hainque

Hi

I was wondering why the vx_crtbegin.o file had a .init_array section and
not a .init_array.00101, when the function is defined with
__attribute__((constructor (101))) (see libgcc/config/vxcrtstuff.c).

After a lot of digging, including making sure that EH_CTOR_ATTRIBUTE
actually gets defined as I expect and adding -frecord-gcc-switches to
the build to see if any of those made ctor priorities ignored, it turns
out that the problem is the declarations preceding the definitions. This
can easily be reproduced by

#define ac(prio) __attribute__((constructor(prio)))

unsigned x = 987;

static void f101(void);
void f102(void);

static void ac(101) f101(void) { x += 101; }
void ac(102) f102(void) { x *= 102; }

static void ac(103) f103(void) { x -= 103; }
void ac(104) f104(void) { x /= 104; }

This results in a .init_array section with two pointers, plus the
expected .init_array.00103 and .init_array.00104 with one each.

This is rather unexpected, especially since the attribute is not
entirely ignored, just the priority part.

Rasmus

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: ctor/dtor priorities ignored when function is pre-declared
  2021-10-28 11:25 ctor/dtor priorities ignored when function is pre-declared Rasmus Villemoes
@ 2021-10-29  7:08 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2021-10-29  7:08 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: gcc, Olivier Hainque

On Thu, Oct 28, 2021 at 1:26 PM Rasmus Villemoes <rv@rasmusvillemoes.dk> wrote:
>
> Hi
>
> I was wondering why the vx_crtbegin.o file had a .init_array section and
> not a .init_array.00101, when the function is defined with
> __attribute__((constructor (101))) (see libgcc/config/vxcrtstuff.c).
>
> After a lot of digging, including making sure that EH_CTOR_ATTRIBUTE
> actually gets defined as I expect and adding -frecord-gcc-switches to
> the build to see if any of those made ctor priorities ignored, it turns
> out that the problem is the declarations preceding the definitions. This
> can easily be reproduced by
>
> #define ac(prio) __attribute__((constructor(prio)))
>
> unsigned x = 987;
>
> static void f101(void);
> void f102(void);
>
> static void ac(101) f101(void) { x += 101; }
> void ac(102) f102(void) { x *= 102; }
>
> static void ac(103) f103(void) { x -= 103; }
> void ac(104) f104(void) { x /= 104; }
>
> This results in a .init_array section with two pointers, plus the
> expected .init_array.00103 and .init_array.00104 with one each.
>
> This is rather unexpected, especially since the attribute is not
> entirely ignored, just the priority part.

Probably a bug in decl merging, failing to copy DECL_HAS_INIT_PRIORITY_P
and altering the on-the-side init priority mapping
(decl_{init,fini}_priority_insert).
I suggest to file a bugreport or try to fix it yourself
(gcc/c/c-decl.c:merge_decls)

Richard.

>
> Rasmus

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-10-29  7:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-28 11:25 ctor/dtor priorities ignored when function is pre-declared Rasmus Villemoes
2021-10-29  7:08 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).