From ac1ed5b7f634062d899320bc19ff65d037b9a04e Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Sun, 27 Apr 2025 00:24:42 +0200 Subject: [PATCH] format: import a Mach-O plug-in --- src/format/mach-o.c | 136 ++++++++++++++++++++++++++++++++++++++++ src/format/project.conf | 6 +- 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/format/mach-o.c diff --git a/src/format/mach-o.c b/src/format/mach-o.c new file mode 100644 index 0000000..0afe0fc --- /dev/null +++ b/src/format/mach-o.c @@ -0,0 +1,136 @@ +/* $Id$ */ +/* Copyright (c) 2025 Pierre Pronchery */ +/* This file is part of DeforaOS Devel Asm */ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . */ + + + +#include +#include +#include "Asm.h" + + +/* Mach-O */ +/* private */ +/* types */ +struct _AsmFormatPlugin +{ + AsmFormatPluginHelper * helper; +}; + + +/* prototypes */ +/* plug-in */ +static AsmFormatPlugin * _macho_init(AsmFormatPluginHelper * helper, + char const * arch); +static int _macho_destroy(AsmFormatPlugin * format); +static char const * _macho_guess(AsmFormatPlugin * format, char const * hint); +static int _macho_section(AsmFormatPlugin * format, char const * section); +static int _macho_decode(AsmFormatPlugin * format, int raw); +static int _macho_decode_section(AsmFormatPlugin * format, AsmSection * section, + AsmArchInstructionCall ** calls, size_t * calls_cnt); + + +/* public */ +/* variables */ +/* plug-in */ +AsmFormatPluginDefinition format_plugin = +{ + "mach-o", + "Mach-O file", + LICENSE_GNU_LGPL3_FLAGS, + NULL, + 0, + _macho_init, + _macho_destroy, + _macho_guess, + NULL, + NULL, + _macho_section, + NULL, + _macho_decode, + _macho_decode_section +}; + + +/* private */ +/* functions */ +/* plug-in */ +/* macho_init */ +static AsmFormatPlugin * _macho_init(AsmFormatPluginHelper * helper, + char const * arch) +{ + AsmFormatPlugin * macho; + (void) arch; + + if((macho = object_new(sizeof(*macho))) == NULL) + return NULL; + macho->helper = helper; + return macho; +} + + +/* macho_destroy */ +static int _macho_destroy(AsmFormatPlugin * format) +{ + object_delete(format); + return 0; +} + + +/* macho_guess */ +static char const * _macho_guess(AsmFormatPlugin * format, char const * hint) +{ + (void) format; + + return hint; +} + + +/* macho_section */ +static int _macho_section(AsmFormatPlugin * format, char const * section) +{ + (void) format; + (void) section; + + /* ignore sections */ + return 0; +} + + +/* macho_decode */ +static int _macho_decode(AsmFormatPlugin * format, int raw) +{ + AsmFormatPluginHelper * helper = format->helper; + off_t offset; + (void) raw; + + if((offset = helper->seek(helper->format, 0, SEEK_END)) >= 0 + && helper->set_section(helper->format, 0, 0, ".text", 0, + offset, 0) != NULL) + return 0; + return -1; +} + + +/* macho_decode_section */ +static int _macho_decode_section(AsmFormatPlugin * format, AsmSection * section, + AsmArchInstructionCall ** calls, size_t * calls_cnt) +{ + AsmFormatPluginHelper * helper = format->helper; + + if(section->id != 0) + return -1; + return helper->decode(helper->format, section->offset, section->size, + section->base, calls, calls_cnt); +} diff --git a/src/format/project.conf b/src/format/project.conf index 4d3c2aa..f0c8212 100644 --- a/src/format/project.conf +++ b/src/format/project.conf @@ -1,4 +1,4 @@ -targets=dex,elf,flat,java,mbr,pe,template +targets=dex,elf,flat,java,mach-o,mbr,pe,template cppflags_force=-I../../include cflags_force=`pkg-config --cflags libSystem` -fPIC cflags=-W -Wall -g -O2 -D_FORTIFY_SOURCE=2 -fstack-protector @@ -33,6 +33,10 @@ type=plugin sources=java.c install=$(LIBDIR)/Asm/format +[mach-o] +type=plugin +sources=mach-o.c + [mbr] type=plugin sources=mbr.c