Subject: bin/13027: dlsym can't find symbols in the executable itself
To: None <>
From: Johan Danielsson <>
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
>Originator:     Johan Danielsson
>Release:        2001-05-01

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


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>

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());
    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());
    printf("%s = %p\n", sym, ptr);

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