Potential of a compiler that creates the executable at once
sfp
sfp at hush.ai
Fri Feb 11 12:34:40 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.
You have now successfully explained this to at least one
programmer! :-) Very good explanation, and very simple mechanism
indeed. Had no idea it worked this way.
Inspired by this, I did a little searching and found this blog
post:
http://www.samanbarghi.com/blog/2014/09/05/how-to-wrap-a-system-call-libc-function-in-linux/
One of these days I should get around to learning all the things
the toolchain can actually do for me!
More information about the Digitalmars-d
mailing list