LDFLAGS="-Wl,--as-needed" support

(cherry picked from commit ef9f7a59c51a8539375b1ca21b41765fca3f1019)