News for hackers. Stuff that matters.
Re:How statically linked programs run on Linux by fiatmoney on Sunday November 17, 2013 @01:59PM (#6750248)
Is it actually practical to statically compile & link large parts of a modern C / C++ based software environment? The last time I tried to do that with a nontrivial C program and GCC it felt like an ultimately unsuccessful snipe hunt.

Incidentally, cross-compiling static binaries is a hugely productive feature of Go.

Re:How statically linked programs run on Linux by saljam on Sunday November 17, 2013 @03:32PM (#6750699)
Static linking is practically impossible on modern Unix because of bullet point 4 in nopaste7's linked page.

> all kinds of features in the libc (locale (through iconv), NSS, IDN, ...) require dynamic linking (...)

These are all features which have been shoehorned into libc on Linux, but only work when it's a shared library because they rely on shared state. I would argue a cleaner way of implementing them would have been as services, but alas, you have what you have.

Re:How statically linked programs run on Linux by caf on Sunday November 17, 2013 @09:26PM (#6752149)
It's not shared state - dynamic libraries don't share any more state between processes than static libraries do.

Those features use dynamic linking so that they can load only the necessary implementations at runtime - eg if you aren't using LDAP name lookups, then the NSS won't load the library that does LDAP name lookups.

Re:How statically linked programs run on Linux by amscanne on Sunday November 17, 2013 @10:07PM (#6752255)
> It's not shared state - dynamic libraries don't share any more state between processes than static libraries do.

The above sentence could be misconstrued.

I assume when you say static libraries, you mean standard shared libraries (i.e. those listed when you run ldd). Not statically-linked libraries?

Re:How statically linked programs run on Linux by caf on Sunday November 17, 2013 @11:30PM (#6752540)
Shared libraries on Linux only share read-only text and data, which should not be application-visible.

If you want to share state between processes, the library must explicitly set up a shared mapping - but of course a statically linked library can do this just as well.

Re:How statically linked programs run on Linux by amscanne on Monday November 18, 2013 @11:33PM (#6759288)
Yes, I know. My point was related to the terminology re: static, dynamic, shared, etc.

A statically-linked library won't share any state with other users of the library. It's statically-linked into the binary. State will be shared only with the instances of the same binary.

Re:How statically linked programs run on Linux by caf on Wednesday November 20, 2013 @07:12PM (#6772237)
It is irrelevant whether the library is statically-linked into the binary or not - a statically-linked library can share state with instances of that library linked into other binaries just as easily as a dynamically-linked library can. In both cases the library must explicitly set up some shared state.
Re:How statically linked programs run on Linux by johntb86 on Sunday November 17, 2013 @09:38PM (#6752166)
Hopefully Linux doesn't allow anything like Windows' shared data sections: http://msdn.microsoft.com/en-us/library/h90dkhs0(v=vs.90).as...
Re:How statically linked programs run on Linux by amscanne on Sunday November 17, 2013 @09:54PM (#6752210)
AFAIK there's no shared, writable section in ELF.

But of course you could still easily mmap() a file to achieve the same thing (even from the shared library .init functions).

I can understand that the shared section might be abused by goofy programmers ("Hey guys! We don't need to open a file for storage, we can just build it into the DDL!") ... but is there a reason you think it's a bad idea otherwise?

Re:How statically linked programs run on Linux by nopaste7 on Sunday November 17, 2013 @02:59PM (#6750536)
Re:How statically linked programs run on Linux by barrkel on Sunday November 17, 2013 @06:51PM (#6751620)
There's only one minor problem; DLL hell. As long as none of your applications break when you update their shared dependencies, everything's fine. The package management system generally takes care of this in modern distributions, and the package maintainers usually do enough coordination to avoid (much) breakage.

But if you're trying to produce commercial shippable software (what used to be called shrinkwrap), you're not going to fit in as easily with a package management system. Your binaries will probably need updating when the shared libraries update; that's a major headache. And this is one of the reasons why there hasn't been much of a commercial market in software for Linux. Everything generally works much more smoothly if your only dependencies are glibc and a handful of other bits.

Re:How statically linked programs run on Linux by amscanne on Sunday November 17, 2013 @10:25PM (#6752316)
Interesting list!

I was actually surprised that the VDSO [1] page was missing from that. For those unfamiliar... the Linux kernel selects the best system call implementation for the current system and exports a special page to user-space. It also provides a few specific additional routines (generally related to getting the current time without having to jump in and out of the kernel).

I'm not advocating for this approach, but it's there and can have a big impact. As a fun exercise, I just checked how much impact this has on my VM right now...

timetest.c:

    #include <stdio.h>
    #include <time.h>

    int main(void) {
        for (int i = 0; i < 1000000000; i++) {
            time(NULL);
        }
    }
gcc timetest.c -o timetest -std=c99 && time ./timetest

    real	0m3.589s
    user	0m3.588s
    sys 	0m0.000s
gcc timetest.c -o timetest -std=c99 -static && time ./timetest

    real	0m41.436s
    user	0m16.900s
    sys 	0m24.456s
[1] http://en.wikipedia.org/wiki/VDSO
Re:How statically linked programs run on Linux by MichaelMoser123 on Sunday November 17, 2013 @08:48PM (#6752044)
Of course there are exceptions: some people like to link in openssl libraries statically; that removes another attack vector - like swapping an openssl shared library with something that returns a constants for all random numbers;

On the other hand that is also a bit silly; if the box is owned then all else is peanuts;

Re:How statically linked programs run on Linux by nopaste7 on Sunday November 17, 2013 @03:34PM (#6750715)
BTW: cross compiling/deploying Go programs also works very good if the runtime is a shared lib. I'm doing so all time using gccgo.