portability: only link to -ldl if needed

We use -ldl in some of our testcases to mock out some system calls via
interposing, using dlsym() to chain up.

Make sure we only use this if we really need to.  Many systems have
dlsym() in the C library and no -ldl.

https://bugzilla.gnome.org/show_bug.cgi?id=720072