Skip to content

Commit

Permalink
[WIP] Wire up some more
Browse files Browse the repository at this point in the history
Co-authored-by: Håvard Sørbø <[email protected]>
  • Loading branch information
oleavr and hsorbo committed Sep 12, 2023
1 parent d475cb8 commit 3ed1612
Showing 1 changed file with 97 additions and 4 deletions.
101 changes: 97 additions & 4 deletions gum/gumswiftapiresolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ typedef size_t (* GumSwiftDemangle) (const char * name, char * output,
size_t length);
typedef struct _GumTargetContextDescriptor GumTargetContextDescriptor;
typedef struct _GumTargetTypeContextDescriptor GumTargetTypeContextDescriptor;
typedef struct _GumTargetGenericContextDescriptorHeader
GumTargetGenericContextDescriptorHeader;
typedef struct _GumTargetTypeGenericContextDescriptorHeader
GumTargetTypeGenericContextDescriptorHeader;
typedef struct _GumTargetClassDescriptor GumTargetClassDescriptor;
typedef struct _GumTargetVTableDescriptorHeader GumTargetVTableDescriptorHeader;
typedef struct _GumTargetOverrideTableHeader GumTargetOverrideTableHeader;

struct _GumSwiftApiResolver
{
Expand Down Expand Up @@ -58,6 +65,18 @@ struct _GumFunctionMetadata
GumAddress address;
};

enum _GumContextDescriptorKind
{
GUM_CONTEXT_DESCRIPTOR_CLASS = 16,
GUM_CONTEXT_DESCRIPTOR_STRUCT,
};

enum _GumTypeContextDescriptorFlags
{
GUM_CLASS_HAS_VTABLE = (1 << 15),
GUM_CLASS_HAS_OVERRIDE_TABLE = (1 << 14),
};

struct _GumTargetContextDescriptor
{
guint32 flags;
Expand All @@ -72,10 +91,42 @@ struct _GumTargetTypeContextDescriptor
gint32 fields_delta;
};

enum _GumContextDescriptorKind
struct _GumTargetGenericContextDescriptorHeader
{
GUM_CONTEXT_DESCRIPTOR_CLASS = 16,
GUM_CONTEXT_DESCRIPTOR_STRUCT,
guint16 num_params;
guint16 num_requirements;
guint16 num_key_arguments;
guint16 flags;
};

struct _GumTargetTypeGenericContextDescriptorHeader
{
gint32 instantiation_cache_delta;
gint32 default_instantiation_pattern_delta;
GumTargetGenericContextDescriptorHeader base;
};

struct _GumTargetClassDescriptor
{
GumTargetTypeContextDescriptor target_type;
gint32 superclass_type_delta;
guint32 metadata_negative_size_in_words_or_resilient_metadata_bounds;
guint32 metadata_positive_size_in_words_or_extra_class_flags;
guint32 num_immediate_members;
guint32 num_fields;

guint32 padding;
};

struct _GumTargetVTableDescriptorHeader
{
guint32 vtable_offset;
guint32 vtable_size;
};

struct _GumTargetOverrideTableHeader
{
guint32 num_entries;
};

static void gum_swift_api_resolver_iface_init (gpointer g_iface,
Expand Down Expand Up @@ -321,6 +372,8 @@ gum_module_metadata_unref (GumModuleMetadata * module)
}
}

static GumAddress the_base;

static GArray *
gum_module_metadata_get_functions (GumModuleMetadata * self)
{
Expand All @@ -329,7 +382,13 @@ gum_module_metadata_get_functions (GumModuleMetadata * self)
self->functions = g_array_new (FALSE, FALSE, sizeof (GumFunctionMetadata));
g_array_set_clear_func (self->functions,
(GDestroyNotify) gum_function_metadata_free);
g_printerr ("sizeof (GumTargetContextDescriptor) == %zu\n", sizeof (GumTargetContextDescriptor));
g_printerr ("sizeof (GumTargetTypeContextDescriptor) == %zu\n", sizeof (GumTargetTypeContextDescriptor));
g_printerr ("sizeof (GumTargetClassDescriptor) == %zu\n", sizeof (GumTargetClassDescriptor));
g_printerr ("offsetof (GumTargetClassDescriptor, target_type) == %zu\n", G_STRUCT_OFFSET (GumTargetClassDescriptor, target_type));
g_printerr ("offsetof (GumTargetClassDescriptor, superclass_type_delta) == %zu\n", G_STRUCT_OFFSET (GumTargetClassDescriptor, superclass_type_delta));
g_printerr ("collecting exports of %s\n", self->path);
the_base = gum_module_find_base_address (self->path);
gum_module_enumerate_sections (self->path,
gum_module_metadata_collect_section, self);
g_printerr (">>>\n");
Expand All @@ -345,13 +404,16 @@ static gboolean
gum_module_metadata_collect_section (const GumSectionDetails * details,
gpointer user_data)
{
GumModuleMetadata * module = user_data;
gsize n, i;
gint32 * type_deltas;

if (strcmp (details->name, "__swift5_types") != 0)
return TRUE;

n = details->size / sizeof (gint32);
g_printerr ("address=%p\n", (void *) details->address);
g_printerr ("offset=0x%x\n", (int) (details->address - the_base));
g_printerr ("n=%zu\n", n);

type_deltas = GSIZE_TO_POINTER (details->address);
Expand All @@ -365,16 +427,47 @@ gum_module_metadata_collect_section (const GumSectionDetails * details,
guint8 kind;

name = (const gchar *) &type->name_delta + type->name_delta;
g_printerr ("\n=== %s\n", name);
g_printerr ("\tflags=0x%08x\n", type->target_ctx.flags);
kind = type->target_ctx.flags & 0x1f;

switch (kind)
{
case GUM_CONTEXT_DESCRIPTOR_CLASS:
g_printerr ("\t[class] %s\n", name);
{
GumTargetClassDescriptor * cd = (GumTargetClassDescriptor *) type;
guint16 type_flags;
gboolean has_vtable, has_override_table;

type_flags = type->target_ctx.flags >> 16;

g_printerr ("\tclass offset=0x%x\n", (int) (GUM_ADDRESS (cd) - the_base));
has_vtable = (type_flags & GUM_CLASS_HAS_VTABLE) != 0;
g_printerr ("\thas_vtable=%s\n", has_vtable ? "TRUE" : "FALSE");
if (has_vtable)
{
GumTargetVTableDescriptorHeader * vth =
(GumTargetVTableDescriptorHeader *) (cd + 1);
g_printerr ("\tvtable offset: 0x%x\n", vth->vtable_offset);
g_printerr ("\tvtable size: 0x%x\n", vth->vtable_size);
}

has_override_table = (type_flags & GUM_CLASS_HAS_OVERRIDE_TABLE) != 0;
g_printerr ("\thas_override_table=%s\n", has_override_table ? "TRUE" : "FALSE");
if (has_override_table)
{
GumTargetOverrideTableHeader * oth =
(GumTargetOverrideTableHeader *) (cd + 1);
g_printerr ("\toverride entries: 0x%x\n", oth->num_entries);
}

break;
}
case GUM_CONTEXT_DESCRIPTOR_STRUCT:
{
g_printerr ("\t[struct] %s\n", name);
break;
}
default:
break;
}
Expand Down

0 comments on commit 3ed1612

Please sign in to comment.