Hi,

I want to remap start-up configuration.

So, I'm thinking of breaking down in parts some start-up procedures and adding c++ constructors/destructors. In order to do that I think I will add some sections like '.init0' ... '.init3' and '.fini0' ... '.fini3' put appropriate code there.

I help do some of the work getting c++ going on the AVR with gcc. This is the way the AVR handle from what I remember, but I think we had another section to for a user hook at pre startup routine if required.

The changes I remember are something like,

adding to gcc/config/avr.c

/* Define the pseudo-ops used to switch to the .ctors and .dtors sections.
   There are no shared libraries on this target, and these sections are
   placed in the read-only program memory, so they are not writable.  */

#undef CTORS_SECTION_ASM_OP
#define CTORS_SECTION_ASM_OP "\t.section .ctors,\"a\",@progbits"

#undef DTORS_SECTION_ASM_OP
#define DTORS_SECTION_ASM_OP "\t.section .dtors,\"a\",@progbits"

#define TARGET_ASM_CONSTRUCTOR avr_asm_out_ctor
/* If defined, a function that outputs assembler code to arrange to
   call the function referenced by SYMBOL at initialization time.  */

#define TARGET_ASM_DESTRUCTOR avr_asm_out_dtor
/* This is like `TARGET_ASM_CONSTRUCTOR' but used for termination
   functions rather than initialization functions.  */

and adding to gcc/config/avr.c

static void   avr_asm_out_ctor PARAMS ((rtx, int));
static void   avr_asm_out_dtor PARAMS ((rtx, int));

and

static void
avr_asm_out_ctor (symbol, priority)
     rtx symbol;
     int priority;
{
  fputs ("\t.global __do_global_ctors\n", asm_out_file);
  default_ctor_section_asm_out_constructor (symbol, priority);
}

static void
avr_asm_out_dtor (symbol, priority)
     rtx symbol;
     int priority;
{
  fputs ("\t.global __do_global_dtors\n", asm_out_file);
  default_dtor_section_asm_out_destructor (symbol, priority);
}


the line .global __do_global_dtors forces the linker to link in the dtors code when compiling c++ code.

Then you need to add the ctors and dtors functions to libgcc.S

The AVR port ended up with 9 .init and .fini sections a few for spares in case someone wanted to add code between the sections. There was no code penalty is there is no code in the section its not linked.

Here is the extract from the avr linker script in binutils in binutils 2.13.2 ld/scripttempl/avr.sc

  /* Internal text space or external memory */
  .text :
  {
    *(.vectors)

    ${CONSTRUCTING+ __ctors_start = . ; }
    ${CONSTRUCTING+ *(.ctors) }
    ${CONSTRUCTING+ __ctors_end = . ; }
    ${CONSTRUCTING+ __dtors_start = . ; }
    ${CONSTRUCTING+ *(.dtors) }
    ${CONSTRUCTING+ __dtors_end = . ; }

    *(.progmem.gcc*)
    *(.progmem*)
    ${RELOCATING+. = ALIGN(2);}
    *(.init0)  /* Start here after reset.  */
    *(.init1)
    *(.init2)  /* Clear __zero_reg__, set up stack pointer.  */
    *(.init3)
    *(.init4)  /* Initialize data and BSS.  */
    *(.init5)
    *(.init6)  /* C++ constructors.  */
    *(.init7)
    *(.init8)
    *(.init9)  /* Call main().  */
    *(.text)
    ${RELOCATING+. = ALIGN(2);}
    *(.text.*)
    ${RELOCATING+. = ALIGN(2);}
    *(.fini9)  /* _exit() starts here.  */
    *(.fini8)
    *(.fini7)
    *(.fini6)  /* C++ destructors.  */
    *(.fini5)
    *(.fini4)
    *(.fini3)
    *(.fini2)
    *(.fini1)
    *(.fini0)  /* Infinite loop after program termination.  */
    ${RELOCATING+ _etext = . ; }
  } ${RELOCATING+ > text}

Then avr-libc was changed to put the bits of code into the correct sections, have a look at

http://savannah.nongnu.org/cgi-bin/viewcvs/avr-libc/avr-libc/crt1/gcrt1.S?rev=1.3&content-type=text/vnd.viewcvs-markup

All these section were put into one file to make them easy to find, although you have some C code to do the bss zero and data copy.

although this code is not GPL so maybe and the msp430 startup code is different, so maybe to and to reimplement the same strategy. Although Marek may allow you to copy it and put it under GPL.


In details:
- .init0 will contain copy data section loop
- .init1 will contain clear bss loop
- .init2 will contain c++ constructors code and linked as appropriate
- .init3 jump to main only:     'br     #main'

- .fini0 will contain _nothing_ .. just a label __stop_ProcExec_
- .fini1 will contail c++ destructors linked as necessary
- .fini2 will contain an old __stop_ProcExec_ and new label to loop at like
.Llabel:
        mov     r15, r2
        jmp     .Llabel

- .fini3 will contain ... something.

Also, two sections have to be introduced - .ctors and .dtors

The overal elf sections map will look as:
.text:
        .init0
        .init1
        .init2
        .init3

        ... user code   as in .text.*

        .fini0
        .fini1
        .fini2
        .fini3

        .ctors
        .dtors

        .data

        .noinit's data (still in doubt if .noinit may contain initialized data)
        
.vectors:

.bss:
        .bss
        .noinit
        .all the user stuff


Any suggestions? Comments? Additions? etc.?

I hope to implement it in gcc-3.3 (hopefully) and above and current binutils.

We found the code generated by g++ constructors, destructors was quite large, I think you end up with three constructor/destructor functions for every one, I don't know if they have fixed this in gcc yet.

Compiling your code with -fno-rtti -fno-enforce-eh-specs -fno-exceptions the define was added to gcc gcc/config/avr/avr.h

/* A C string constant that tells the GNU CC driver program options to
   pass to `cc1'.  It can also specify how to translate options you
   give to GNU CC into options for GNU CC to pass to the `cc1'.

   Do not define this macro if it does not need to do anything.  */

#define CC1PLUS_SPEC "%{!frtti:-fno-rtti} \
    %{!fenforce-eh-specs:-fno-enforce-eh-specs} \
    %{!fexceptions:-fno-exceptions}"

Do you have this under control or want me do do some of the above work and send you the patches?

Regards,

--
Peter Jansen
STS
Australian Antarctic Division
Channel Highway
Kingston
TAS        7050
AUSTRALIA
Ph  (03) 62 323 533
Fax (03) 62 323 351


___________________________________________________________________________

   Australian Antarctic Division - Commonwealth of Australia
IMPORTANT: This transmission is intended for the addressee only. If you are
not the intended recipient, you are notified that use or dissemination of
this communication is strictly prohibited by Commonwealth law. If you have
received this transmission in error, please notify the sender immediately
by e-mail or by telephoning +61 3 6232 3209 and DELETE the message.
       Visit our web site at http://www.aad.gov.au/
___________________________________________________________________________

Reply via email to