Creating a microcontroller startup file

Mike via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 8 16:18:40 PDT 2015


On Wednesday, 8 April 2015 at 17:45:01 UTC, Jens Bauer wrote:
> On Wednesday, 8 April 2015 at 15:53:37 UTC, Jens Bauer wrote:
>> [snip] I find it strange that calling an empty function outside
>> the source file will cause that huge difference.
>> -But of course, there's a logic explanation somewhere. ;)
>>
>> It might be caused by the linker script; I'll try and see if I 
>> can modify it to get rid of those things.
>
> Nope, that wasn't it. However, I found out that when I call an 
> external function, some unwinding code is forced upon me; it's 
> used by libgcc. I can't seem to get rid of it. I've removed the 
> -lgcc from my linker flags (along with *all* other libraries, 
> and it's still forced upon me.
>
> I tried to remove as much of the druntime, as I could, but it 
> did not help a tad.

I can think of two places in the runtime where extra code can be 
added to your binary: runtime initialization and thread-local 
storage.  There may be others depending on what features are 
implemented in the runtime.

You can find the runtime initialization code for GDC's runtime 
here:  
https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/rt/dmain2.d#L152

That eventually will call some things that malloc, and more. That 
all happens before you get to main.  Are you compiling with 
-fno-emit-moduleinfo?  That may help reduce some of that runtime 
initialization.

As I recall, thread-local storage also employs malloc as memory 
is needed for each thread-local variable when a new thread is 
created.  If your project is single-threaded, it still works the 
same way when the initial thread is created.  If you have any 
thread-local state try removing them or changing them to 
__gshared, and see if that helps.

My runtime is quite minimal, which has both benefits and 
consequences.  You can find it here: 
https://github.com/JinShil/stm32f42_discovery_demo/tree/master/source/runtime

This will give you a C-like programming experience.  You can use 
classes, but won't be able to allocate them on the GC heap.  
Instead, you can employ some of the patterns here:  
http://wiki.dlang.org/Memory_Management

You may also wnat to compile with -nodefaultlibs -nostdlib 
-nostartfiles.  That removes the cruntime and libgcc.  But if you 
do that, you may have to compensate by adding additional startup 
code In D.  You can see how I've done that here:  
https://github.com/JinShil/stm32f42_discovery_demo/blob/master/source/start.d#L102

As I understand it, minlibd is a more full-featured runtime, and 
that too has its benefits and consequences.

Mike


More information about the Digitalmars-d-learn mailing list