Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

distinguish shared objects from position independent executables

Tags:

elf

I'm looking for a fast way to check if a ELF binary is a shared object or a position independent executable. I think a can do that by checking the contained symbols / functions. I'm looking for a more efficient way of not having to read the complete file. I have to perform the check on different platforms, at least Android, Linux (32 and 64 bit).

like image 375
Uhli Avatar asked Apr 30 '13 14:04

Uhli


People also ask

How does position-independent executable Work?

Position Independent Executables (PIE) are an output of the hardened package build process. A PIE binary and all of its dependencies are loaded into random locations within virtual memory each time the application is executed. This makes Return Oriented Programming (ROP) attacks much more difficult to execute reliably.

How do I know if an EXE is pie?

There is no way to check: a PIE executable is a shared object.

What is PIC and pie?

In computing, position-independent code (PIC) or position-independent executable (PIE) is a body of machine code that, being placed somewhere in the primary memory, executes properly regardless of its absolute address.

What is a shared object?

A shared object is an indivisible unit that is generated from one or more relocatable objects. Shared objects can be bound with dynamic executables to form a runable process. As their name implies, shared objects can be shared by more than one application.


1 Answers

I'm looking for a fast way to check if a ELF binary is a shared object or a position independend executable.

There is no way to check: a PIE executable is a shared object.

I think a can do that by checking the contained symbols / functions.

Symbols can be stripped, and once they are, you can't tell.

shared objects and executables they normally differ by the linked startup code

That's true: the PIE is normally linked with Scrt1.o, but a shared library is normally not. But there is nothing to prevent a shared library to be linked with Scrt1.o as well, and in a stripped binary even finding that startup code may be somewhat problematic.

If what you really want is to distinguish between a shared library and a PIE executable which you built yourself (rather than solving a general case of any shared library and any PIE), then checking for presence of PT_INTERP (readelf -l a.out | grep INTERP) is likely the easiest way to go: a PIE executable is guaranteed to have PT_INTERP, and shared libraries normally don't have it (libc.so.6 is a notable exception).

like image 180
Employed Russian Avatar answered Oct 04 '22 04:10

Employed Russian