bin/134391: dladdr(3) does effectively not work on main program.

Poul-Henning Kamp phk at
Sat May 9 15:20:02 UTC 2009

>Number:         134391
>Category:       bin
>Synopsis:       dladdr(3) does effectively not work on main program.
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat May 09 15:20:01 UTC 2009
>Originator:     Poul-Henning Kamp
>Release:        FreeBSD 8.0-CURRENT i386
System: FreeBSD 8.0-CURRENT FreeBSD 8.0-CURRENT #1 r191911: Fri May 8 09:42:35 UTC 2009 root at i386

	The dladdr(3) function does not return anything remotely
	useful for the main program, because it only examines the
	dynamic symbol table.

	The most common use for this function, is to construct a

	ports/devel/libexecinfo is an example of this use.

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

	extern void foo(const void *a);
	static void bar(void *a);

	main(int argc, char **argv)


		foo((const char *)bar + 4);
		return (0);

	void foo(const void *a)
		Dl_info info;
		int i;

		i = dladdr(a, &info);

		printf("i = %d (%p)\n", i, a);
		printf("fn = %s\n", info.dli_fname);
		printf("fb = %p\n", info.dli_fbase);
		printf("sn = %s\n", info.dli_sname);
		printf("sb = %p\n", info.dli_saddr);

	static void bar(void *b)
		printf("%p\n", b);


	Linking the program with `-Wl,--export-dynamic fills the
	dynamic symbol table with all non-static symbols, which is
	a bit better, but still useless because all static functions
	are invisible.

	A simple correct solution escapes me, since we might not
	even be able to open the original binary again to read the
	DWARF symbol table.

	One, quasi-hackish way to solve this, is to notice that the
	program references dladdr(3) in the first place, and have rtld(1)
	load the DWARF-symbol table(s) also.

More information about the freebsd-bugs mailing list