Skip to content

Commit

Permalink
armv7/64_32 Support across board. Structs: Support Variable Sizes, St…
Browse files Browse the repository at this point in the history
…reamline defs.
  • Loading branch information
0cyn committed Nov 11, 2023
1 parent aaf144c commit ad4b1e3
Show file tree
Hide file tree
Showing 18 changed files with 3,811 additions and 472 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ poetry.lock
tests/bins/testbin1
tests/bins/testbin1.fat
tests/bins/testlib1.dylib
/tests/.build/
8 changes: 4 additions & 4 deletions src/ktool/codesign.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CodesignInfo(Constructable):
def from_image(cls, image, codesign_cmd: linkedit_data_command):
superblob: SuperBlob = image.read_struct(codesign_cmd.dataoff, SuperBlob)
slots: List[BlobIndex] = []
off = codesign_cmd.dataoff + SuperBlob.SIZE
off = codesign_cmd.dataoff + SuperBlob.size()

req_dat = None

Expand All @@ -41,7 +41,7 @@ def from_image(cls, image, codesign_cmd: linkedit_data_command):
blob_index.type = swap_32(blob_index.type)
blob_index.offset = swap_32(blob_index.offset)
slots.append(blob_index)
off += BlobIndex.SIZE
off += BlobIndex.size()

for blob in slots:
if blob.type == CSSLOT_ENTITLEMENTS:
Expand All @@ -50,14 +50,14 @@ def from_image(cls, image, codesign_cmd: linkedit_data_command):
ent_blob.magic = swap_32(ent_blob.magic)
ent_blob.length = swap_32(ent_blob.length)
ent_size = ent_blob.length
entitlements = image.read_fixed_len_str(start + Blob.SIZE, ent_size - Blob.SIZE)
entitlements = image.read_fixed_len_str(start + Blob.size(), ent_size - Blob.size())

elif blob.type == CSSLOT_REQUIREMENTS:
start = superblob.off + blob.offset
req_blob = image.read_struct(start, Blob)
req_blob.magic = swap_32(req_blob.magic)
req_blob.length = swap_32(req_blob.length)
req_dat = image.read_bytearray(start + Blob.SIZE, req_blob.length - Blob.SIZE)
req_dat = image.read_bytearray(start + Blob.size(), req_blob.length - Blob.size())

return cls(superblob, slots, entitlements=entitlements, req_dat=req_dat)

Expand Down
11 changes: 10 additions & 1 deletion src/ktool/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def serialize(self):
return {'install_name': self.install_name, 'load_command': LOAD_COMMAND(self.cmd.cmd).name}

def _get_name(self, cmd) -> str:
read_address = cmd.off + dylib_command.SIZE
read_address = cmd.off + dylib_command.size()
return self.source_image.read_cstr(read_address)


Expand Down Expand Up @@ -248,6 +248,7 @@ def __init__(self, macho_slice: Slice, force_misaligned_vm=False):
self.vm = MisalignedVM()
else:
self.vm_realign()
self.ptr_size = self.slice.ptr_size

self.base_name = "" # copy of self.name
self.install_name = ""
Expand Down Expand Up @@ -398,6 +399,14 @@ def read_uint(self, offset: int, length: int, vm=False):
offset = self.vm.translate(offset)
return self.slice.read_uint(offset, length)

def read_ptr(self, offset: int, vm=False):
""" Read a ptr (uint of size self.ptr_size)
:param offset:
:param vm:
"""
return self.read_uint(offset, self.ptr_size, vm=vm)

def read_int(self, offset: int, length: int, vm=False):
return uint_to_int(self.read_uint(offset, length, vm), length * 8)

Expand Down
2 changes: 1 addition & 1 deletion src/ktool/ktool_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ def _list(args):
lc_dat = str(lc)
if LOAD_COMMAND(lc.cmd) in [LOAD_COMMAND.LOAD_DYLIB, LOAD_COMMAND.ID_DYLIB,
LOAD_COMMAND.SUB_CLIENT]:
lc_dat += '\n"' + image.read_cstr(lc.off + lc.SIZE, vm=False) + '"'
lc_dat += '\n"' + image.read_cstr(lc.off + lc.size(), vm=False) + '"'
table.rows.append([str(i), LOAD_COMMAND(lc.cmd).name.ljust(15, ' '), lc_dat])
print(table.fetch_all(get_terminal_size().columns - 5))
elif args.get_classes:
Expand Down
14 changes: 7 additions & 7 deletions src/ktool/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def _load_symbol_table(self) -> List[Symbol]:
typing = symtab_entry if self.image.macho_header.is64 else symtab_entry_32

for i in range(0, self.cmd.nsyms):
entry = self.image.read_struct(read_address + typing.SIZE * i, typing)
entry = self.image.read_struct(read_address + typing.size() * i, typing)
symbol = Symbol.from_image(self.image, self.cmd, entry)
symbol_table.append(symbol)

Expand All @@ -381,7 +381,7 @@ def from_image(cls, image: Image, chained_fixup_cmd: linkedit_data_command):
log.error("Unknown Fixup Format")
return cls([])

import_table_size = fixup_header.imports_count * dyld_chained_import.SIZE
import_table_size = fixup_header.imports_count * dyld_chained_import.size()
if import_table_size > chained_fixup_cmd.datasize:
log.error("Chained fixup import table is larger than chained fixup linkedit region")
return cls([])
Expand Down Expand Up @@ -753,15 +753,15 @@ def _load_binding_info(self, table_start: int, table_size: int) -> List[record]:
import_stack.append(
record(cmd_start_addr, seg_index, seg_offset, lib_ordinal, btype, flags, name, addend,
special_dylib))
seg_offset += 8
seg_offset += self.image.ptr_size
o, read_address = self.image.read_uleb128(read_address)
seg_offset += o

elif binding_opcode == BINDING_OPCODE.DO_BIND_ADD_ADDR_IMM_SCALED:
import_stack.append(
record(cmd_start_addr, seg_index, seg_offset, lib_ordinal, btype, flags, name, addend,
special_dylib))
seg_offset = seg_offset + (value * 8) + 8
seg_offset = seg_offset + (value * self.image.ptr_size) + self.image.ptr_size

elif binding_opcode == BINDING_OPCODE.DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
count, read_address = self.image.read_uleb128(read_address)
Expand All @@ -771,18 +771,18 @@ def _load_binding_info(self, table_start: int, table_size: int) -> List[record]:
import_stack.append(
record(cmd_start_addr, seg_index, seg_offset, lib_ordinal, btype, flags, name, addend,
special_dylib))
seg_offset += skip + 8
seg_offset += skip + self.image.ptr_size

elif binding_opcode == BINDING_OPCODE.DO_BIND:
if not uses_threaded_bind:
import_stack.append(
record(cmd_start_addr, seg_index, seg_offset, lib_ordinal, btype, flags, name, addend,
special_dylib))
seg_offset += 8
seg_offset += self.image.ptr_size
else:
threaded_stack.append(
record(cmd_start_addr, seg_index, seg_offset, lib_ordinal, btype, flags, name, addend,
special_dylib))
seg_offset += 8
seg_offset += self.image.ptr_size

return import_stack
Loading

0 comments on commit ad4b1e3

Please sign in to comment.