Compare commits

..

No commits in common. "83ce6ce0e7f6bdf518f999351e3c877be0f55e84" and "58120f292c7b4ee3055b90a84b1083176b8c27ac" have entirely different histories.

2 changed files with 28 additions and 27 deletions

View File

@ -80,20 +80,11 @@ def get_return_function_type(symbol: pelfy.elf_symbol) -> str:
def get_stencil_position(func: pelfy.elf_symbol) -> tuple[int, int]:
start_index = 0 # There must be no prolog
# Find last relocation in function
last_instr = get_last_call_in_function(func)
assert func.section, f"No code section specified for symbol {func.name}"
# func.section.fields['sh_size'] is equivalent to func.fields['st_size']
# expect for ARM thumb, here nop padding at the end for 4-byte alignment
# is not included in st_size
function_size = func.section.fields['sh_size']
# Check if jump is the last instruction and can be striped
if last_instr + 5 >= function_size:
end_index = last_instr
function_size = func.fields['st_size']
if last_instr + 5 >= function_size: # Check if jump is last instruction
end_index = last_instr # Jump can be striped
else:
end_index = function_size
@ -107,12 +98,11 @@ def get_last_call_in_function(func: pelfy.elf_symbol) -> int:
if reloc.symbol.name.startswith('dummy_'):
return -0xFFFF # Last relocation is not a jump
else:
# Assume the jump/call instruction is 4 bytes long for relocations
# with less than 32 bit and 5 bytes otherwise
# Assume the call instruction is 4 bytes long for relocations with less than 32 bit and 5 bytes otherwise
instruction_lengths = 4 if reloc.bits < 32 else 5
address_field_length = 4
#print(f"-> {[r.fields['r_offset'] - func.fields['st_value'] for r in func.relocations]}")
return reloc.fields['r_offset'] - func.offset_in_section + address_field_length - instruction_lengths
return reloc.fields['r_offset'] - func.fields['st_value'] + address_field_length - instruction_lengths
def get_op_after_last_call_in_function(func: pelfy.elf_symbol) -> int:
@ -120,7 +110,7 @@ def get_op_after_last_call_in_function(func: pelfy.elf_symbol) -> int:
assert func.relocations, f'No call function in stencil function {func.name}.'
reloc = func.relocations[-1]
assert reloc.bits <= 32, "Relocation segment might be larger then 32 bit"
return reloc.fields['r_offset'] - func.offset_in_section + 4
return reloc.fields['r_offset'] - func.fields['st_value'] + 4
class stencil_database():
@ -131,7 +121,7 @@ class stencil_database():
var_size (dict[str, int]): dictionary of object names and their sizes
byteorder (ByteOrder): byte order of the ELF file
elf (elf_file): the loaded ELF file
thumb_mode (bool): entry_function_shell in ARM thumb mode
thumb_mode (int): 1 if ARM in thumb mode, 0 otherwise
"""
def __init__(self, obj_file: str | bytes):
@ -158,7 +148,10 @@ class stencil_database():
# if s.info == 'STT_OBJECT'}
self.byteorder: ByteOrder = self.elf.byteorder
self.thumb_mode = self.elf.symbols['entry_function_shell'].thumb_mode
self.arm = '.ARM.attributes' in self.elf.sections
# Returns 1 for ARM in thumb mode, 0 otherwise
self.thumb_mode = self.elf.symbols['entry_function_shell'].fields['st_value'] & 1
#for name in self.function_definitions.keys():
# sym = self.elf.symbols[name]
@ -201,14 +194,14 @@ class stencil_database():
for reloc in symbol.relocations:
# address to fist byte to patch relative to the start of the symbol
patch_offset = reloc.fields['r_offset'] - symbol.offset_in_section - start_index
patch_offset = reloc.fields['r_offset'] - symbol.fields['st_value'] - start_index
if patch_offset < end_index - start_index: # Exclude the call to the result_* function
reloc_entry = relocation_entry(reloc.symbol.name,
reloc.symbol.info,
reloc.symbol.fields['st_value'], # LSB on ARM indicates thumb mode
reloc.symbol.fields['st_value'],
reloc.symbol.fields['st_shndx'],
symbol.offset_in_section,
symbol.fields['st_value'],
start_index,
reloc)
cache.append(reloc_entry)
@ -355,7 +348,10 @@ class stencil_database():
start_stencil, end_stencil = get_stencil_position(func)
assert func.section
start_index = func.offset_in_file + start_stencil
# For arm functions, mask out the thumb mode bit
function_offset = func.fields['st_value'] & ~int(self.arm)
start_index = func.section['sh_offset'] + function_offset + start_stencil
lengths = end_stencil - start_stencil
self._stencil_cache[name] = (start_index, lengths)
@ -393,7 +389,7 @@ class stencil_database():
def get_symbol_offset(self, name: str) -> int:
"""Returns the offset of a specified symbol in the section."""
return self.elf.symbols[name].offset_in_section
return self.elf.symbols[name].fields['st_value']
def get_symbol_section_index(self, name: str) -> int:
"""Returns the section index for a specified symbol name."""
@ -424,6 +420,13 @@ class stencil_database():
func = self.elf.symbols[name]
assert func.info == 'STT_FUNC', f"{name} is not a function"
# For arm functions, mask out the thumb mode bit
function_offset = func.fields['st_value'] & ~int(self.arm)
start_index = func.section['sh_offset'] + function_offset
lengths = end_stencil - start_stencil
#return self.elf.read_bytes(start_index, func.fields['st_size'])
if part == 'start':
index = get_last_call_in_function(func)
return func.data[:index]

View File

@ -25,16 +25,14 @@ ar x ../../musl/lib/libc.a sinf.o cosf.o tanf.o asinf.o acosf.o atanf.o atan2f.o
ar x ../../musl/lib/libc.a sqrtf.o logf.o expf.o sqrt.o
ar x ../../musl/lib/libc.a logf_data.o __tandf.o __cosdf.o __sindf.o
ar x ../../musl/lib/libc.a fabsf.o scalbn.o floor.o floorf.o exp2f_data.o powf.o powf_data.o
ar x ../../musl/lib/libc.a __rem_pio2f.o __math_invalid.o __math_invalidf.o __stack_chk_fail.o
ar x ../../musl/lib/libc.a __math_divzerof.o __math_oflowf.o __rem_pio2_large.o __math_uflowf.o __math_xflowf.o __rsqrt_tab.o
ar x ../../musl/lib/libc.a __rem_pio2f.o __math_invalidf.o __stack_chk_fail.o __math_divzerof.o __math_oflowf.o __rem_pio2_large.o __math_uflowf.o __math_xflowf.o
# Check out .lo (PIC)
ar x ../../musl/lib/libc.a sinf.lo cosf.lo tanf.lo asinf.lo acosf.lo atanf.lo atan2f.lo
ar x ../../musl/lib/libc.a sqrtf.lo logf.lo expf.lo sqrt.lo
ar x ../../musl/lib/libc.a logf_data.lo __tandf.lo __cosdf.lo __sindf.lo
ar x ../../musl/lib/libc.a fabsf.lo scalbn.lo floor.lo floorf.o exp2f_data.lo powf.lo powf_data.lo
ar x ../../musl/lib/libc.a __rem_pio2f.lo __math_invalid.lo __math_invalidf.lo __stack_chk_fail.lo
ar x ../../musl/lib/libc.a __math_divzerof.lo __math_oflowf.lo __rem_pio2_large.lo __math_uflowf.lo __math_xflowf.lo __rsqrt_tab.lo
ar x ../../musl/lib/libc.a __rem_pio2f.lo __math_invalidf.lo __stack_chk_fail.lo __math_divzerof.lo __math_oflowf.lo __rem_pio2_large.lo __math_uflowf.lo __math_xflowf.lo
cd ../../musl