News for hackers. Stuff that matters.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by RcouF1uZ4gsC on Saturday February 20, 2021 @01:58PM (#26206777)
To be honest, I am actually glad about this. Dynamic linking is the cause of a lot of complexity. First there is DLL hell on Windows and its equivalent on Linux. Second, having to maintain a stable ABI is a huge drain on language evolution. In C++, there have been many features that have been vetoed by the compiler/standard library implementers because it would cause an ABI break. Because of this ABI concerns, C++ is stuck with overheads for unique_ptr, map, unordered_map, and regex classes that are inefficient.

Secondly, a lot of the benefits of dynamic linking don’t make as much sense now.

First with regards to space savings. With modern disk capacity having extra copies of executable code is probably not that big of a deal. In addition with Link Time Optimization, you actually may not be saving space with dynamic linking since the linker can pretty aggressively throw away code that it knows is ever called.

Second, with regards to security. One thing to consider is that dynamic linking is so complex that it is one of the reasons why people use docker, to make sure their binary and its dependencies are stable. Once you have something in a docker, it has a lot of the same update issues as a static linked binary, only less efficient.

In addition, with security, Rust unlike C and C++ is a memory safe language. Given the vast majority of security issues are memory safety issues, I would expect Rust to have a lot fewer CVE’s. I think the experience with Go is likely enlightening in this regard. I am not aware of a huge issue of lack of security because it didn’t do dynamic linking and you couldn’t do security updates as easily.

Finally, Rust as a new language is embracing the new way of programming and deploying. We are shifting away from using binary artifacts (often closed source) that once built were rarely changed, to build from source and continuously build/deploy. Cargo makes it relatively easy to do this. In that model, updating a binary for security is just a subset of the normal building and updating of a binary that is done daily.

By avoiding dynamic linking, I think, Rust positions itself best as the no-compromise, high performance, modern, systems programming language.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by millstone on Saturday February 20, 2021 @02:24PM (#26206996)
Rust is certainly made simpler by not sweating ABI stability. But the main advantage of dynamic linking is to enable yesterday's app to run on tomorrow's OS (and vice-versa). It's why your apps keep working when you update your phone.

Without a dynamic linking story, Rust is just not viable for writing UIKit, Android's frameworks, etc. Which is OK, it's a reasonable choice, but it limits Rust's scope.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by dralley on Saturday February 20, 2021 @03:11PM (#26207471)
Zig is in a really good position here. Its featureset sticks closer to C, and being able to automatically generate C headers from Zig code and automatically consume C headers from Zig code is really convenient.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Saturday February 20, 2021 @03:17PM (#26207533)
Not until it sorts out its use-after-free story.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by millstone on Saturday February 20, 2021 @03:19PM (#26207555)
Does Zig really solve this problem? For example, if I add a field to a Zig struct, can existing code still use the struct without needing to be recompiled?
Re:How Swift achieved dynamic linking where Rust couldn't (2... by dralley on Saturday February 20, 2021 @03:26PM (#26207619)
No, but what I'm saying is, it makes dynamic linking a lot easier in general than Rust does.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by coliveira on Saturday February 20, 2021 @02:19PM (#26206954)
First of all, C/C++ can do static linking as well as dynamic linking. If static linking is preferred, people will do it. Second, dynamic linking is necessary because of how modern OSs are designed. Most GUIs are gigantic libraries that cannot be statically linked. Similarly for network code and other areas of modern OSs.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by Animats on Saturday February 20, 2021 @02:13PM (#26206892)
I tend to agree. Dynamic linking is only a win on space when 1) you have multiple programs running which share the same library, 2) they're not all identical programs which would share code, and 3) the programs use a significant fraction of what's in each dynamic library they load.

With static linking, you get to prune at the function level at link time. DLLs can't do that.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by coliveira on Saturday February 20, 2021 @02:22PM (#26206981)
A major use of dynamic linking is loading extensions. Without DL you'll have a hard time using languages such as Python, or creating extensions to applications.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by sgtnoodle on Saturday February 20, 2021 @03:12PM (#26207483)
Dynamic linking and dynamic loading aren't necessarily the same thing, are they? I imagine one could statically link in some C code to a rust program that uses dlopen, for example, or even just do the syscall directly in rust (assuming that's possible).
Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Saturday February 20, 2021 @03:18PM (#26207544)
Yes they are, exactly the same OS infrastructure.

It doesn't matter if it is the kernel or your application that calls dlopen().

How do imagine that something like ld.so gets implemented?

Re:How Swift achieved dynamic linking where Rust couldn't (2... by sgtnoodle on Saturday February 20, 2021 @03:53PM (#26207850)
The difference, I think, is that dynamic linking necessarily binds local symbols to the dynamically loaded foreign code, while dynamic loading simply loads the foreign code into the same address space and provides an API for looking up symbols' addresses by name. There's no inherent need for the ABI to match, as long as you don't directly try to call into a function; you can write adapters to explicitly handle the dynamically loaded code's ABI, like python does with ctypes.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Saturday February 20, 2021 @04:10PM (#26208008)
Nope, that is the same infrastructure, just the details change across how they get exposed across OSes, specially non-POSIX ones.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by sgtnoodle on Saturday February 20, 2021 @05:41PM (#26208673)
At the end of the day it's all machine code running on silicon. What point are you trying to make? I was responding to the claim that a lack of good support for dynamic linking in a language prevents it from using shared objects for plugins and modules. My point was that dynamic loading is an operating system feature implemented through system calls, and can therefore be done in any language that can do a system call, whether or not the language makes it easy for you. I also claim that dynamic linking is not equivalent to loading, but I agree with you that they are related to each other and linking is indeed implemented on top of the system calls that provide dynamic loading. Dynamic linking specifically implies (for me) that non-static application symbols are resolved one way or another before the program counter jumps to main().
Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Sunday February 21, 2021 @02:39AM (#26211641)
First of all, depending on the OS there are no syscalls to call to, Linux is the exception here.

Everyone else has to go through dlopen, LoadLibrary, or whatever alternative the OS offers.

Which is exactly the same code path that at some level the OS dynamic linker relies on.

If you are statically linking everything, then there are no libraries to give input to dlopen anyway, it doesn't load .a files, and .so is not what we want to support anyway.

But lets keep the assumption that in the world of static linking advocacy, explicitly loading dynamic libraries is acceptable.

SO now, not only has the application to do the job of mapping dynamic library symbols to function pointers, they are constrained to C ABI function calls, which is not Rust anyway.

As for you Linux based assumption, there are OSes, where dynamic linking doesn't take place before the PC jumps to main, you can do deferred dynamic linking. The call sites are marked for triggering a page fault on call, which only when it happens, will the OS look around for code to load.

This is how managed languages like Java and C# work, which many keep forgetting there are also AOT compilers, just in case the VM arguments comes into play.

But since we are talking about systems languages, you can find this feature on Windows and Aix, just to give two examples how the OS world isn't everywhere a Linux clone.

https://docs.microsoft.com/en-us/cpp/build/reference/linker-...

https://www.ibm.com/support/knowledgecenter/ssw_aix_71/gener...

Re:How Swift achieved dynamic linking where Rust couldn't (2... by jgilias on Saturday February 20, 2021 @06:15PM (#26208986)
Just dropping by to say that you're right. You can absolutely _load_ dynamic libraries from Rust. I've done this. But of course, the executable is not dynamically _linked_ to the requested libraries. E.g., running `ldd` on my executable won't show which shared libraries it is or should be linked against. Rather, it will just fail with an error if the requested library is not there. There are crates for loading dynamic libraries that use `dlopen` under the hood.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by coliveira on Saturday February 20, 2021 @05:58PM (#26208838)
In abstract you could separate these things, but in modern OSs there is little to no difference between these concepts. Dynamic loading is implemented using dynamic linking and if you don't want to use dynamic linking then you have to reinvent the wheel down to the OS level. You will have a very hard time to make dynamic loading available in any architecture following this route.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by a1369209993 on Saturday February 20, 2021 @07:09PM (#26209431)
> It doesn't matter if it is the kernel or your application that calls dlopen().

My application calls mmap and mprotect(PROT_EXEC), not dlopen.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by sgtnoodle on Saturday February 20, 2021 @11:00PM (#26210681)
Neat, memory mapping is a nifty concept. Are you loading a raw binary blob and directly jumping into it then? That's hardcore.

I'm not familiar with the inner workings of dlopen, but I wouldn't be surprised if dlopen itself is mostly userspace code that knows how to interpret the ELF format, and internally it uses mmap as the actual system call. Maybe it has its own system call, though, to let the kernel do most of the work?

I've seen some really cool uses of mmap. It's nice for reducing the number of memory copies when you're churning through a bunch of data on disk, and often necessary when using shared memory for low overhead inter-process communication. I worked on a project that needed publish-subscribe message passing semantics with fine-grained synchronization across dozens of processes, for example, and a lock-free shared memory "disrupter" queue structure was able to handle millions of messages per second, while sockets could only handle a few hundred thousand or so.

I've seen some unfortunate uses as well. Sometimes folk like the idea of manipulating files on disk as arrays in memory, and use mmap even though there's no practical performance need for it. The code often times ends up harder to read than it would have had it used file descriptors. Also, the performance ends up worse because the implemented algorithm's access patterns unknowingly cause lots of page faults and disk seeks because the data is an "array". Often times there's an alternative way to do the same work sequentially with an additional in-memory data structure, and using file descriptors naturally biases one towards doing that.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Sunday February 21, 2021 @02:40AM (#26211647)
Try that on an OS that isn't a Linux distribution.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by a1369209993 on Sunday February 21, 2021 @05:13AM (#26212304)
Works fine on BSD (though it's been long enough that I forget which BSD) as long as you ignore the parts of the documentation written by facist morons. Haven't had occasion to beat proper linking behaviour into a Windows machine, but what I've heard suggests it'd be a pain the ass, yes. (Then again, Windows isn't a OS, it's a shitty GUI for DOS that bloated until it could read email. Kinda like OSX, except there isn't a vaguely decent OS buried under the manure.)
Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Sunday February 21, 2021 @07:16AM (#26212930)
A systems programming language cannot afford to silo itself on a single OS solutions.

There are plenty of OSes to choose from by the way, even your rant barely scratches the surface of the options available for any serious systems programming language.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by a1369209993 on Sunday February 21, 2021 @01:33PM (#26215826)
> A systems programming language cannot afford to silo itself on a single OS solutions.

If a OS (or alleged OS) demands that you dynamically link against libc (or anything else written in C) to interact with it, that problem is not the (non-C) programming language's fault.

> your rant barely scratches the surface of the [OS] options available

That's fair, but most of the not-linux I have experience with is microcontrollers or other things that are far enough from unix/posix that they don't have dlopen (mmap itself is hit-or-miss, but tends to be hit in the sorts of situations where I'd have occasion to dynamically load stuff in the first place), so there's not much I could do about that.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Saturday February 20, 2021 @03:09PM (#26207454)
And you are forced to run multiple processes with OS IPC, as means for application extensions, which uses a lot of more CPU resources to get going.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by zozbot234 on Saturday February 20, 2021 @04:05PM (#26207962)
But then you can simply deploy these multiple processes on separate machines and connect them via a network, and you basically get microservices!
Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Saturday February 20, 2021 @04:12PM (#26208033)
Only when they make use of IPC APIs that allows for that.

And even then good luck getting everything to work without issues.

Ah the wonders of having a language server microservice running across the network, or a image conversion plugin or audio DAW.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by MR4D on Saturday February 20, 2021 @02:05PM (#26206831)
> has a lot of the same update issues as a static linked binary, only less efficient.

I think this is an insightful comment. Obviously there are other reasons to use docker, but too often (cough...Python...cough) I see virtualization used for exactly that.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Saturday February 20, 2021 @03:16PM (#26207514)
>. One thing to consider is that dynamic linking is so complex that it is one of the reasons why people use docker, to make sure their binary and its dependencies are stable.

Coding for almost 40 years and I am yet to use Docker to sort out this kind of issues.

> I think the experience with Go is likely enlightening in this regard. I am not aware of a huge issue of lack of security because it didn’t do dynamic linking and you couldn’t do security updates as easily.

When I started programming, dynamic linking was only available on big iron machines filling computer rooms, we did not need Go for knowing what static linking entails.

> Finally, Rust as a new language is embracing the new way of programming and deploying. We are shifting away from using binary artifacts (often closed source) that once built were rarely changed, to build from source and continuously build/deploy. Cargo makes it relatively easy to do this. In that model, updating a binary for security is just a subset of the normal building and updating of a binary that is done daily.

If Rust wants to succeed in replacing C and C++ in typical big corp, cargo better support binary libraries eventually.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by gpm on Saturday February 20, 2021 @04:24PM (#26208131)
You must work in very different big corps than I, I have yet to see a binary .so we linked to but didn't have the source to.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Saturday February 20, 2021 @05:00PM (#26208381)
The kind of corps where Oracle and SQL Server licenses are tiny drops on project expenses, do Windows, macOS, iOS, Android and UNIX in general.

I am quite sure WebSphere, just to give an example, isn't releasing the source code for the .so that come along its Java implementation.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by jcelerier on Sunday February 21, 2021 @04:29AM (#26212115)
you live in a very shelled, blessed world (just looking at the fact that you said ".so", and not ".dll built with Visual Studio 2012").
Re:How Swift achieved dynamic linking where Rust couldn't (2... by kaishin on Saturday February 20, 2021 @02:06PM (#26206845)
Swift has unfortunately no choice but to solve this problem if it is to be used to build native software for Apple platforms.
Re:How Swift achieved dynamic linking where Rust couldn't (2... by randomNumber7 on Saturday February 20, 2021 @02:16PM (#26206923)
> By avoiding dynamic linking, I think, Rust positions itself best as the no-compromise, high performance, modern, systems programming language.

You can use static linking in c++ if you want to. There is 0 advantage in not having the option to use dynamic linking if you want to. It might be even more secure if e.g. the program links dynamically against a commonly used library and this library is updated regularly.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by a1369209993 on Saturday February 20, 2021 @06:55PM (#26209323)
> There is 0 advantage in not having the option to use dynamic linking if you want to.

The advantage is in other people (eg libc maintainers) not having the option to require you to use dynamic linking when you don't want to.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by RcouF1uZ4gsC on Saturday February 20, 2021 @02:30PM (#26207046)
> You can use static linking in c++ if you want to. There is 0 advantage in not having the option to use dynamic linking if you want to.

While you can do static linking in C++ if you want to, you still pay the cost for support for dynamic linking because of ABI issues. You can static link, but you still lost out on new language features and changes that break ABI and so you are still stuck with sub-optimal standard library code. For example, for years, until GCC 5, GCC std lib had a non-standard, slower copy on write implementation of std::string, that stuck around for so long because it would break ABI to change it. Even now, in MSVC STL, there are a lot of performance optimizations which the maintainer acknowledges, but will have to wait because they would break ABI. So even if you just statically link and continuously build in C++, you are still paying the price for support of dynamic linking.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by pjmlp on Saturday February 20, 2021 @03:07PM (#26207349)
Microsoft breaks the ABI all the time, this is why there is usually several MSVCrt.dll versions installed.

Visual Studio 2017 - 2019 were the first time they ever bothered with it, plus it is planned that VS vNext will break it again.

This not counting the other C++ compilers on Windows with their own standard libraries, like C++ Builder and Intel C++.

There is always COM/WinRT if I want an OOP ABI that doesn't break as easily.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by kortex on Saturday February 20, 2021 @02:53PM (#26207220)
I largely agree and personally hate working with dynamic linking, but ultimately it depends on your architecture model - to share memory, or not to share memory. If you've already decided you don't want to share memory, and you don't want to use system IPC, that leaves you with sockets (network/unix/fifo), which are all just byte streams. If you have byte streams, then you gotta use (de)serialization (serde) on either end, so effectively there is one ABI: {uint8[], size_t}. This is the microservices/docker/cloud model, and it's great. For many purposes.

But if you are dealing with OS level stuff, talking directly to hardware, in the embedded space, etc, now you care about different ABIs for performance reason. Now you decide, do I want static linking or dynamic linking? The only place I see dynamic linking really being a feature is when you have to link against the kernel, drivers, or proprietary subsystems (e.g. CUDA). IMHO, most userspace application layer stuff shouldn't be sharing memory, it should be communicating, again using byte streams and serde.

So that basically leaves the hardware/software interface: kernel APIs, drivers, etc. If you're in this space, great, it makes sense to use dynamic linking with very stable ABIs with no generic shenanigans, you don't need them. I think a lot of pain of DLL hell comes from using dynamic libs when you really should be statically compiling if it's a library, and IPC if you need to communicate.

Re:How Swift achieved dynamic linking where Rust couldn't (2... by ChrisMarshallNY on Saturday February 20, 2021 @02:36PM (#26207084)
If I use SPM, I seldom do dylibs. Since I wrote almost every package I consume, and the ones I didn't write are quite small; that works fine, for me.