Binding vs Linking in Ada
Asked Answered
I

2

5

I wonder what is the fundamental difference between binding and linking when working with Ada code? I couldn't find a good explanation on google and this is why I ask the question.

For the binding process what is the input and what is the output? What is the relation between binding and linking? I assume binding needs to be done first.

Thanks, Bogdan.

Insulin answered 1/6, 2018 at 8:10 Comment(0)
S
9

With GNAT, there are two jobs which the binder performs: first, checking that all the necessary compilations have been done, so that the program’s closure is consistent, and secondly arranging for elaboration to happen (these jobs are needed for any Ada build system, but they may be implemented differently).

When using gnatmake, the first of these jobs is usually superfluous, because gnatmake has already organised all the necessary compilations. It is possible to get this wrong (by, for example, moving a unit to a different library and not deleting its compilation products from the original place) but quite hard!

Elaboration is a feature of Ada that isn’t present in many other languages. There’s explanation at gcc.gnu.org and other places, but for a simple example,

with Foo;
package Bar is
   Int : Integer := Foo.Value;
   [...]
end Bar;

package Foo is
   function Value return Integer;
   [...]
end Foo;

we don’t know what Foo.Value is going to return at compile time, and we may not know until run time (what if it reads a value from the command line?), so Foo.Value must be in a fit state to be called before Bar’s initialisation happens.

Bar’s initialisation happens when Bar is elaborated, and likewise for Foo, so it’s gnatbind’s job to recognise this and arrange that Foo is elaborated before Bar.

It does this by emitting calls to packages’ elaboration code in a function (usually called adanit), and a main(), which is to be called by the operating system and calls adainit and then the Ada main program, say program.adb.

gnatmake then calls gnatlink, which takes the gnatbind-generated code, in Ada in files called b-program.ad[sb] or b__program.ad[sb] or b~program.ad[sb] depending on the vintage of the compiler, compiles it, and links it with the program’s closure to produce the final executable.

Schnell answered 1/6, 2018 at 10:3 Comment(2)
Thanks for the answer. So is it correct to say like this: that binding is a pre-processing stage which ensures that code will run without any errors that may have to do with uninitialized sw components? This pre-processing stage operates solely on source code (pretty much like the C pre-processor) and generates extra source code that will be later compiled and linked without any difference compared to C and C++ ?Insulin
Logically it only needs to operate on source code, but in fact the dependency information includes the checksum and date of the files that this file was compiled against. Binding isn’t at all like the C preprocessor, which changes the source code that is seen by the compiler; binding generates extra source code.Schnell
D
5

See the four points listed here: https://docs.adacore.com/gnat_ugn-docs/html/gnat_ugn/gnat_ugn/building_executable_programs_with_gnat.html#binding-with-gnatbind

You could think of it as a built-in make but without the recompilation: it ensures objects are consistent, generates a correct initialization order, compiles it, and passes everything to the linker.

As pointed out, in Ada the program entry point is not your main procedure, but one that performs a safe initialization and then calls your main procedure.

Dobb answered 1/6, 2018 at 11:58 Comment(1)
"make, done for you" is a pretty good way of looking at it. A (small) part of Ada's complexity goes into putting hhe information to eliminate "Make" in the source code, and binding processes that information. For practical purposes, you only need to learn one language (Ada) instead of two (e.g. C and Make syntax)Gouache

© 2022 - 2024 — McMap. All rights reserved.