News for hackers. Stuff that matters.
Re:Dynamic linking by cryptonector on Friday June 26, 2020 @03:47PM (#23656173)
By far the most important reason for dynamic linking for C is semantics: static linking semantics are stuck in 1978 and suck (more on that below), while dynamic linking semantics make C a much better language.

In particular, static linking for C has two serious problems:

1. symbol collisions -> accidental interposition (and crashes);

2. you have to flatten the dependency tree into a topological sort at the final link-edit.

Both of these are related, and they are disastrous. They are also related to the lack of namespaces in C.

Besides fixing these issues, the C dynamic linking universe also enables things like:

- run-time code injection via LD_PRELOAD and intended interposition

- run-time code loading/injection via dlopen(3)

- audit (sotruss)

- reflection

- filters (which allow one to move parts of libraries contents to other libraries without forcing re-links and without forcing built systems to change to add new -lfoo arguments to link-edits)

- use of dladdr(3) to find an object's install location, and then that to find related assets' install locations relative to the first, which then yields code that can be relocated at deploy time (sure, "don't do that" is a great answer, but if you statically-link then you think you can, and now you just can't have assets to load at run-time)

- use of weak symbols to detect whether a process has some library loaded

and others.

C with those features is a far superior language -- a different language, really -- to C without them.

(EDIT: A lot of the semantics of ELF could be brought to static linking. Static link archives could have a .o that has metadata like depedencies, "rpaths", exported/protected symbols, interposer symbols, etc. The link-editor would write and consume that metadata. However, it's 2020, and the static link ecosystem is stuck in 1980 because no one has bothered, and no one has bothered because dynamic linking is pretty awesome. Still, it could be done, and once in a while I think I ought to do it to help save people from themselves who want static linking.)

> Do your installed programs share dynamic libraries?

> Findings: not really

> Over half of your libraries are used by fewer than 0.1% of your executables.

The C library most certainly gets shared, as well as libm and such. The rest, it's true, not so much, but it does depend on what you're measuring. Are you measuring C++ apps? Yeah, C++ monomorphization leads to essentially static linking. Are you measuring Java apps with no significant JNI usage? You won't find much outside the libraries the JVM uses.

> Is loading dynamically linked programs faster?

> Findings: definitely not

Dynamically-linked programs will load faster when their dependencies are already loaded in memory, and slower otherwise. The biggest win here is the C library.

> Will security vulnerabilities in libraries that have been statically linked cause large or unmanagable updates?

> Findings: not really

Correct. But, being able to update libc or some such and not have to worry about updating consumers you might not even know about is a very nice feature.

Re:Dynamic linking by mforney on Friday June 26, 2020 @06:19PM (#23657707)
You bring up some good points here. Here are some of my experiences with these problems when working on oasis (my static linux distro).

> 1. symbol collisions -> accidental interposition (and crashes);

I've encountered symbol collisions only twice, but both resulted in linker errors due to multiple function definitions. I'm not sure how this could happen accidentally. Maybe you are referring to variables in the common section getting merged into a single symbol? Recent gcc enables -fno-common by default, so those will be caught by the linker as well.

> 2. you have to flatten the dependency tree into a topological sort at the final link-edit.

Yes, this is pretty annoying. pkg-config can solve this to some degree with its --static option, but that only works if your libraries supply a .pc file (this is often the case, though).

I think libtool also can handle transitive dependencies of static libraries, but it tries hard to intercept the -static option before it reaches the compiler so it links everything but libc statically. You can trick it by passing `-static --static`.

For oasis, I use a separate approach to linking involving RSP files (i.e. linking with @libfoo.rsp), which really are just lists of other libraries they depend on.

> Besides fixing these issues, the C dynamic linking universe also enables things like: > - run-time code injection via LD_PRELOAD and intended interposition

Yes, this can be a problem. I wanted to do this recently to test out the new malloc being developed for musl libc, but ended up having to manually integrate it into the musl sources instead of just using LD_PRELOAD.

> - run-time code loading/injection via dlopen(3)

In particular, this is a big problem for scripting languages that want to use modules written in compiled languages, as well as OpenGL which uses dlopen to load a vendor-specific driver.

> Dynamically-linked programs will load faster when their dependencies are already loaded in memory, and slower otherwise. The biggest win here is the C library.

But doesn't the dynamic linker still have to do extra work to resolve the relocations in the executable, even when the dependency libraries are already loaded?

Re:Dynamic linking by cryptonector on Friday June 26, 2020 @06:40PM (#23657868)
> > 1. symbol collisions -> accidental interposition (and crashes);

> I've encountered symbol collisions only twice, but both resulted in linker errors due to multiple function definitions. I'm not sure how this could happen accidentally. Maybe you are referring to variables in the common section getting merged into a single symbol? Recent gcc enables -fno-common by default, so those will be caught by the linker as well.

No, this comes up all the time. Try building an all-in-one busybox-style program, and you'll quickly run into conflicts.

If static link archives had all the metadata that ELF files have, then the link-editor could resolve conflicts correctly. That is the correct fix, but no one is putting effort into it. The static linkers haven't changed much since symbol length limits were raised from 14 bytes!

> > 2. you have to flatten the dependency tree into a topological sort at the final link-edit.

> Yes, this is pretty annoying. pkg-config can solve this to some degree with its --static option, but that only works if your libraries supply a .pc file (this is often the case, though).

pkg-config alleviates the problem, but it's not enough. Among other things building a build system that can build with both, static and dynamic linking is a real pain. But more importantly, this flattening of dependency trees loses information and makes it difficult for link-editors to resolve symbol conflicts correctly (see above).

> > Dynamically-linked programs will load faster when their dependencies are already loaded in memory, and slower otherwise. The biggest win here is the C library.

> But doesn't the dynamic linker still have to do extra work to resolve the relocations in the executable, even when the dependency libraries are already loaded?

It's still faster than I/O. (Or at least it was back in the days of hard drives. But I think it's still true even in the days of SSDs.)

Re:Dynamic linking by psykotic on Friday June 26, 2020 @10:26PM (#23659034)
> But doesn't the dynamic linker still have to do extra work to resolve the relocations in the executable, even when the dependency libraries are already loaded?

For dynamic linking there's usually only a single reference that needs to be fixed up in the PLT or GOT for each referenced symbol and the fix-ups are all localized, so that part is typically a very small cost. But this means every call to a dynamically linked library goes through an extra stub function, which adds I$ and BTB pressure compared to static linking.

(If you do things manually with dlsym there's also an extra indirection but it's a little different mechanically since you do an indirect call of a function pointer rather than doing a direct call to a forwarding stub. The advantage of the forwarding stub is that even if there's a BTB miss for either of the two direct calls, you don't suffer a branch mispredict but just a few front-end cycles waiting for the instruction decode, which VTune labels as a "branch resteer". The direct call option also leads to smaller code size for each call site which helps with I$ pressure. Basically it's the difference between CALL rel32 and MOV tmp, [RIP + disp32]; CALL [tmp].)

Re:Dynamic linking by cryptonector on Friday June 26, 2020 @04:16PM (#23656472)
In a containerized world, static linking will generally be faster, unless the OS does page de-duping for text/rodata. So doing the work to make static linking semantics not suck seems likely to be important.
Re:Dynamic linking by saurik on Saturday June 27, 2020 @12:21AM (#23659489)
Most linkers support archive "groups" at the command line, so you don't need to do the topological sort. Most linkers also support relocatable objects, so you can trivially solve the symbol collision problem by just linking together a subset of your app and then stripping that symbol before linking the conflict. And like, it frankly just sounds like you have never heard of libtool, which adds most of the stuff in your edit--like dependencies and configurable linker settings--to the static link process, and which has not only existed but has been widely used among most marquee projects for over two decades. :/
Re:Dynamic linking by cryptonector on Saturday June 27, 2020 @12:58AM (#23659621)
I know all about libtool, thank you, and I'm quite aware that it writes dependency information into its .la files. I use libstool every day. It sucks. This functionality has to be built into the linker.
Re:Dynamic linking by saurik on Sunday June 28, 2020 @01:23AM (#23666906)
Making the linker--which is architecture specific and generally "just barely what is required to munge together object files"--into some fat stack of code (that I guess you would expect to work the same for every single linker) because you don't believe wrappers are appropriate for some reason is a disagreement over "clean reusable architecture" and not that the ecosystem as a whole is somehow "stuck in the 80s" :/.
Re:Dynamic linking by cryptonector on Sunday June 28, 2020 @03:10PM (#23671231)
Part of what has to be done can be done in wrappers, it's true: the part that has to do with recording additional metadata. But the other part -using that metadata to resolve symbol conflicts- cannot be done in a wrapper -- it has to be done in the linker-editor. IMO this is a very strong argument for putting all of this in the linker-editor.
Re:Dynamic linking by ndesaulniers on Saturday June 27, 2020 @01:30AM (#23659733)
> - use of weak symbols to detect whether a process has some library loaded

RTLD_NOLOAD

> Dynamically-linked programs will load faster when their dependencies are already loaded in memory, and slower otherwise. The biggest win here is the C library.

RTLD_LAZY, though it's one less attack vector to have RTLD_NOW and mprotect your GOT and PLT as readonly.