Changes between Version 7 and Version 8 of TBR/UserManual/Using_C_Plus_Plus


Ignore:
Timestamp:
06/26/07 18:38:39 (15 years ago)
Author:
Rsg
Comment:

/* Global Object Construction and Destruction */

Legend:

Unmodified
Added
Removed
Modified
  • TBR/UserManual/Using_C_Plus_Plus

    v7 v8  
    4444Global object construction requires the compiler and RTEMS work together. This is a constant source of maintenance issues as changes in one can effect the other. The compiler generates special code to construct and destruct each global object. The constructor and destructor pointer's to this code is placed in a special section, usually called .ctor and .dtor. The linker will pull all the pointers into a single section giving you a table of pointers to constructors and destructors. The order or placement of a specific pointer in these tables cannot be easily set and any application that relies on a specific order should be considered broken and needs to be fixed.
    4545
    46 RTEMS calls the constructors after the kernel is running, but before first '''Init''' task runs. This means global objects can allocation memory from the heap, create threads and semaphores.  This runs in the context of the Init task before its entry point is invoked.  See _Thread_Handler() for details.
     46RTEMS calls the constructors after the kernel is running, but before first '''Init''' task runs. This means global objects can allocate memory from the heap, and create objects such as threads and semaphores.  These constructors run in the context of the Init task before its entry point is invoked.  See _Thread_Handler() for details.
    4747
    4848Different processors can require different initialisation sequences. An example is the PowerPC and its support for the SYSV/EABI compliant environment.
     
    5252BSP should call (gcc provided) '''_eabi()''' very early to set up a SYSV/EABI compliant environment (load r2/r13, stack align etc.). If you don't do this, e.g., SYSV/EABI short data areas won't work (see gcc -msdata -meabi options).
    5353
    54 RTEMS calls '''init()''' which among other things works through the C++ static constructor list -- provided that your linker stript and '''bsp_specs''' files are correct. See [wiki:#InitMagic C++ .init Linker Magic] on how it works.
     54RTEMS calls '''init()''' which among other things works through the C++ static constructor list -- provided that your linker script and '''bsp_specs''' files are correct. See [wiki:#InitMagic C++ .init Linker Magic] on how it works.
    5555
    5656 * However, there is a problem here, in that '''eabi()''' ends up calling '''init()''' (actually, '''__init()''', we renamed to fix the problem described here) when it is still too early to initialize the C++ environment.
     
    5858  '''Note''' both details are of the 'seems to work' type. You won't notice anything if you don't call '''eabi()''' until you try to use an essential SYSV/EABI feature. Likewise, calling '''init()''' too early might not cause problems in many cases until one of your constructors uses an yet unavailable feature provided by RTEMS such as '''malloc()'''.
    5959
    60 For the PowerPC a typical chicken + egg. problem exists -
     60For the PowerPC a typical chicken and egg problem exists -
    6161
    6262##we want to call '''_eabi()''' early (libbsp/powerpc/shared/start/start.S)