format: import a Mach-O plug-in

This commit is contained in:
Pierre Pronchery 2025-04-27 00:24:42 +02:00
parent 4d0ea35ff2
commit ac1ed5b7f6
2 changed files with 141 additions and 1 deletions

136
src/format/mach-o.c Normal file
View File

@ -0,0 +1,136 @@
/* $Id$ */
/* Copyright (c) 2025 Pierre Pronchery <khorben@defora.org> */
/* 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 <http://www.gnu.org/licenses/>. */
#include <System.h>
#include <string.h>
#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);
}

View File

@ -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