Bug 12939 - resolving shared library symbols returns wrong file
Summary: resolving shared library symbols returns wrong file
Status: NEW
Alias: None
Product: glibc
Classification: Unclassified
Component: dynamic-link (show other bugs)
Version: 2.14
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-27 16:51 UTC by Andi Kleen
Modified: 2015-01-12 20:27 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments
dladdr arm vs x86_64 different behavior example (149 bytes, text/x-c++src)
2014-07-02 15:38 UTC, Alex Long
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andi Kleen 2011-06-27 16:51:42 UTC
dladdr() when called with a shared library symbol like printf
returns the name of the main executable in dli_fname

I suspect it happens because it resolves the PLT, but that's clearly
unexpected and makes it hard to use if you want to know
what .so printf is in. Is there a way around it?

Maybe the function could check for PLTs and resolve them?
Comment 1 Alex Long 2014-07-02 15:38:23 UTC
Created attachment 7675 [details]
dladdr arm vs x86_64 different behavior example

compiled with
g++ main.cpp -o main -Wall -Wextra -ldl -fPIC
Comment 2 Alex Long 2014-07-02 15:39:15 UTC
As suggested in the dladdr() man-page, compiling your code to be position independent, using -fPIC for example, may cause dladdr to return the path to the shared library the symbol comes from rather than return the path to the executable. 

However, one issue with this that I've noticed is that when building on or cross-compiling for armv7l, dladdr will always return the path to the executable for exported symbols that also happen to be called within the program.

When compiled with 
/usr/bin/g++-4.7 main.cpp -o main-ARM -Wall -Wextra -ldl -fPIC

the attached example program prints
"printf comes from /lib/x86_64-linux-gnu/libc.so.6" on x86_64 machines
and
"printf comes from ./main" on armv7l