Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading GNU ld script with dlopen

Tags:

c++

gnu

ld

I have an C++14 code that should load an arbitrary shared object file with dlopen. Unfortunately on some systems (e.g. my archlinux, reportedly also applies to some .so on ubuntu and gentoo), these so-files can be "GNU ld scripts" instead of the actual binaries.

For reference, here is the content of my /usr/lib/libm.so:

/* GNU ld script
*/
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /usr/lib/libm.so.6  AS_NEEDED ( /usr/lib/libmvec.so.1 ) )

I have found a couple of code-pieces that deal with this issue in ghc or ruby. I would like to avoid resorting to manually parsing the text file based parsing the dlerror text and the file. I feel that is terribly evil and I won't be able to implement and maintain corner cases of this format.

Is there a clean way to implement handling this case? Frankly I am puzzled as to why dlopen does not actually handle these tranparaently.

Note: Considering the aforementioned patches I think this is not simply an issue with my system configuration / versions. If this should work out-of-the-box with dlopen (bug instead of missing feature), please let me know.

like image 448
Zulan Avatar asked Jan 19 '16 14:01

Zulan


1 Answers

The linker scripts are intended to be used by the linker, not the run-time linker.

The GNU ld script comment should have been a giveaway: this is for ld, not for ld.so. ;-)

See for instance: http://www.math.utah.edu/docs/info/ld_3.html

So I guess using this with dlopen() would mean mimicking/importing part of ld's magic for this, which would confirm your fears about resorting to manually parsing the text and maintaining terribly evil code.

EDIT: There seems to be one thing that can help you though:

https://www.sourceware.org/ml/libc-alpha/2011-07/msg00152.html

<gnu/lib-names.h> should contain a define LIBM_SO which should point you to the correct file that you can actually dlopen().

That means that normally no evil code would be necessary.

like image 126
marcolz Avatar answered Oct 23 '22 03:10

marcolz