Potential of a compiler that creates the executable at once

max haughton maxhaton at gmail.com
Fri Feb 11 17:20:50 UTC 2022


On Friday, 11 February 2022 at 06:33:20 UTC, Walter Bright wrote:
> On 2/10/2022 7:45 PM, max haughton wrote:
>> If by hook you mean a callback of sorts that can be overrided, 
>> then the problem solved is not strictly the same as a weakly 
>> defined function. If you have multiple library's in the same 
>> playpen then it simply doesn't work to have them all trying to 
>> override the same symbols. If they can neatly hook and unhook 
>> things that goes away.
>
> That's not how multiple libraries work.
>
> Suppose you have 3 libraries, A, B, and C. You have an object 
> file X. The linker command is:
>
>     link X.obj A.lib B.lib C.lib
>
> X refers to "foo". All 4 define "foo". Which one gets picked?
>
>    X.foo
>
> That's it. There are no unresolved symbols to look for.
>
> Now, suppose only B and C define "foo". Which one gets picked?
>
>    B.foo
>
> because it is not in X. Then, A is looked at, and it is not in 
> A. Then, B is looked at, and it is in B. C is not looked at 
> because it is now resolved.
>
> It has nothing to do with weak definitions. It's a simple "foo" 
> is referenced. Got to find a definition. Look in the libraries 
> in the order they are supplied to the linker.
>
> That's it.
>
> Want to not use the library definition? Define it yourself in 
> X. No need for hooking. No need for anything clever at all. 
> Just define it in your .obj file.
>
> ----
>
> Now suppose X.obj and Y.obj both define foo. Link with:
>
>     link X.obj Y.obj A.lib B.lib C.lib
>
> You get a message:
>
>     Multiple definition of "foo", found in X.obj and Y.obj
>
> because order does not matter for .obj files as far as symbols 
> go. All the symbols in .obj files get added.

If all the libraries rely on hooking something you will silently 
break all but one, whereas the process of overriding a runtime 
hook can be made into an atomic operation that can fail in a 
reasonable manner if wielded incorrectly.

Doing things based on the order at link-time is simply not good 
practice in the general case. It's OK if you control all the 
things in the stack and want to (say) override malloc, but 
controlling what happens on an assertion is exactly the kind of 
thing that resolution at link-time can make into a real nightmare 
to do cleanly (and mutably, you might want to catch assertions 
differently when acting as a web server than when loading data).

Also linking (especially around shared libraries) doesn't work in 
exactly the same way on all platforms, so basically maximizing 
the entropy of a given link (minimize possible outcomes, so 
minimal magic) can be a real win when it comes to making a 
program that builds and runs reliably on different platforms. At 
Symmetry we have had real issues with shared libraries, for 
reasons more complicated than mentioned here granted, so we 
actually cannot ship anything with dmd even if we wanted to.


More information about the Digitalmars-d mailing list