ELF relocation in shared object causes segmentation fault

0

I created sectionless 32 bit ARM ELF executable file from scratch in hex editor that calls two functions from glibc: printf to display "hello world" and exit to terminate. I had to create two REL relocation entries for those symbols that contain r_offset field. Specification https://refspecs.linuxbase.org/elf/gabi4+/ch4.reloc.html describes this field as:

r_offset
This member gives the location at which to apply the relocation action. For a relocatable file, the value is the byte offset from the beginning of the section to the storage unit affected by the relocation. For an executable file or a shared object, the value is the virtual address of the storage unit affected by the relocation.

When I create EXEC (Executable file) ELF I know the virtual address to which relocation should apply and I can run executable. By changing ELF type to DYN (Shared object file) (position independent executable) I get segmentation fault because relocations are applied after loading executable to random virtual address and therefore r_offset points to memory area that cannot be written to.

How to create correct relocation entries for DYN type ELF?

My DYNAMIC segment has 10 entries:

0x00000001 (NEEDED) Shared library: [libc.so.6]<br>
0x00000004 (HASH) 0x11134<br>
0x00000005 (STRTAB) 0x110cc<br>
0x00000006 (SYMTAB) 0x11164<br>
0x0000000a (STRSZ) 23 (bytes)<br>
0x0000000b (SYMENT) 16 (bytes)<br>
0x00000011 (REL) 0x11194<br>
0x00000012 (RELSZ) 16 (bytes)<br>
0x00000013 (RELENT) 8 (bytes)<br>
0x00000000 (NULL) 0x0
linux
elf
asked on Stack Overflow Nov 7, 2019 by pajacol • edited Nov 8, 2019 by sungtm

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0