Bug 37667 - Initialization of global variables and functions with constructor attribute.
Summary: Initialization of global variables and functions with constructor attribute.
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-09-28 00:43 UTC by Michael Uleysky
Modified: 2022-01-01 02:29 UTC (History)
4 users (show)

See Also:
Host: x86_64-unknown-linux
Target: x86_64-unknown-linux
Build: x86_64-unknown-linux
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Uleysky 2008-09-28 00:43:20 UTC
The test code

#include <stdlib.h>
class Test
{
 public:
 int t;
 Test():t(1) {}
};

Test ex_t;

void Init() __attribute__ ((constructor));
void Init() { if(ex_t.t==0) abort(); }

int main(void) { return 0; }

aborted if compiled with gcc 4.3.2. If compiled with gcc 4.1.0 or 3.3.6 it proceed normally. As I understand, in first case the ex_t constructor called after function Init, but in second case the ex_t constructor called before function Init. I not found in documentation what case is right, so I think this is a bug. In my opinion, constructors of global variables must have more priority than __attribute__ ((constructor)) functions.

Compilation command is simple: g++ -o test test.cpp
Comment 1 Andrew Pinski 2008-09-28 18:57:09 UTC
This works on the trunk at -O0 but fails at -O2.

I think this is undefined as the priority are the same.

You should use init_priority and set a priority for the constructor.
Comment 2 Michael Uleysky 2008-09-29 02:07:28 UTC
The priority must not be same. The only method to interact constructor function with main is global variables. It is no reason to initialize global variables after constructor function. Anyway,

Test ex_t __attribute__ ((init_priority(102)));
void Init() __attribute__ ((constructor(101)));

have no effect.
Comment 3 Andrew Pinski 2021-12-25 01:33:07 UTC
(In reply to Michael Uleysky from comment #2)
> The priority must not be same. The only method to interact constructor
> function with main is global variables. It is no reason to initialize global
> variables after constructor function. Anyway,
> 
> Test ex_t __attribute__ ((init_priority(102)));
> void Init() __attribute__ ((constructor(101)));

Because you have the wrong order, lower number is a higher priority which means 101/Init will always be first.

From https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Common-Function-Attributes.html#index-constructor-function-attribute:

"However, at present, the order in which constructors for C++ objects with static storage duration and functions decorated with attribute constructor are invoked is unspecified. In mixed declarations, attribute init_priority can be used to impose a specific ordering."

"A constructor with a smaller priority number runs before a constructor with a larger priority"