Subject: dlopen(0, 1) should work?
To: None <current-users@NetBSD.ORG>
From: Simon J. Gerraty <sjg@quick.com.au>
List: current-users
Date: 03/02/1998 17:12:09
I'll send-pr this - but first I want a sanity check :-)

I'm fiddling with doing a native port of Sun's JDK based on the latest
patches from the FreeBSD java team (thanks guys).

In their current patches they use a function which calls dlopen(0,1),
to get a handle on the currently mapped shared objects - so they can
find the path of a mapped library (various uses).

Looking at /usr/src/gnu/usr.bin/ld/rtld/rtld.c (v1.51), I see
__dlopen() says:

        /*
         * A NULL argument returns the current set of mapped objects.
         */
        if (name == NULL) {
                LM_PRIVATE(link_map_head)->spd_refcount++;
                /* Return magic token */
                return (void *)1;
        }

the comment matches the goal, but I can't find where a token of 1, as
the return will result in anything but a seg fault - which is what I
get:

Program terminated with signal 11, Segmentation fault.
Reading symbols from /usr/libexec/ld.so...done.
Reading symbols from /usr/lib/libc.so.12.20...done.
#0  0x1812 in dlfname (addr=0x208c) at tit.c:15
15              if (addr >= (void *) so_map->som_addr
(gdb) l
10      {
11          struct so_map *so_map;
12      
13          so_map = (struct so_map *)dlopen(0, RTLD_LAZY);
14          for (; so_map; so_map = so_map->som_next) {
15              if (addr >= (void *) so_map->som_addr
16                  && (so_map->som_next == 0
17                      || addr < (void *)
so_map->som_next->som_addr)) {
18                  break;
19              }
(gdb) p so_map
$1 = (struct so_map *) 0x1
(gdb)