伏虎 wrote:
> On Apr 20, 9:51 pm, DJ Delorie <
d...@delorie.com> wrote:
>>伏虎 <
fuhu...@hotmail.com> writes:
>>>On Apr 20, 6:52 pm, DJ Delorie <
d...@delorie.com> wrote:
>>>>It starts in main, just like C programs.
>>
>>>no. befor main, some constructors need to be run.
>>
>>That's up to the startup code for your system. In ELF, for example,
>>crt0.s or the equivalent is responsible for running all the .init/.fini
>>code, which then calls the constructors and destructors. Where that
>>code is, and how it gets called, depends on your tools, it's not a
>>generic question we can answer here. The GNU tools, for example,
>>typically look for a symbol called "_start" or "start".
>
> my question is I have a piece of asm code, and I want to start the C++
> main(), which function shall I call?
>
> I am using gnu tool.
This is very specific to the library you are using. If you are using GNU
tools, chances are there is library source code you can look at.
Execution starts at whatever symbol you tell the linker to use as entry
point ("-e start"). At that symbol, you'll usually find code to set up
stack, initialize malloc, clear bss, initialize stdio, and call
constructors. In an embedded context, it might also set up hardware,
interrupts, etc. You'll have to figure out yourself (with your library's
source code and documentation) for your specific environment what you
have to do to fulfill the requirements for calling 'main', there is no
general answer.
If you know what your compiler does, you could even get away with
start:
ldr sp, =initial_stack
bx main
I've been doing that in several low-level projects like boot loaders,
that do not need constructors, malloc, stdio, bss, etc.
Stefan