Jan 22, 2014

[ELF]

Reference:
load-time-relocation-of-shared-libraries

load-time relocation

gcc -g -c ml_main.c -o ml_mainreloc.o
gcc -shared -o libmlreloc.so ml_mainreloc.o
$ readelf -h libmlreloc.so

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  [...] some header fields
  Entry point address:               0x3b0
  [...] some header fields
$ objdump -d -Mintel libmlreloc.so

libmlreloc.so:     file format elf32-i386

[...] skipping stuff

0000046c :
 46c: 55                      push   ebp
 46d: 89 e5                   mov    ebp,esp
 46f: a1 00 00 00 00          mov    eax,ds:0x0
 474: 03 45 08                add    eax,DWORD PTR [ebp+0x8]
 477: a3 00 00 00 00          mov    ds:0x0,eax
 47c: a1 00 00 00 00          mov    eax,ds:0x0
 481: 03 45 0c                add    eax,DWORD PTR [ebp+0xc]
 484: 5d                      pop    ebp
 485: c3                      ret

[...] skipping stuff
$ readelf -r libmlreloc.so

Relocation section '.rel.dyn' at offset 0x2fc contains 7 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00002008  00000008 R_386_RELATIVE
00000470  00000401 R_386_32          0000200C   myglob
00000478  00000401 R_386_32          0000200C   myglob
0000047d  00000401 R_386_32          0000200C   myglob
[...] skipping stuff
Replace 00 with R_386_32's content

R_386_32 contains the address of myglob
(which is the start location of this .so [Entry point address] + the offset 0000200C of myglob)

00000470 is the offset to 46f: a1 00 00 00 00 , a1's operand.

00000478 is the offset to 477: a3 00 00 00 00 , a3's operand.

and so on~

Can also see this through
$ nm libmlreloc.so
[...] skipping stuff
0000200c D myglob

D means in the initialized data section (.data).

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.