cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
tcmichals
Visitor
Visitor
1,852 Views
Registered: ‎01-18-2018

Invoking C++ constuctors before main

Jump to solution

Using Vitis 2020.01, created a default C++ application based on FreeRTOS for microblaze.  Project was created, added my main, etc.  created tasks, etc,  compiled and loaded program executed without issue.

Then Added a simple class in main.cpp:

class example
{
protected:
int m_variable;
public:
int get(void);
example();
~example();

};
inline example::example(): m_variable(-1){}
inline example::~example(){}
inline int example::get(){ return m_variable;}

static example gExample; <-- created an instance

int main( void )
{
gExample.get();   <---  m_variable should be -1 because the constructor set the variable in the constructor

 

I set a break point in example::example   the function is not called, it is in the binary

63 inline example::example(): m_variable(-1){}
example:
000034f8: addik r1 , r1 , -8
000034fc: swi r19, r1 , 4
00003500: addk r19, r1 , r0
00003504: swi r5 , r19, 12
00003508: lwi r3 , r19, 12
0000350c: addik r4 , r0 , -1
00003510: swi r4 , r3 , 0
00003514: or r0 , r0 , r0
00003518: addk r1 , r19, r0
0000351c: lwi r19, r1 , 4
00003520: addik r1 , r1 , 8
00003524: rtsd r15, 8
00003528: or r0 , r0 , r0

 

__init should be calling C++ constructors?  It is not? What I'm doing wrong?

Also tried:

void __attribute__((constructor)) calledFirst();
void callFirst()
{
}

This did not called either.

 

 

 

 

Tags (2)
1 Solution

Accepted Solutions
tcmichals
Visitor
Visitor
1,046 Views
Registered: ‎01-18-2018

2020.2 Vitis fixes this issue.

View solution in original post

0 Kudos
11 Replies
sgeorgi_sen
Explorer
Explorer
1,723 Views
Registered: ‎04-07-2014

Hi tcmichals,

I have the same problem here: Vitis 2020.1, Windows 10, Microblaze toolchain.

In Vitis 2019.2 constructors were successfully called in __init using __do_global_ctors_aux call. This is missing in Vitis 2020.1. The provided gcc library (specifically crtend.o and crtbegin.o) seem to be broken: __do_global_dtors_aux is missing in __fini and __do_global_ctors_aux is missing in __init.

I replaced the corresponding crtend.o inside C:\Xilinx\Vitis\2020.1\gnu\microblaze\... with the version from Vitis 2019.2. Now constructors are called again.

To find out, which of the many crtend.o files you have to replace, call the linker with --verbose option. It will print the used files then. There seem to be many different versions for different hardware configurations (endiannes, multiplier,...)

@xilinx_support: this is a big problem with Vitis 2020.1. because all microblaze c++ projects are currently broken!

Please fix this.

Regards,
Sebastian

sgeorgi_sen
Explorer
Explorer
1,721 Views
Registered: ‎04-07-2014

Dear Xilinx Support,

and for your reference I hearby attach the disassemblies of the following minimal example:

#include <xil_printf.h>

class myClass {
public:
myClass(void) {
xil_printf("myClass Constructor\r\n");
while (1);
}
} myClassInst;

int main() {
xil_printf("Main\r\n");
while (1);
return 0;
}

 

Please note, the difference in __init function when using the current crtend.o and the crtend.o from Vitis 2019.2

Regards,

Sebastian

sgeorgi_sen
Explorer
Explorer
1,694 Views
Registered: ‎04-07-2014

Hi @tcmichals ,

what is also missing is the termination of the __CTOR_LIST__.

As temporary workaround you can add the line LONG(0xFFFFFFFF) after ___CTORS_LIST___ = .; to your lscript.ld

Perhaps it is easier to roll back to Vitis 2019.2.

Regards,
Sebastian

sgeorgi_sen
Explorer
Explorer
1,654 Views
Registered: ‎04-07-2014

Dear Xilinx Support,

I would definitely appreciate a response regarding this topic.

Have you been able to reproduce the issue? Is there a fix available?

Regards,

Sebastian

jvallie
Observer
Observer
1,593 Views
Registered: ‎06-05-2018

Hello,

We are also having the same issue with our standalone Microblaze C++ applications.

Our current workaround is to compile the BSP with Vitis 2020.1, copy the generated libraries, and then compile our application with Vitis 2019.2 toolchain.

We are waiting for a real solution from Xilinx.

0 Kudos
tcmichals
Visitor
Visitor
1,515 Views
Registered: ‎01-18-2018

I tried this and it does not work, looked at the LD scripts from 2019.2 and it matches 2020.1

 

.ctors : {
__CTOR_LIST__ = .;
___CTORS_LIST___ = .;
KEEP (*crtbegin.o(.ctors))
KEEP (*(EXCLUDE_FILE(*crtend.o) .ctors))
KEEP (*(SORT(.ctors.*)))
KEEP (*(.ctors))
__CTOR_END__ = .;
___CTORS_END___ = .;
LONG(0xFFFFFFFF);
} > microblaze_0_local_memory_ilmb_bram_if_cntlr_Mem_microblaze_0_local_memory_dlmb_bram_if_cntlr_Mem

0 Kudos
sgeorgi_sen
Explorer
Explorer
1,483 Views
Registered: ‎04-07-2014

hi @tcmichals ,

as I said before, it is perhaps the best way to roll back to Vitis 2019.2

If you want to stay at 2020.1 you need to temporarily modify your lscript until the toolchain has been fixed:

.ctors : {
__CTOR_LIST__ = .;
___CTORS_LIST___ = .;
LONG(0xFFFFFFFF)
KEEP (*crtbegin.o(.ctors))
KEEP (*(EXCLUDE_FILE(*crtend.o) .ctors))
KEEP (*(SORT(.ctors.*)))
KEEP (*(.ctors))
__CTOR_END__ = .;
___CTORS_END___ = .;
} > RAM

Regards,
Sebastian

0 Kudos
tcmichals
Visitor
Visitor
1,418 Views
Registered: ‎01-18-2018

little more portable, add this function to the file that contains main(int argc, char **argc)

static void callConstructors()
{
// Start and end points of the constructor list,
// defined by the linker script.
extern void (*___CTORS_LIST___)();
extern void (*__CTOR_END__)();

// Call each function in the list.
// We have to take the address of the symbols, as __init_array_start *is*
// the first function pointer, not the address of it.
for (void (**p)() = &___CTORS_LIST___; p < &__CTOR_END__; ++p) {
(*p)();
}
}

 

 

int main( void )
{
callConstructors();

sgeorgi_sen
Explorer
Explorer
1,301 Views
Registered: ‎04-07-2014

Hi @tcmichals ,

this is a good workaround indeed.

I tried Vitis Update 2020.1.1. The effect remains the same.

Thanks and regards,

Sebastian

0 Kudos
tcmichals
Visitor
Visitor
1,047 Views
Registered: ‎01-18-2018

2020.2 Vitis fixes this issue.

View solution in original post

0 Kudos
sgeorgi_sen
Explorer
Explorer
962 Views
Registered: ‎04-07-2014

Hi @tcmichals,

I was able to confirm, that Vitis 2020.2 fixes the issue.

Thanks and regards,

Sebastian

0 Kudos