Skip to content

Commit

Permalink
Improve docs, add MachOImageHeader.__iter__ for load commands
Browse files Browse the repository at this point in the history
  • Loading branch information
0cyn committed Nov 11, 2023
1 parent ad4b1e3 commit 22fa1f8
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
Binary file added docs/source/_static/ktool2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
# -- Project information -----------------------------------------------------

project = 'ktool'
copyright = '2022, kat'
copyright = '2023, 0cyn'
author = 'cynder'

# The full version, including alpha/beta/rc tags
release = '1.2.0'
release = '2.0.0'

# -- General configuration ---------------------------------------------------

Expand Down Expand Up @@ -50,8 +50,8 @@
html_theme_options = {
"sidebar_hide_name": True,
"navigation_with_keys": True,
"light_logo": "logo.png",
"dark_logo": "logo.png",
"light_logo": "ktool2.png",
"dark_logo": "ktool2.png",
'navigation_depth': 4,
}
html_css_files = [
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ktool
:maxdepth: 2
:caption: ktool Library:

quickstart
ktool


Expand Down
68 changes: 67 additions & 1 deletion docs/source/quickstart.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,70 @@
Quick-Start Guide
---------------------

TODO
This is documentation for getting started with the library as a component of other python projects.

Gonna try and speedrun this explanation so you can get up and running as soon as possible.

Basic Concepts to understand:
* There are a lot of subfiles and a few modules, but :python:`import ktool` will import all of the stuff you most likely need.
* My struct system emulates C's. Or if you don't know C, it's like someone smashed together python structs and namedtuples.

On the github, :sh:`src/ktool/ktool_script.py` is a fairly standard client for this library, and you can reference it to
figure out how to do a lot of the basic stuff this library is capable of.

Install The Library
=======================

:sh:`python3 -m pip install k2l`

To install new updates:

:sh:`python3 -m pip install --upgrade k2l`


Code Examples
=======================

Ideally this library is fairly intuitive to use, and things just work how you expect.

.. code-block:: python
:caption: Load an image and dump the symbol list
:emphasize-lines: 3
import ktool
image = ktool.load_image('my/file.dylib')
for addr, symbol in image.symbols.items():
print(f'{symbol.name} => {addr}')
.. code-block:: python
:caption: Dump the classlist for an image
:emphasize-lines: 4
import ktool
image = ktool.load_image('my/file.dylib')
objc_image = ktool.load_objc_metadata(image)
for objc_class in objc_image.classlist:
print(f'{objc_class.name}')
.. code-block:: python
:caption: Loading and iterating the Mach-O Header.
:emphasize-lines: 3,4
import ktool
image = ktool.load_image('my/file.dylib')
for load_command in image.macho_header: # Using the MachOImageHeader __iter__ functionality
if isinstance(load_command, dylinker_command):
print('Dylinker cmd!')
print(f'{load_command.render_indented(4)}')
# OR, using the basic list iterator
for load_command in image.macho_header.load_commands:
if isinstance(load_command, dylinker_command):
print('Dylinker cmd!')
print(f'{load_command.render_indented(4)}')
21 changes: 21 additions & 0 deletions src/ktool/macho.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,24 @@ def _load_subtype(self, cputype: CPUType):


class MachOImageHeader(Constructable):

class MachOLoadCommandIterator:
def __init__(self, hdr: 'MachOImageHeader'):
self.pos = -1
self.hdr = hdr

def __iter__(self):
return self

def __next__(self):
self.pos += 1
if self.pos >= len(self.hdr.load_commands):
self.pos = -1
raise StopIteration
return self.hdr.load_commands[self.pos]



"""
This class represents the Mach-O Header
It contains the basic header info along with all load commands within it.
Expand Down Expand Up @@ -528,6 +546,9 @@ def __init__(self):
self.load_commands = []
self.raw = bytearray()

def __iter__(self):
return MachOImageHeader.MachOLoadCommandIterator(self)

def serialize(self):
return {'filetype': self.filetype.name, 'flags': [flag.name for flag in self.flags], 'is_64_bit': self.is64,
'dyld_header': self.dyld_header.serialize(),
Expand Down

0 comments on commit 22fa1f8

Please sign in to comment.