improve error handling in map_library and support long phdrs

previously, errno could be meaningless when the caller wrote it to the
dlerror string or stderr. try to make it meaningful. also, fix
incorrect check for over-long program headers and instead actually
support them by allocating memory if needed.
if map_library has allocated a buffer for phdrs, free it on success too

this fixes an oversight in the previous commit.