[Mspgcc-users] Calling constructors (C++) manually with mspgcc4
Dear list, I'm currently trying to port a project (using C++) from mspgcc (3.2.3) to mspgcc4. It is a generic C++ library, running (among other systems) on top of Contiki (which is a C-based firmware/OS). As far as I know, constructors of global C++ objects are not called automatically, so that the user must call them manually. So far I did this successfully via typedef void(*ctor_t)(); extern ctor_t __ctors_start; extern ctor_t __ctors_end; ... ctor_t ctor = __ctors_start; while( ctor __ctors_end ) { ctor(); ctor = (ctor_t)((void*)ctor + sizeof(void*)); } However, compiling the project with mspgcc4, it doesn't work anymore. Printing addresses of ctors_start/end via printf( Call ctors from %04x to %04x\n, (unsigned int)*__ctors_start, (unsigned int)*__ctors_end ); produces a higher number for ctors start address than for the end (looking in the *.map-file, everything looks fine). Do I understand sth wrong (so, is the above code incorrect, and it worked on the 3.2.3 just by luck)? Do you have an idea, how I should call the constructors? Or is the information above incomplete - then please let me know! Best, Tobias -- Nokia and ATT present the 2010 Calling All Innovators-North America contest Create new apps games for the Nokia N8 for consumers in U.S. and Canada $10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store http://p.sf.net/sfu/nokia-dev2dev ___ Mspgcc-users mailing list Mspgcc-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Re: [Mspgcc-users] Calling constructors (C++) manually with mspgcc4
Constructors for global C++ objects should be called automatically, as long as you're not doing something in the link phase that disrupts things. My testing with the program below shows that it seems to work. Note that if you fall off the end of main(), the global dtors are called. 0x1c00#global ctor 0x2bfe#local ctor In main 0x2bfe#local dtor 0x1c00#global dtor Peter #include stdio.h class CtorDtor { public: CtorDtor (const char* id) : id_(id) { printf(%p#%s ctor\r\n, this, id_); } ~CtorDtor () { printf(%p#%s dtor\r\n, this, id_); } private: const char* id_; }; CtorDtor global(global); int main () { CtorDtor local(local); printf(In main\r\n); return 0; } On Wed, Oct 20, 2010 at 9:48 AM, Tobias Baumgartner tb...@ibr.cs.tu-bs.dewrote: Dear list, I'm currently trying to port a project (using C++) from mspgcc (3.2.3) to mspgcc4. It is a generic C++ library, running (among other systems) on top of Contiki (which is a C-based firmware/OS). As far as I know, constructors of global C++ objects are not called automatically, so that the user must call them manually. So far I did this successfully via typedef void(*ctor_t)(); extern ctor_t __ctors_start; extern ctor_t __ctors_end; ... ctor_t ctor = __ctors_start; while( ctor __ctors_end ) { ctor(); ctor = (ctor_t)((void*)ctor + sizeof(void*)); } However, compiling the project with mspgcc4, it doesn't work anymore. Printing addresses of ctors_start/end via printf( Call ctors from %04x to %04x\n, (unsigned int)*__ctors_start, (unsigned int)*__ctors_end ); produces a higher number for ctors start address than for the end (looking in the *.map-file, everything looks fine). Do I understand sth wrong (so, is the above code incorrect, and it worked on the 3.2.3 just by luck)? Do you have an idea, how I should call the constructors? Or is the information above incomplete - then please let me know! Best, Tobias -- Nokia and ATT present the 2010 Calling All Innovators-North America contest Create new apps games for the Nokia N8 for consumers in U.S. and Canada $10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store http://p.sf.net/sfu/nokia-dev2dev ___ Mspgcc-users mailing list Mspgcc-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mspgcc-users -- Nokia and ATT present the 2010 Calling All Innovators-North America contest Create new apps games for the Nokia N8 for consumers in U.S. and Canada $10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store http://p.sf.net/sfu/nokia-dev2dev___ Mspgcc-users mailing list Mspgcc-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Re: [Mspgcc-users] Calling constructors (C++) manually with mspgcc4
Hmmm, what is the definition of __ctors_start? I doubt it is an extern variable pointer to void. It is a constant value. Or more precisely it is no lvalue. There is no memory location where its value is stored. It just has a value, is a linker generated 'constant' reference. But by your extern definition, the compiler doesn't know this. It assumes that it is a normal pointer variable. So later, when you use __ctors_start, the compiler uses an additional inference, as the value of a variable is the value stored at the location it references, not the value it actually HAS. In other words (example): if __ctors_start is 0x5c00, the line ctor_t ctor = __ctors_start; results in mov 0x5c00, ctor; moving the value at the begin of Flash into your ctor variable as new pointer, while what you want is a mov #0x5c00, ctor; moving the VALUE of __ctors_start as new pointer into ctor. So use __ctors_start when you want the address __ctors_start points to (the value of __ctors_start). JMGross - Ursprüngliche Nachricht - Von: Tobias Baumgartner Gesendet am: 20 Okt 2010 16:48:55 I'm currently trying to port a project (using C++) from mspgcc (3.2.3) to mspgcc4. It is a generic C++ library, running (among other systems) on top of Contiki (which is a C-based firmware/OS). As far as I know, constructors of global C++ objects are not called automatically, so that the user must call them manually. So far I did this successfully via typedef void(*ctor_t)(); extern ctor_t __ctors_start; extern ctor_t __ctors_end; ... ctor_t ctor = __ctors_start; while( ctor __ctors_end ) { ctor(); ctor = (ctor_t)((void*)ctor + sizeof(void*)); } However, compiling the project with mspgcc4, it doesn't work anymore. Printing addresses of ctors_start/end via printf( Call ctors from %04x to %04x\n, (unsigned int)*__ctors_start, (unsigned int)*__ctors_end ); produces a higher number for ctors start address than for the end (looking in the *.map-file, everything looks fine). Do I understand sth wrong (so, is the above code incorrect, and it worked on the 3.2.3 just by luck)? Do you have an idea, how I should call the constructors? Or is the information above incomplete - then please let me know! Best, Tobias -- Nokia and ATT present the 2010 Calling All Innovators-North America contest Create new apps games for the Nokia N8 for consumers in U.S. and Canada $10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store http://p.sf.net/sfu/nokia-dev2dev ___ Mspgcc-users mailing list Mspgcc-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mspgcc-users