Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell, with something like objdump, if an object file has been built with -fPIC?

How can I tell, with something like objdump, if an object file has been built with -fPIC?

like image 261
Crazy Chenz Avatar asked Aug 27 '09 11:08

Crazy Chenz


People also ask

How do I find the symbol in an .O file?

If so, you need to use the respective nm or objdump commmand. For example, if you have used XXX-YYY-gcc to compile the .o file, you need to use XXX-YYY-nm or XXX-YYY-objdump to process the files. Show activity on this post.

What is objdump used for?

objdump displays information about one or more object files. The options control what particular information to display. This information is mostly useful to programmers who are working on the compilation tools, as opposed to programmers who just want their program to compile and work.

What is the object file created by?

Created by the assembler and link editor, object files are binary representations of programs intended to be executed directly on a processor. Programs that require other abstract machines, such as shell scripts, are excluded.

How can I see the code of an object in Linux?

Check out the binutils package. In it you'll find objdump - display information from object files. Then try running objdump -d xyz.o .


2 Answers

The answer depends on the platform. On most platforms, if output from

readelf --relocs foo.o | egrep '(GOT|PLT|JU?MP_SLOT)' 

is empty, then either foo.o was not compiled with -fPIC, or foo.o doesn't contain any code where -fPIC matters.

like image 59
Employed Russian Avatar answered Sep 23 '22 01:09

Employed Russian


I just had to do this on a PowerPC target to find which shared object (.so) was being built without -fPIC. What I did was run readelf -d libMyLib1.so and look for TEXTREL. If you see TEXTREL, one or more source files that make up your .so were not built with -fPIC. You can substitute readelf with elfdump if necessary.

E.g.,

[user@host lib]$ readelf -d libMyLib1.so | grep TEXT   # Bad, not -fPIC  0x00000016 (TEXTREL) [user@host lib]$ readelf -d libMyLib2.so | grep TEXT   # Good, -fPIC [user@host lib]$ 

And to help people searching for solutions, the error I was getting when I ran my executable was this:

root@target:/# ./program: error while loading shared libraries: /usr/lib/libMyLi b1.so:  R_PPC_REL24 relocation at 0x0fc5987c for symbol 'memcpy' out of range 

I don't know whether this info applies to all architectures.

Source: blogs.oracle.com/rie

like image 36
indiv Avatar answered Sep 21 '22 01:09

indiv