/* $Id: bflt.c,v 1.3 2021/11/06 17:35:07 khorben Exp $ */ /* Copyright (c) 2010 khorben of UberWall */ /* This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation version 2 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include "plugin.h" #define max(a, b) (((a) > (b)) ? (a) : (b)) #define min(a, b) (((a) < (b)) ? (a) : (b)) /* bflt */ /* public */ /* types */ #pragma pack(1) struct flat_hdr { char magic[4]; uint32_t rev; uint32_t entry; uint32_t data_start; uint32_t data_end; uint32_t bss_end; uint32_t stack_size; uint32_t reloc_start; uint32_t reloc_count; uint32_t flags; uint32_t filler[6]; }; #pragma pack() /* constants */ #define FLAT_FLAG_RAM 0x01 #define FLAT_FLAG_GOTPIC 0x02 #define FLAT_FLAG_GZIP 0x04 /* variables */ /* magic */ static unsigned char sig[] = "bFLT"; static PluginMagic bflt_magic[] = { { sizeof(struct flat_hdr), 0, sig, sizeof(sig)-1 }, { 0, 0, NULL, 0 } }; /* functions */ static int bflt_callback(PluginHelper * ph, int signature, FILE * fp); /* plugin */ Plugin plugin = { PT_COMPRESSION | PT_EXECUTABLE, "BFLT", bflt_magic, bflt_callback }; /* functions */ /* bflt_callback */ static int _callback_endian(PluginHelper * ph, struct flat_hdr * buf); static int _callback_version(PluginHelper * ph, uint32_t version); static int _callback_offsets(PluginHelper * ph, uint32_t data_start, uint32_t data_end, uint32_t bss_end); static int _callback_stack(PluginHelper * ph, uint32_t stack_size); static int _callback_reloc(PluginHelper * ph, uint32_t reloc_start, uint32_t reloc_count); static int _callback_flags(PluginHelper * ph, uint32_t flags); static int _callback_extract(PluginHelper * ph, FILE * fp, struct flat_hdr * fh); static int bflt_callback(PluginHelper * ph, int signature, FILE * fp) { int score = 0; struct flat_hdr hdr; struct flat_hdr buf; (void) signature; if(fread(&hdr, sizeof(hdr), 1, fp) != 1) return 0; memcpy(&buf, &hdr, sizeof(buf)); score+=_callback_endian(ph, &buf); score+=_callback_version(ph, buf.rev); score+=_callback_offsets(ph, buf.data_start, buf.data_end, buf.bss_end); score+=_callback_stack(ph, buf.stack_size); score+=_callback_reloc(ph, buf.reloc_start, buf.reloc_count); score+=_callback_flags(ph, buf.flags); ph->printf(ph, "\n"); _callback_extract(ph, fp, &hdr); return score / 6; } static int _callback_endian(PluginHelper * ph, struct flat_hdr * buf) { if(buf->rev == 2 || buf->rev == 4) { ph->printf(ph, "%s", #if BYTE_ORDER == BIG_ENDIAN "big endian"); #else "little endian"); #endif return 100; } if(bswap32(buf->rev) != 2 && bswap32(buf->rev) != 4) { ph->printf(ph, "%s", "unknown endian"); return 0; } ph->printf(ph, "%s", #if BYTE_ORDER == BIG_ENDIAN "little endian"); #else "big endian"); #endif #define SWAP(a) a = bswap32(a) SWAP(buf->rev); SWAP(buf->entry); SWAP(buf->data_start); SWAP(buf->data_end); SWAP(buf->bss_end); SWAP(buf->stack_size); SWAP(buf->reloc_start); SWAP(buf->reloc_count); SWAP(buf->flags); return 100; } static int _callback_version(PluginHelper * ph, uint32_t version) { if(version == 2 || version == 4) { ph->printf(ph, "%s%u", ", version ", version); return 100; } ph->printf(ph, "%s%u", ", unknown version (", version, ")"); if(version <= 4) return 50; return 0; } static int _callback_offsets(PluginHelper * ph, uint32_t data_start, uint32_t data_end, uint32_t bss_end) { ph->printf(ph, "%s%x%s%x%s%x", ", data_start 0x", data_start, ", data_end 0x", data_end, ", bss_end 0x", bss_end); if(data_start >= sizeof(struct flat_hdr) && data_end > data_start && (bss_end > data_end || bss_end <= data_start)) return 100; return 0; } static int _callback_stack(PluginHelper * ph, uint32_t stack_size) { ph->printf(ph, "%s%x", ", stack_size 0x", stack_size); if(stack_size == 0x2000) return 100; return 50; } static int _callback_reloc(PluginHelper * ph, uint32_t reloc_start, uint32_t reloc_count) { ph->printf(ph, "%s%u%s%x", ", ", reloc_count, " relocations at 0x", reloc_start); return 100; } static int _callback_flags(PluginHelper * ph, uint32_t flags) { static const struct { uint32_t flag; char const * string; } name[] = { { FLAT_FLAG_RAM, "loaded in RAM" }, { FLAT_FLAG_GOTPIC, "is PIC with GOT" }, { FLAT_FLAG_GZIP, "compressed" }, { 0, NULL } }; unsigned int i; for(i = 0; name[i].string != NULL; i++) if((flags & name[i].flag) == name[i].flag) ph->printf(ph, "%s%s", ", ", name[i].string); return flags & ~(FLAT_FLAG_RAM | FLAT_FLAG_GOTPIC | FLAT_FLAG_GZIP) ? 0 : 100; } static int _callback_extract(PluginHelper * ph, FILE * fp, struct flat_hdr * fh) { FILE * xfp; size_t size; size_t i; char buf[1024]; size_t cnt = 0; size = max(fh->data_end, fh->bss_end); /* FIXME what about relocations? */ if((xfp = ph->fopen(ph, "a.out")) == NULL) return -1; if(ph->fwrite(fh, sizeof(*fh), 1, xfp) != 1) return -1; while((i = fread(buf, sizeof(char), min(size - cnt, sizeof(buf)), fp)) > 0) { if(ph->fwrite(buf, sizeof(char), i, xfp) != i) break; cnt += i; } return ph->fclose(xfp); }