Subject: bin/13027: dlsym can't find symbols in the executable itself
To: None <gnats-bugs@gnats.netbsd.org>
From: Johan Danielsson <joda@pdc.kth.se>
List: netbsd-bugs
Date: 05/24/2001 12:46:37
>Number:         13027
>Category:       bin
>Synopsis:       dlsym can't find symbols in the executable itself
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu May 24 03:46:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Johan Danielsson
>Release:        2001-05-01
>Organization:
>Environment:
>Description:

If you try go get to symbols in the running application you only get
NULL back.

>How-To-Repeat:

This program fails to find `main' on on 1.5/-current i386+alpha ELF
systems, but works on 1.4/sparc/a.out, as well as most other systems
such as IRIX and DEC UNIX. Finding something that lives in a library,
such as printf does work.

#include <stdio.h>
#include <dlfcn.h>

int
main(int argc, char **argv)
{
    void *handle, *ptr;
    char symbol[32], *sym;
    handle = dlopen(NULL, RTLD_GLOBAL|RTLD_NOW);
    if(handle == NULL) {
	printf("%s\n", dlerror());
	exit(1);
    }
    snprintf(symbol, sizeof(symbol), "_%s", argv[1]);
    sym = symbol + 1;
    ptr = dlsym(handle, sym);
    if(ptr == NULL) 
	ptr = dlsym(handle, --sym);
    if(ptr == NULL) {
	printf("%s\n", dlerror());
	exit(1);
    }
    printf("%s = %p\n", sym, ptr);
    exit(0);
}

$ ./a.out main
Undefined symbol "_main"
$ ./a.out printf
printf = 0x480ca74c

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted: