4 ms·
Why is static linking supposed to REDUCE memory usage and binary size? Am I missing something really obvious? Eg, if five different binaries are statically lin
by scott_karana 10y ago
Why is static linking supposed to REDUCE memory usage and binary size? Am I missing something really obvious?
Eg, if five different binaries are statically linked against the same version of OpenSSL, won't it be in memory and on disk four times more than it would be on a dynamic system?
- marmaduke 10y agoIf you took the "one tool one purpose" thing to an extreme, you'd only have one binary with OpenSSL compiled in, right? Given some familiarity with the suckless ideology, it seems that it is the simplicity of static linking, not conservative use of disk space, which is its virtue. For, if anyone groks the tool chain perfectly but lacks disk space, I'd like to hear the secret.
- Retr0spectrum 10y ago> you'd only have one binary with OpenSSL compiled in So then any process that wanted to use crypto etc. would have to call this binary? That just seems like a very inefficient way if implementing dymanic linking...
- JdeBP 10y agoYou have the wrong metric. The metric should be the efficiency of the mechanism in implementing SSL. Now look at things like Gerrit Pape's sslio , http://smarden.org/ipsvd/sslio.8.html http://smarden.org/ipsvd/sslio.8.html . Note that the subject at hand purportedly uses several of Gerrit Pape's other tools.
- qznc 10y agoMany applications only use parts of libraries, then the linker can throw the unnecessary parts away.
- infinity0 10y agoHow do I get it to do that? $ cat test.c #include <stdio.h> int main() { printf("Hello, World!\n"); } $ gcc -o test1 test.c && ls -lgG test1 -rwxr-xr-x 1 6712 Sep 28 10:24 test1* $ gcc -static -o test1 test.c && ls -lgG test1 -rwxr-xr-x 1 800904 Sep 28 10:24 test1*
- ptman 10y agoprobably by using musl instead of glibc
- ericbb 10y agoI always use dynamic linking but I thought it'd be fun to try making a small, static hello-world. The following assumes x86-64 Linux (and is a total hack that "works for me" NO WARRANTY!). $ cat test.c static int sys_write(int fd, const void *buf, unsigned long n) { asm("mov $1,%rax; syscall"); } static void sys_exit(int status) { asm("mov $60,%rax; syscall"); } void _start(void) { char s[] = "Hello, World\n"; int r = sys_write(1, s, sizeof(s) - 1); sys_exit(r == -1 ? 1 : 0); } $ gcc -nostdlib -static -o test test.c && ls -lgG test -rwxr-xr-x 1 1480 Sep 28 11:47 test More to the point of your question, if you want to see the effect you're looking for, I think you need to link to a libfoo.a library that includes some module.o that your program doesn't use.
- ericbb 10y agoI like this one even better: $ cat test.c void _start(void) { __asm__( "movabsq $6278066737626506568,%rax\n\t" "movq %rax,-32(%rsp)\n\t" "movl $1684828783,-24(%rsp)\n\t" "movw $10,-20(%rsp)\n\t" "movq $1,%rax\n\t" "movq $1,%rdi\n\t" "leaq -32(%rsp),%rsi\n\t" "movq $13,%rdx\n\t" "syscall\n\t" "movq $60,%rax\n\t" "movq $0,%rdi\n\t" "syscall"); } $ gcc -nostdlib -static -Os -o test test.c && strip test && ls -lgG test -rwxr-xr-x 1 864 Sep 28 14:22 test 864 bytes! :)
- 10y ago