public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99843] New: Making a function a friend of a class will hide function constructor priority if has constructor(n) attribute
@ 2021-03-31  2:20 itirg1 at optusnet dot com.au
  2021-03-31  8:15 ` [Bug c++/99843] " rguenth at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: itirg1 at optusnet dot com.au @ 2021-03-31  2:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99843

            Bug ID: 99843
           Summary: Making a function a friend of a class will hide
                    function constructor priority if has constructor(n)
                    attribute
           Product: gcc
           Version: 9.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: itirg1 at optusnet dot com.au
  Target Milestone: ---

When declaring a function a friend of a class and that function uses the
__attribute__((constructor(priority))) attribute, the priority is lost. I have
only tested this on the arm-none-eabi-g++ on windows.

In the code below I made a quick demo of the problem, all of the initialisation
functions correctly have a section called .init_array.(5 digit priority number)
containing a pointer to the function except for the function that was made a
friend which is placed in .init_array which as a result the linker script will
place after the numbered ones.

F:\Test>arm-none-eabi-g++ --version
arm-none-eabi-g++ (GNU Arm Embedded Toolchain 9-2020-q3-update) 9.3.1 20200408
(release)
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

F:\Test>cat testinit.cpp
// Initialization order test

void Init101(void) __attribute__((constructor(101)));
void Init101(void)
{
    while(1);
}

static void Init102(void) __attribute__((constructor(102)));
static void Init102(void)
{
    while(1);
}

namespace Something
{

void Init103(void) __attribute__((constructor(103)));
void Init103(void)
{
    while(1);
}

static void Init104(void) __attribute__((constructor(104)));
static void Init104(void)
{
    while(1);
}

class SomeClass
{
    friend void Init105(void);

    private:
        int     foo;
    public:
        int     bar;
};

SomeClass * AClassPtr = nullptr;

void Init105(void) __attribute__((constructor(105)));
void Init105(void)
{
    if (AClassPtr == nullptr)
    {
        AClassPtr = new SomeClass;
    }
    AClassPtr->foo = 219;
}

void Init106(void) __attribute__((constructor(106)));
void Init106(void)
{
    while(1);
}

}

F:\Test>arm-none-eabi-g++ -mcpu=cortex-m0 -c -fno-exceptions testinit.cpp

F:\Test>arm-none-eabi-objdump -h -r -t testinit.o

testinit.o:     file format elf32-littlearm

Sections:
Idx Name              Size      VMA       LMA       File off  Algn
  0 .text             0000004c  00000000  00000000  00000034  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 .data             00000000  00000000  00000000  00000080  2**0
                      CONTENTS, ALLOC, LOAD, DATA
  2 .bss              00000004  00000000  00000000  00000080  2**2
                      ALLOC
  3 .init_array.00101 00000004  00000000  00000000  00000080  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  4 .init_array.00102 00000004  00000000  00000000  00000084  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  5 .init_array.00103 00000004  00000000  00000000  00000088  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  6 .init_array.00104 00000004  00000000  00000000  0000008c  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  7 .init_array       00000004  00000000  00000000  00000090  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  8 .init_array.00106 00000004  00000000  00000000  00000094  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  9 .comment          0000004d  00000000  00000000  00000098  2**0
                      CONTENTS, READONLY
 10 .ARM.attributes 0000002c  00000000  00000000  000000e5  2**0
                      CONTENTS, READONLY

SYMBOL TABLE:
00000000 l    df *ABS*                  00000000 testinit.cpp
00000000 l    d  .text                  00000000 .text
00000000 l    d  .data                  00000000 .data
00000000 l    d  .bss                   00000000 .bss
00000000 l    d  .init_array.00101      00000000 .init_array.00101
00000006 l     F .text                  00000006 _ZL7Init102v
00000000 l    d  .init_array.00102      00000000 .init_array.00102
00000000 l    d  .init_array.00103      00000000 .init_array.00103
00000012 l     F .text                  00000006 _ZN9SomethingL7Init104Ev
00000000 l    d  .init_array.00104      00000000 .init_array.00104
00000000 l    d  .init_array            00000000 .init_array
00000000 l    d  .init_array.00106      00000000 .init_array.00106
00000000 l    d  .comment               00000000 .comment
00000000 l    d  .ARM.attributes        00000000 .ARM.attributes
00000000 g     F .text                  00000006 _Z7Init101v
0000000c g     F .text                  00000006 _ZN9Something7Init103Ev
00000000 g     O .bss                   00000004 _ZN9Something9AClassPtrE
00000018 g     F .text                  0000002c _ZN9Something7Init105Ev
00000000         *UND*                  00000000 _Znwj
00000044 g     F .text                  00000006 _ZN9Something7Init106Ev

RELOCATION RECORDS FOR [.text]:
OFFSET   TYPE              VALUE
00000026 R_ARM_THM_CALL    _Znwj
00000040 R_ARM_ABS32       _ZN9Something9AClassPtrE


RELOCATION RECORDS FOR [.init_array.00101]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _Z7Init101v


RELOCATION RECORDS FOR [.init_array.00102]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZL7Init102v


RELOCATION RECORDS FOR [.init_array.00103]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZN9Something7Init103Ev


RELOCATION RECORDS FOR [.init_array.00104]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZN9SomethingL7Init104Ev


RELOCATION RECORDS FOR [.init_array]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZN9Something7Init105Ev


RELOCATION RECORDS FOR [.init_array.00106]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZN9Something7Init106Ev

F:\Test>

Adding the __attribute__((constructor(105))) into the friend statement as well
fixes the problem and the correct output is then generated however in the first
case I think at the very least a warning should have been generated or ideally
keeping the priority instead of quietly removing it.

Changing the line with the friend statement to

    friend void Init105(void) __attribute__((constructor(105)));

generated the correct sections

F:\Test>arm-none-eabi-objdump -h -r -t testinit.o

testinit.o:     file format elf32-littlearm

Sections:
Idx Name              Size      VMA       LMA       File off  Algn
  0 .text             0000004c  00000000  00000000  00000034  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 .data             00000000  00000000  00000000  00000080  2**0
                      CONTENTS, ALLOC, LOAD, DATA
  2 .bss              00000004  00000000  00000000  00000080  2**2
                      ALLOC
  3 .ARM.extab        00000000  00000000  00000000  00000080  2**0
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .ARM.exidx        00000030  00000000  00000000  00000080  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
  5 .init_array.00101 00000004  00000000  00000000  000000b0  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  6 .init_array.00102 00000004  00000000  00000000  000000b4  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  7 .init_array.00103 00000004  00000000  00000000  000000b8  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  8 .init_array.00104 00000004  00000000  00000000  000000bc  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
  9 .init_array.00105 00000004  00000000  00000000  000000c0  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
 10 .init_array.00106 00000004  00000000  00000000  000000c4  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, DATA
 11 .comment          0000004d  00000000  00000000  000000c8  2**0
                      CONTENTS, READONLY
 12 .ARM.attributes   0000002c  00000000  00000000  00000115  2**0
                      CONTENTS, READONLY

SYMBOL TABLE:
00000000 l    df *ABS*              00000000 testinit.cpp
00000000 l    d  .text              00000000 .text
00000000 l    d  .data              00000000 .data
00000000 l    d  .bss               00000000 .bss
00000000 l    d  .ARM.extab         00000000 .ARM.extab
00000000 l    d  .ARM.exidx         00000000 .ARM.exidx
00000000 l    d  .init_array.00101  00000000 .init_array.00101
00000006 l     F .text              00000006 _ZL7Init102v
00000000 l    d  .init_array.00102  00000000 .init_array.00102
00000000 l    d  .init_array.00103  00000000 .init_array.00103
00000012 l     F .text              00000006 _ZN9SomethingL7Init104Ev
00000000 l    d  .init_array.00104  00000000 .init_array.00104
00000000 l    d  .init_array.00105  00000000 .init_array.00105
00000000 l    d  .init_array.00106  00000000 .init_array.00106
00000000 l    d  .comment           00000000 .comment
00000000 l    d  .ARM.attributes    00000000 .ARM.attributes
00000000 g     F .text              00000006 _Z7Init101v
0000000c g     F .text              00000006 _ZN9Something7Init103Ev
00000018 g     F .text              0000002c _ZN9Something7Init105Ev
00000000         *UND*              00000000 _Znwj
00000000 g     O .bss               00000004 _ZN9Something9AClassPtrE
00000000         *UND*              00000000 __aeabi_unwind_cpp_pr0
00000044 g     F .text              00000006 _ZN9Something7Init106Ev


RELOCATION RECORDS FOR [.text]:
OFFSET   TYPE              VALUE
00000026 R_ARM_THM_CALL    _Znwj
00000040 R_ARM_ABS32       _ZN9Something9AClassPtrE


RELOCATION RECORDS FOR [.ARM.exidx]:
OFFSET   TYPE              VALUE
00000000 R_ARM_PREL31      .text
00000008 R_ARM_PREL31      .text
00000010 R_ARM_PREL31      .text
00000018 R_ARM_PREL31      .text
00000020 R_ARM_PREL31      .text
00000020 R_ARM_NONE        __aeabi_unwind_cpp_pr0
00000028 R_ARM_PREL31      .text


RELOCATION RECORDS FOR [.init_array.00101]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _Z7Init101v


RELOCATION RECORDS FOR [.init_array.00102]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZL7Init102v


RELOCATION RECORDS FOR [.init_array.00103]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZN9Something7Init103Ev


RELOCATION RECORDS FOR [.init_array.00104]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZN9SomethingL7Init104Ev


RELOCATION RECORDS FOR [.init_array.00105]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZN9Something7Init105Ev


RELOCATION RECORDS FOR [.init_array.00106]:
OFFSET   TYPE              VALUE
00000000 R_ARM_TARGET1     _ZN9Something7Init106Ev

F:\Test>

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

* [Bug c++/99843] Making a function a friend of a class will hide function constructor priority if has constructor(n) attribute
  2021-03-31  2:20 [Bug c++/99843] New: Making a function a friend of a class will hide function constructor priority if has constructor(n) attribute itirg1 at optusnet dot com.au
@ 2021-03-31  8:15 ` rguenth at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-03-31  8:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99843

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-03-31
      Known to fail|                            |11.0, 7.5.0
           Keywords|                            |wrong-code
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Probably an issue with merging the declarations

    friend void Init105(void);

and

void Init105(void) __attribute__((constructor(105)));

Smaller testcase:

//void Init105(void) __attribute__((constructor(105)));  -- fixes it
class Foo
{
   friend void Init105(void);
};
void Init105(void) __attribute__((constructor(105)));
void Init105(void)
{
}

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

end of thread, other threads:[~2021-03-31  8:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-31  2:20 [Bug c++/99843] New: Making a function a friend of a class will hide function constructor priority if has constructor(n) attribute itirg1 at optusnet dot com.au
2021-03-31  8:15 ` [Bug c++/99843] " rguenth at gcc dot gnu.org

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).