This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Advise needed: gcc initialization (crt0?)


Hi,

I asked the same question last week. Here is a linker script and two very simple start up files that work. crt0.s crt1.c and
link.cmd. This calls a hello world in main.c and runs properly on a Hitachi SH 7032 evaluation board. Global variables and
static variables all work properly. The key is the AT statement in the .data part of the linker script.

However, I have not been able to get a C++ program to compile properly.  I get the error message that
___get_dynamic_handler_chain is not found. If anyone has some advice on this one I would appreciate it.

Ron


!*****************
! crt0.s
!*****************

 ! Set top of stack
 .set _stack, 0xf002000

 .global _end
 .global _stack


 .section .vect
_vector_table:
 .long  _start  ! Big reset
 .long  _stack  !
 .long  _start  ! Not so big reset
 .long  _stack  !


 .section .text
 .global _start
 .global _exit

 .align 2

_start:
 ! Initialize Stack
 mov.l STACK_k,r15
    mov.l r14,@-r15
    mov r15,r14
 nop

 ! Initialize RAM
 mov.l INITRAM_k,r1
 jsr @r1
 nop

 ! call main
 mov.l MAIN_k,r1
 jsr @r1
 nop


_end:
 ! _end calls _start
 mov.l START_k,r0
 jmp @r0
 nop


 .align 4

STACK_k:
   .long _stack
START_k:
   .long _start
MAIN_k:
   .long _main
INITRAM_k:
   .long _initRAM



////////////////////////////////////////////////////////////
// crt1.c

////////////////////////////////////////////////////////////////////
// global errno
#include <errno.h>
#undef errno
extern int errno;

#include <sys/types.h>

extern char btext, etext, bdata, edata, bstart, bend, romdata;
////////////////////////////////////////////////////////////////////
// Transfer data from .text segment in ROM to .data segment in RAM
void
initRAM()
{
    char *src = &romdata;
    char *dst = &bdata;

    // Copy ROM data at end of text to RAM
    while (dst < &edata) {
        *dst++ = *src++;
    }
    // Zero bss
    for (dst = &bstart; dst< &bend; dst++) {
        *dst = 0;
    }
}

////////////////////////////////////////////////////////////////////
// Deal with environment variable

char *__env[1] = { 0 };
char **environ = __env;

//typedef char * caddr_t;

////////////////////////////////////////////////////////////////////
// sbrk
caddr_t
sbrk(int incr) {
   extern char end; /* Defined by the linker. */
   static char *heap_end = 0;
   char *prev_heap_end;

   if (heap_end == 0) {
      heap_end = &end;
   }
   prev_heap_end = heap_end;
   heap_end += incr;
   return (caddr_t) prev_heap_end;
}


/********************************************
 link.cmd
********************************************/
SEARCH_DIR(s:\tools\sh\lib)
/* SEARCH_DIR(C:\cygnus\sh\H-i386-cygwin32\sh-hms\lib) */
/* SEARCH_DIR(C:\cygnus\sh\H-i386-cygwin32\lib\sh-hms\gcc-lib\2.7-97r2a) */

GROUP(libc.a libgcc.a)
/* INPUT(libc.a libg++.a libstdc++.a libm.a libiberty.a) */
/* add libm.a to the above statement to add math library support (e.g. sin()) */

OUTPUT_FORMAT(symbolsrec)
OUTPUT_ARCH(sh)

MEMORY
{
  eprom : o = 0x0, l = 128k
  ram : o = 0xf000000, l = 8k
}

SECTIONS
{
  .vect 0x0: {
    *(.vect);
  } > eprom

  .text 0x400: {
     _btext = . ;
    *(.text)
    *(.strings)
     _etext = . ;
     _romdata = . ;
  } > eprom

  .tors : {
    ___ctors = . ;
    *(.ctors)
    ___ctors_end = . ;
    ___dtors = . ;
    *(.dtors)
    ___dtors_end = . ;
  }  > eprom

  .data : AT (_romdata) {
     _bdata = . ;
    *(.data)
     _edata = . ;
  }  > ram

  .bss : {
     _bstart = . ;
    *(.bss)
    *(COMMON)
     _bend = . ;
  }  > ram

   .stack 0xf001ff0: {
    _stack = .;
  } > ram

/*

  .stab 0 (NOLOAD) :
  {
    *(.stab)
  }

  .stabstr 0 (NOLOAD) :
  {
    *(.stabstr)
  }
*/
}





Johan Nilson wrote:

> After much mumbling and grumbling I have finally manged to build my first ever GCC cross-compiler.
> I built it with ecgs-1.1.2 for Hitachi SH target.
> It "seems" to work (I don't really have a target machine yet), and I think I have even managed
> to make a linker script to put code and variables in proper memory areas (embedded target).
>
> There is one thing however that puzzles me.
> Where can I find the c initialization routine and/or instructions on what it need to contain.
> I imagine there must be a routine for setting up initial register content, stack initialization, zeroing out
> uninitialized data, setting up initializard data etc, before jumping to main()?
> Also, how do I define size and location of the stack? Is there a .stack segment in addition to .text, .data and .bss?
>
> I have to admit I'm not entirely clear on the directory structure and contents of the compiler I managed to build, but I
> did find a few files named crt0.o, and I read somewhere that this would be the routine taking care of this. But why is
> there only a object file? How do I know if it handles "ROMable" code, and if not, how do I know how to write my own
> initialization routine?
>
> Thankful for advise,
>
> Johan


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]