% cat so_init.c #include <stdio.h> void so_init(int argc, char** argv, char** env) { for (int i = 0; i < argc; i++) { printf("arg[%d]: %s\n", i, argv[i]); } } % cc -std=c99 -fPIC -shared -Wl,-init=so_init so_init.c -o test.so % env LD_PRELOAD=$PWD/test.so true 1 2 3 Bus error % /compat/linux/bin/cc -std=c99 -fPIC -shared -Wl,-init=so_init so_init.c -o test.so % env LD_PRELOAD=$PWD/test.so /compat/linux/bin/true 1 2 3 arg[0]: /compat/linux/bin/true arg[1]: 1 arg[2]: 2 arg[3]: 3 Some notes there: 1. relevant glibc code: https://sourceware.org/git/?p=glibc.git;a=blob;f=elf/dl-init.c;h=3e72fa3013a6aaeda05fe61a0ae7af5d46640826;hb=HEAD#l58 ; 2. rtld already does this for functions referenced in DT_INIT_ARRAY, so it's enough to replace a call_initfini_pointer call with call_init_pointer.
Doesn't this have unpleasant security implications?
(In reply to Bob Bishop from comment #1) I'm not aware of any security implications.
https://reviews.freebsd.org/D26351
A commit references this bug: Author: kib Date: Mon Sep 7 21:32:28 UTC 2020 New revision: 365432 URL: https://svnweb.freebsd.org/changeset/base/365432 Log: rtld: pass argc/argv/env to dso inits. This is consistent with how array inits are called, and also makes us more compatible with glibc environment. Requested by: Alex S <iwtcex@gmail.com> PR: 249162 Reviewed by: dim, emaste Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D26351 Changes: head/libexec/rtld-elf/rtld.c
(In reply to commit-hook from comment #4) Thank you, that should be quite useful.
A commit references this bug: Author: kib Date: Mon Sep 14 10:49:39 UTC 2020 New revision: 365714 URL: https://svnweb.freebsd.org/changeset/base/365714 Log: MFC r365432: rtld: pass argc/argv/env to dso inits. PR: 249162 Changes: _U stable/12/ stable/12/libexec/rtld-elf/rtld.c
A commit references this bug: Author: kib Date: Mon Sep 14 11:02:41 UTC 2020 New revision: 365716 URL: https://svnweb.freebsd.org/changeset/base/365716 Log: MFC r365432: rtld: pass argc/argv/env to dso inits. PR: 249162 Changes: _U stable/11/ stable/11/libexec/rtld-elf/rtld.c