This is the mail archive of the ecos-patches@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [ECOS] arguments to main


>>>>> "Gary" == Gary Thomas <gary@chez-thomas.org> writes:

    Gary> On Wed, 2002-07-31 at 11:41, Andrew Lunn wrote:
    >> > Additionally, these variables should be exported via some HAL .h file,
    >> > possibly <cyg/hal/hal.h> or maybe <cyg/hal/hal_arch.h>.
    >> > 
    >> > Finally, there should be appropriate ChangeLog entries.
    >> > 
    >> 
    >> I could not work out how to get access to the environment. I
    >> can see it on the stack, but i cannot find an obvious pointer
    >> to it anywhere. Does anyone know of any documentation of how
    >> the linux kernel sets up the stack before handing it over to
    >> the new process?

    Gary> IIRC, you have to compute it. You could look at the GLIBC
    Gary> code (crt0.s) to figure it out.

I believe the correct way to get hold of both arguments and the
environment vector is in the vectors.S startup, ensuring that the
right thing can be done for different architectures. The following
code should do the right thing:

------------------------------------------------------------------------
# According the SVR4/i386 ABI, most registers are undefined. However
# there is some interesting information on the stack:   
#     %esp              argc
#     %esp+4            argv[0]
#     ...               argv[1 onwards]
#     %esp + (argc*4)   NULL
#     ...               environ[0]
#     ...               ...
#     ...               NULL                                                
#
# There are some other things that could be done, for example aligning
# the stack to a 16-byte boundary for SSE, but it is not clear which of
# those things are actually useful. The glibc source file
# sysdeps/i386/elf/start.S contains some relevant information.
                                
        .file   "vectors.S"

	.extern	_linux_entry

        .data
        .global cyg_hal_sys_argc
cyg_hal_sys_argc:       
        .long   0
        .global cyg_hal_sys_argv
cyg_hal_sys_argv:
        .long   0
        .global cyg_hal_sys_environ
cyg_hal_sys_environ:
        .long 0
        
	.text
	.globl	_start
_start:
        popl    %eax
        movl    %eax, cyg_hal_sys_argc
        movl    %esp, %ebx
        movl    %ebx, cyg_hal_sys_argv
        inc     %eax
        addl    %eax, %eax
        addl    %eax, %eax
        addl    %eax, %ebx
        movl    %ebx, cyg_hal_sys_environ
                        
	jmp	_linux_entry
------------------------------------------------------------------------

This creates variables cyg_hal_sys_argc, argv and environ, to go
alongside functions like cyg_hal_sys_open(). The argc, argv and
environ variables are not touched, deliberately. It forces code
that uses argv to use a #ifdef for e.g. CYGPKG_HAL_SYNTH, or the code
won't compile for targets other than the synthetic target. Otherwise
people might start depending on being able to set eCos environment
variables, and run into strange difficulties when development switches
to a real embedded target.

Bart


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