comp.lang.ada
 help / color / mirror / Atom feed
* Can I get rid of C libraries dependencies?
@ 2015-09-16 12:55 Leff Ivanov
  2015-09-16 13:44 ` Simon Clubley
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Leff Ivanov @ 2015-09-16 12:55 UTC (permalink / raw)


Okey, so I wrote this code:

pragma No_Run_Time;

with Interfaces.C.Strings;
use  Interfaces.C.Strings;
with Interfaces.C;
use  Interfaces.C;

procedure Main is
   procedure cputs(str:Char_Array);
   pragma Import(C, cputs, "puts");
   
   procedure cexit(code:Integer);
   pragma Import(C, cexit, "exit");
begin
   cputs("Hello World!");
   cexit(0);
end Main;

What I want to achive is a tiny 2kb binary executable 
with a single function called as entry point (which is 
defined in Ada and called Main procedure). The pragma 
on top let me get rid of the huge RTL code that is written
in Ada. However I'm still getting a lot of code linked 
in from C libraries, so...

1) Can I get rid of the C code linked in?
2) What is implemented in thoose C libraries?
3) Can I use Ada's RTL library without the C 
   support code, if I use some Restrictions?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Can I get rid of C libraries dependencies?
  2015-09-16 12:55 Can I get rid of C libraries dependencies? Leff Ivanov
@ 2015-09-16 13:44 ` Simon Clubley
  2015-09-16 14:20 ` G.B.
  2015-09-16 16:13 ` Simon Wright
  2 siblings, 0 replies; 6+ messages in thread
From: Simon Clubley @ 2015-09-16 13:44 UTC (permalink / raw)


On 2015-09-16, Leff Ivanov <droiddermo@gmail.com> wrote:
>
> What I want to achive is a tiny 2kb binary executable 
> with a single function called as entry point (which is 
> defined in Ada and called Main procedure). The pragma 
> on top let me get rid of the huge RTL code that is written
> in Ada. However I'm still getting a lot of code linked 
> in from C libraries, so...
>
> 1) Can I get rid of the C code linked in?
> 2) What is implemented in thoose C libraries?
> 3) Can I use Ada's RTL library without the C 
>    support code, if I use some Restrictions?

The basic problem here is that the C API _is_ considered to be the
interface to the OS for this kind of thing on Linux/Unix and
(these days) probably Windows as well[*].

Unless you call the operating system services directly then at
some level either you, or the Ada RTL acting on your behalf, has
to call the C API.

It is possible to call the OS services directly (once you understand
how the interface is implemented for each OS and architecture
combination), but is this really a road that it's wise to do down ?

Simon.

[*] I have a VMS background and it's a lot easier to do this kind
of thing there. Unfortunately, I doubt you are running VMS. :-)

-- 
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Can I get rid of C libraries dependencies?
  2015-09-16 12:55 Can I get rid of C libraries dependencies? Leff Ivanov
  2015-09-16 13:44 ` Simon Clubley
@ 2015-09-16 14:20 ` G.B.
  2015-09-16 16:13 ` Simon Wright
  2 siblings, 0 replies; 6+ messages in thread
From: G.B. @ 2015-09-16 14:20 UTC (permalink / raw)


On 16.09.15 14:55, Leff Ivanov wrote:
> 3) Can I use Ada's RTL library without the C
>     support code, if I use some Restrictions?

Maybe, depending on the restrictions. Also, depending on how
the object code is to be installed: there is a documented
way to use a C main, not an Ada main; if this, in spite
of mentioning C, can mean that you just call the object
code using C conventions, then linking need not be done at
the Ada tools chain's end. If your object code is guaranteed
not to require Ada RT support, it should work, I think.

Some bits of a reduced RTS may be ready for inspection
with implementations of GNAT for smaller computers
(incl. no OS, IIRC). I'd start by looking for ARM, Cortex,
STM32, Atmega, ...


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Can I get rid of C libraries dependencies?
  2015-09-16 12:55 Can I get rid of C libraries dependencies? Leff Ivanov
  2015-09-16 13:44 ` Simon Clubley
  2015-09-16 14:20 ` G.B.
@ 2015-09-16 16:13 ` Simon Wright
  2015-09-17  8:13   ` Leff Ivanov
  2 siblings, 1 reply; 6+ messages in thread
From: Simon Wright @ 2015-09-16 16:13 UTC (permalink / raw)


Leff Ivanov <droiddermo@gmail.com> writes:

> What I want to achive is a tiny 2kb binary executable 

Can you tell us a little more about why you need to do this, what OS
you're using ...


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Can I get rid of C libraries dependencies?
  2015-09-16 16:13 ` Simon Wright
@ 2015-09-17  8:13   ` Leff Ivanov
  2015-09-17 10:25     ` Brian Drummond
  0 siblings, 1 reply; 6+ messages in thread
From: Leff Ivanov @ 2015-09-17  8:13 UTC (permalink / raw)


среда, 16 сентября 2015 г., 19:13:11 UTC+3 пользователь Simon Wright написал:
> Leff Ivanov <droiddermo@gmail.com> writes:
> 
> > What I want to achive is a tiny 2kb binary executable 
> 
> Can you tell us a little more about why you need to do this, what OS
> you're using ...

I'm using windows and want to write tiny tools in Ada like I can 
write one in C/C++ using -nostdlib flag and directly linking to
OS specific libraries. The same can be applied to Linux and Mac
OSX as well, but currently I'm focusing on Windows.

If I do "gnatmake -a -f -nostdlib main.adb" I can see that GNAT
actually recompiles a lot of stuff from RTS, like a-except.adb,
s-secsta.abd and etc. So the No_Run_Time pragma cuts off only
a part of Ada RTS, but leaves the rest.

Gnatlink tool with -nostdlib returns a lot of undefined references
to C code, which I want to get rid off. It seems that mostly it is
functions that implements exceptions, like _Unwind_SjLj_Register,
__gnat_personality_sj0 and etc. 

Undefined references to stuff like memmove and memcmp I can simply
fix by linking directly with libmsvcrt.a, so theese functions would
be taked from standard windows msvcrt.dll library. But there's also 
undefined reference to __chkstk_ms, this function seem to check if 
the stack is full. This function is MinGW specific and there is no implementation in standard windows libraries.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Can I get rid of C libraries dependencies?
  2015-09-17  8:13   ` Leff Ivanov
@ 2015-09-17 10:25     ` Brian Drummond
  0 siblings, 0 replies; 6+ messages in thread
From: Brian Drummond @ 2015-09-17 10:25 UTC (permalink / raw)


On Thu, 17 Sep 2015 01:13:47 -0700, Leff Ivanov wrote:

> среда, 16 сентября 2015 г., 19:13:11 UTC+3 пользователь Simon Wright
> написал:
>> Leff Ivanov <droiddermo@gmail.com> writes:
>> 
>> > What I want to achive is a tiny 2kb binary executable
>> 
>> Can you tell us a little more about why you need to do this, what OS
>> you're using ...
> 
> I'm using windows and want to write tiny tools in Ada like I can write
> one in C/C++ using -nostdlib flag and directly linking to OS specific
> libraries. The same can be applied to Linux and Mac OSX as well, but
> currently I'm focusing on Windows.
> 
> If I do "gnatmake -a -f -nostdlib main.adb" I can see that GNAT actually
> recompiles a lot of stuff from RTS, like a-except.adb,
> s-secsta.abd and etc. So the No_Run_Time pragma cuts off only a part of
> Ada RTS, but leaves the rest.
> 
> Gnatlink tool with -nostdlib returns a lot of undefined references to C
> code, which I want to get rid off. It seems that mostly it is functions
> that implements exceptions, like _Unwind_SjLj_Register,
> __gnat_personality_sj0 and etc.

There are pragmas to restrict the use of exceptions : these should reduce 
the undefined references.
 
> Undefined references to stuff like memmove and memcmp I can simply fix
> by linking directly with libmsvcrt.a, so theese functions would be taked
> from standard windows msvcrt.dll library.  But there's also undefined
> reference to __chkstk_ms, this function seem to check if the stack is
> full. 

One way is to specify your own RTS, with the "--RTS=path/to/rts" option. 
Then you would need to create your own RTS, which in this case would be 
your wrapper that handles anything you can't link directly like memcpy 
etc. You can get a handle on how this works by studying the AVR-Ada 
microcontroller port, which is quite capable of generating executables 
down to a couple of hundred bytes.

-- Brian

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-09-17 10:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-16 12:55 Can I get rid of C libraries dependencies? Leff Ivanov
2015-09-16 13:44 ` Simon Clubley
2015-09-16 14:20 ` G.B.
2015-09-16 16:13 ` Simon Wright
2015-09-17  8:13   ` Leff Ivanov
2015-09-17 10:25     ` Brian Drummond

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox