This is my proof-of-concept on making --copy-dt-needed-entries work. Apparently, i've been able to hack on it hard enough to make it actually emit DT_NEEDED for all symbols, recursively:
$ clang -fuse-ld=lld -Wl,--copy-dt-needed-entries test.c -lgit2 $ readelf -d a.out Dynamic section at offset 0x3010 contains 40 entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libgit2.so.27] 0x0000000000000001 (NEEDED) Shared library: [librt.so.1] 0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0] 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x0000000000000001 (NEEDED) Shared library: [ld-linux-x86-64.so.2] 0x0000000000000001 (NEEDED) Shared library: [libcurl.so.4] 0x0000000000000001 (NEEDED) Shared library: [libcares.so.2] 0x0000000000000001 (NEEDED) Shared library: [libnghttp2.so.14] 0x0000000000000001 (NEEDED) Shared library: [libidn2.so.4] 0x0000000000000001 (NEEDED) Shared library: [libunistring.so.2] 0x0000000000000001 (NEEDED) Shared library: [libssl.so.1.1] 0x0000000000000001 (NEEDED) Shared library: [libcrypto.so.1.1] 0x0000000000000001 (NEEDED) Shared library: [libz.so.1] 0x0000000000000001 (NEEDED) Shared library: [libdl.so.2] 0x0000000000000001 (NEEDED) Shared library: [libhttp_parser.so.2.9] 0x0000000000000001 (NEEDED) Shared library: [libssh2.so.1] 0x0000000000000015 (DEBUG) 0x0 0x0000000000000007 (RELA) 0x200620 0x0000000000000008 (RELASZ) 48 (bytes) 0x0000000000000009 (RELAENT) 24 (bytes) 0x0000000000000017 (JMPREL) 0x200650 0x0000000000000002 (PLTRELSZ) 72 (bytes) 0x0000000000000003 (PLTGOT) 0x202010 0x0000000000000014 (PLTREL) RELA 0x0000000000000006 (SYMTAB) 0x2002b0 0x000000000000000b (SYMENT) 24 (bytes) 0x0000000000000005 (STRTAB) 0x200488 0x000000000000000a (STRSZ) 404 (bytes) 0x000000006ffffef5 (GNU_HASH) 0x200400 0x0000000000000004 (HASH) 0x200428 0x0000000000000019 (INIT_ARRAY) 0x203008 0x000000000000001b (INIT_ARRAYSZ) 8 (bytes) 0x000000000000001a (FINI_ARRAY) 0x203000 0x000000000000001c (FINI_ARRAYSZ) 8 (bytes) 0x000000000000000c (INIT) 0x201200 0x000000000000000d (FINI) 0x201218 0x000000006ffffff0 (VERSYM) 0x2003b8 0x000000006ffffffe (VERNEED) 0x2003d0 0x000000006fffffff (VERNEEDNUM) 1 0x0000000000000000 (NULL) 0x0
Can't say the code is nice but before I try to make it cleanly, I'd appreciate some tips on whether I'm even starting in the right direction.
FWICS, GNU ld's --copy-dt-needed-entries adds lot less libraries — it seems as if it implicitly forced --as-needed, and --no-as-needed doesn't seem to make a difference.