Inspecting What is inside executable or library

Sometimes, it is necessary to know what dynamic libraries an executable depends on and what functions are defined in particular library, etc. In this blog, some tools which aid understanding the content of executable / library is discussed.

size

Size is a diagnostic tool for executable file analysis. This utility displays the total size for each object file. The running program of the computer is called a process. Each process of the computer allocates some memory from RAM known as process memory. The process memory of the computer is divided into different blocks segment such as code, data, heap and stack.

Ex.

int x=100; // data

int y; // bss

void main()

{

static int a=10; // data

static int b; // bss

In the above program x and y are external variables where as a and b are static variable. All external and initialize static and external variable are created in Data Segment but uninitialized static and external variables are created in bss (uninitialized data segment) segment. To view the memory allocation by each of these segment we can use the size command.

$ size ./a.out

output:

data bss dec hex filename

252 8 1319 527 a.out

You can type the object files name to be examined. If none are specified, the file "a.out" will be used.

options

-d The total size is always displayed in decimal format.

-o The total size is always displayed in octal format.

$ size -x ./a.out

text data bss dec hex filename

0x3b7 0x100 0x10 1223 4c7 ./a.out

$ size -o ./a.out

text data bss oct hex filename

01667 0400 020 2307 4c7 ./a.out

ldd

ldd is a diagnostic tool for executable file analysis. It shows which shared libraries an executable would use in your environment. A C program is nothing but collection of some functions. Every function is defined in a library file (static/dynamic library). When a program is loaded along with that the respective dynamic libraries are loaded. To enhance yourself with the dynamic libraries used in your program you are equipped with a development tool called ldd. It gives you brief details about which dynamic library is loaded & which dynamic library is missing in your program.

Ex.

main()

{

printf(“Hello”);

bbsr();

Here main.c is the source file. On compiling generates the object file ( main.o). Now the object file is linked with the dynamic libraries (libc.so, sample.so) to build the executable file(singo).

For Example:

$ldd ./singo

Output :

linux-gate.so.1 => (0x00110000)

sample.so=> not found

libc.so.6 => /lib/libc.so.6 (0x00367000)

/lib/ld-linux.so.2 (0x00348000) 

nm

This command list symbol names from object file. These symbol names can be either functions, global variables or static variables. For each symbol the value, symbol type & symbol names are displayed. Lower case symbol types means the symbol is local , upper case means the symbol is global.

Options:

-S print size of undefined symbols

-D Print dynamic, not normal, symbols. Useful only when working with dynamic objects (for example some kinds of shared libraries).

-l For each symbol, use debugging information to try to find a file name and line number. For a defined symbol, look for the line number of the address of the symbol. For an undefined symbol, look for the line number of a relocation entry which refers to the symbol. If line number information can be found, print it after the other symbol information.

Ex.

//prg.c

main()

{

bbsr();

printf(“Hello”);

ctc();

}

int ctc()

{

printf(“I am incuttack”);

}

$ gcc -c prg.c

$nm prg.o

output:

U bbsr

00000030 T ctc

00000000 T main

U printf

Here in the above output printf() & bbsr() are undefined.

$ nm -D -l -S ./a.out

080484b8 00000004 R _IO_stdin_used

w __gmon_start__

U __libc_start_main

U printf 

objdump

It displays information about object files.

This command is used to disassemble shared objects & libraries. It locates the method in which the problem originates.

Options:

-d Display the assembler mnemonics for the machine instructions from object file. This option disassembles only those sections that are expected to contain instructions.

-s Display the full contents of any section requested.

-h Display summary information from the section headers of the object file.

using objdump -h to list the file section headers can’t show the correct addresses. Instead, it shows the usual addresses, which are implicit for the target.

Example

//prg1.c

main()

{

static int x=12;

static int y;

printf(“%d”,x);

bbsr();

}

int bbsr()

{

printf(“Hi”);

}

$objdump -h ./a.out

output:

./a.out: file format elf32-i386

Sections:

Idx Name Size VMA LMA File off Algn

0 .interp 00000013 08048134 08048134 00000134 2**0

CONTENTS, ALLOC, LOAD, READONLY, DATA

1 .note.ABI-tag 00000020 08048148 08048148 00000148 2**2

CONTENTS, ALLOC, LOAD, READONLY, DATA

2 .note.gnu.build-id 00000024 08048168 08048168 00000168 2**2

CONTENTS, ALLOC, LOAD, READONLY, DATA

$ objdump -s -j .rowdata ./a.out

output:

a.out: file format elf32-i386

$ objdump -d -r -j .text ./a.out

output:

./a.out: file format elf32-i386

Disassembly of section .text:

080482f0 <_start>:

80482f0: 31 ed xor %ebp,%ebp

80482f2: 5e pop %esi

80482f3: 89 e1 mov %esp,%ecx

80482f5: 83 e4 f0 and $0xfffffff0,%esp

80482f8: 50 push %eax

The above output is the disassembly of text section with the -d flag. This option disassembles the section which are expected to contains the instructions.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s