Skip to content

Commit

Permalink
Implemented R_ARM_ABS32 relocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
AeonLucid committed Jul 9, 2018
1 parent 544f292 commit 146b5e3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# AndroidNativeEmu

Allows you to partly emulate an Android native library.

This is an educational project to learn more about the ELF file format and [Unicorn](https://github.com/unicorn-engine/unicorn).

## Resources

Expand All @@ -9,6 +11,8 @@ All resources used while developing AndroidNativeEmu.
### Text sources
- https://greek0.net/elf.html
- https://stackoverflow.com/questions/13908276/loading-elf-file-in-c-in-user-space
- https://programtalk.com/python-examples/pyelftools.elftools.elf.relocation.Relocation/
- http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044f/IHI0044F_aaelf.pdf

### Code sources
- https://github.com/lunixbochs/usercorn
6 changes: 6 additions & 0 deletions androidemu/internal/arm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# From http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044f/IHI0044F_aaelf.pdf

R_ARM_ABS32 = 2
R_ARM_GLOB_DAT = 21
R_ARM_JUMP_SLOT = 22
R_ARM_RELATIVE = 23
38 changes: 33 additions & 5 deletions androidemu/internal/modules.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from elftools.elf.elffile import ELFFile
from elftools.elf.relocation import RelocationSection
from unicorn import UC_PROT_ALL

from androidemu.internal import get_segment_protection
from androidemu.internal import get_segment_protection, arm


class Module:
Expand Down Expand Up @@ -38,10 +39,6 @@ def load_module(self, filename, main=True):
if not dynamic:
raise NotImplementedError("Only ET_DYN is supported at the moment.")

# Parse section header (Linking view).
# for section in elf.iter_sections():
# print(section.name + " " + section.header.sh_type)

# Parse program header (Execution view).

# - LOAD (determinate what parts of the ELF file get mapped into memory)
Expand Down Expand Up @@ -80,3 +77,34 @@ def load_module(self, filename, main=True):
self.module_main = module
else:
self.modules.append(module)

# Parse section header (Linking view).
dynsym = elf.get_section_by_name(".dynsym")
dynstr = elf.get_section_by_name(".dynstr")

# Relocate.
for section in elf.iter_sections():
if not isinstance(section, RelocationSection):
continue

for rel in section.iter_relocations():
sym = dynsym.get_symbol(rel['r_info_sym'])
sym_value = sym['st_value']

rel_addr = load_base + rel['r_offset'] # Location where relocation should happen

# Relocation table for ARM
if rel.entry.r_info_type == arm.R_ARM_ABS32: # Static | Data | Op: (S + A) | T
# Create the new value.
value = load_base + sym_value

# Write the new value
self.emu.mu.mem_write(rel_addr, value.to_bytes(4, byteorder='little'))
elif rel.entry.r_info_type == arm.R_ARM_GLOB_DAT: # Dyn | Data | Op: (S + A) | T
pass
elif rel.entry.r_info_type == arm.R_ARM_JUMP_SLOT: # Dyn | Data | Op: (S + A) | T
pass
elif rel.entry.r_info_type == arm.R_ARM_RELATIVE: # Dyn | Data | Op: B(S) + A[Note: see Table 4-18]
pass
else:
print("Unhandled relocation type %i." % rel.entry.r_info_type)

0 comments on commit 146b5e3

Please sign in to comment.