From 75f3f661a754ecf31fcc61cc27bd4020c0dfc793 Mon Sep 17 00:00:00 2001 From: maxcodehack Date: Sat, 5 Dec 2020 07:05:55 +0000 Subject: [PATCH] Upload libudis86 disassembly library git-svn-id: svn://kolibrios.org@8322 a494cfbc-eb01-0410-851d-a64ba20cac60 --- contrib/other/udcli/Makefile | 11 ++ contrib/other/udcli/config.h | 56 +++++++ contrib/other/udcli/console_obj.h | 141 ++++++++++++++++ contrib/other/udcli/udcli.c | 267 ++++++++++++++++++++++++++++++ contrib/other/udcli/udis86.h | 16 ++ 5 files changed, 491 insertions(+) create mode 100755 contrib/other/udcli/Makefile create mode 100755 contrib/other/udcli/config.h create mode 100755 contrib/other/udcli/console_obj.h create mode 100755 contrib/other/udcli/udcli.c create mode 100755 contrib/other/udcli/udis86.h diff --git a/contrib/other/udcli/Makefile b/contrib/other/udcli/Makefile new file mode 100755 index 0000000000..086a073d0d --- /dev/null +++ b/contrib/other/udcli/Makefile @@ -0,0 +1,11 @@ +SDK_DIR = ../../sdk +LDFLAGS = -static -S -nostdlib -T $(SDK_DIR)/sources/newlib/app.lds --image-base 0 +CFLAGS = -c -fno-ident -O2 -fomit-frame-pointer -fno-ident -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 +INCLUDES = -I $(SDK_DIR)/sources/newlib/libc/include +LIBPATH = -L $(SDK_DIR)/lib -L /home/autobuild/tools/win32/mingw32/lib -L ./libudis86 + +default: + kos32-gcc $(CFLAGS) $(INCLUDES) -o udcli.o udcli.c + kos32-ld $(LDFLAGS) $(LIBPATH) --subsystem console -o udcli udcli.o -ludis86 -lgcc -lc.dll + objcopy udcli -O binary + rm *.o diff --git a/contrib/other/udcli/config.h b/contrib/other/udcli/config.h new file mode 100755 index 0000000000..a10198dd83 --- /dev/null +++ b/contrib/other/udcli/config.h @@ -0,0 +1,56 @@ +/* config.h. Generated from config.h.in by configure. */ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Name of package */ +#define PACKAGE "udis86" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "vivek@sig9.com" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "udis86" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "udis86 1.7" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "udis86" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "1.7" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Version number of package */ +#define VERSION "1.7" diff --git a/contrib/other/udcli/console_obj.h b/contrib/other/udcli/console_obj.h new file mode 100755 index 0000000000..29d99ad0cf --- /dev/null +++ b/contrib/other/udcli/console_obj.h @@ -0,0 +1,141 @@ +// Console.obj loading for kos32-gcc +// Writed by rgimad and maxcodehack + +#include +#include + +#ifndef CONSOLE_OBJ_H +#define CONSOLE_OBJ_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef NULL +#define NULL 0 +#endif + +#ifndef cdecl +#define cdecl __attribute__ ((cdecl)) +#endif + +#ifndef stdcall +#define stdcall __attribute__ ((stdcall)) +#endif + +typedef unsigned int dword; +typedef unsigned short word; + +const char* imports[] = { + "START", "version", "con_init", "con_write_asciiz", "con_write_string", + "con_printf", "con_exit", "con_get_flags", "con_set_flags", "con_kbhit", + "con_getch", "con_getch2", "con_gets", "con_gets2", "con_get_font_height", + "con_get_cursor_height", "con_set_cursor_height", "con_cls", + "con_get_cursor_pos", "con_set_cursor_pos", "con_set_title", + (char*)0 +}; + +dword *version; + +typedef int (stdcall * con_gets2_callback)(int keycode, char** pstr, int* pn, + int* ppos); + +void stdcall (*con_init)(dword wnd_width, dword wnd_height, dword scr_width, dword scr_height, const char* title) = 0; +void stdcall (*con_exit)(int bCloseWindow) = 0; +void stdcall (*con_set_title)(const char* title) = 0; +void stdcall (*con_write_asciiz)(const char* str) = 0; +void stdcall (*con_write_string)(const char* str, dword length) = 0; +int cdecl (*con_printf)(const char* format, ...) = 0; +dword stdcall (*con_get_flags)(void) = 0; +dword stdcall (*con_set_flags)(dword new_flags) = 0; +int stdcall (*con_get_font_height)(void) = 0; +int stdcall (*con_get_cursor_height)(void) = 0; +int stdcall (*con_set_cursor_height)(int new_height) = 0; +int stdcall (*con_getch)(void) = 0; +word stdcall (*con_getch2)(void) = 0; +int stdcall (*con_kbhit)(void) = 0; +char* stdcall (*con_gets)(char* str, int n) = 0; +char* stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n) = 0; +void stdcall (*con_cls)() = 0; +void stdcall (*con_get_cursor_pos)(int* px, int* py) = 0; +void stdcall (*con_set_cursor_pos)(int x, int y) = 0; + +const char lib_path[] = "/sys/lib/console.obj"; + +void* load_library(const char *name) +{ + void *table; + __asm__ __volatile__( + "int $0x40" + :"=a"(table) + :"a"(68), "b"(19), "c"(name)); + return table; +} + +void *load_library_procedure(void *exports, const char *name) +{ + if (exports == NULL) { return 0; } + while (*(dword*)exports != 0) + { + char *str1 = (char*)(*(dword*)exports); + if (strcmp(str1, name) == 0) + { + void *ptr = (void*)*(dword*)(exports + 4); + return ptr; + } + exports += 8; + } + return 0; +} + +void output_debug_string(const char *s) +{ + unsigned int i = 0; + while(*(s + i)) + { + asm volatile ("int $0x40"::"a"(63), "b"(1), "c"(*(s + i))); + i++; + } +} + +void load_console() +{ + void *lib = load_library(lib_path); + + if (!lib) + { + output_debug_string("Console.obj loading error\r\n"); + exit(1); + } + + dword (*start_lib)(dword) = (dword(*)(dword))load_library_procedure(lib, imports[0]); + + version = (dword*)load_library_procedure(lib, imports[1]); + + con_init = (void stdcall(*)(dword,dword,dword,dword,const char*))load_library_procedure(lib, imports[2]); + con_write_asciiz = (void stdcall(*)(const char*))load_library_procedure(lib, imports[3]); + con_write_string = (void stdcall(*)(const char*,dword))load_library_procedure(lib, imports[4]); + con_printf = (int cdecl(*)(const char*,...))load_library_procedure(lib, imports[5]); + con_exit = (void stdcall(*)(int))load_library_procedure(lib, imports[6]); + con_get_flags = (dword stdcall(*)(void))load_library_procedure(lib, imports[7]); + con_set_flags = (dword stdcall(*)(dword))load_library_procedure(lib, imports[8]); + con_kbhit = (int stdcall(*)(void))load_library_procedure(lib, imports[9]); + con_getch = (int stdcall(*)(void))load_library_procedure(lib, imports[10]); + con_getch2 = (word stdcall(*)(void))load_library_procedure(lib, imports[11]); + con_gets = (char* stdcall(*)(char*,int))load_library_procedure(lib, imports[12]); + con_gets2 = (char* stdcall(*)(con_gets2_callback,char*,int))load_library_procedure(lib, imports[13]); + con_get_font_height = (int stdcall(*)(void))load_library_procedure(lib, imports[14]); + con_get_cursor_height = (int stdcall(*)(void))load_library_procedure(lib, imports[15]); + con_set_cursor_height = (int stdcall(*)(int))load_library_procedure(lib, imports[16]); + con_cls = (void stdcall(*)(void))load_library_procedure(lib, imports[17]); + con_get_cursor_pos = (void stdcall(*)(int*,int*))load_library_procedure(lib, imports[18]); + con_set_cursor_pos = (void stdcall(*)(int,int))load_library_procedure(lib, imports[19]); + con_set_title = (void stdcall(*)(const char*))load_library_procedure(lib, imports[20]); + +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/contrib/other/udcli/udcli.c b/contrib/other/udcli/udcli.c new file mode 100755 index 0000000000..8fc3753464 --- /dev/null +++ b/contrib/other/udcli/udcli.c @@ -0,0 +1,267 @@ +/* ----------------------------------------------------------------------------- + * udcli.c - front end to udis86. + * + * Copyright (c) 2004,2005,2006,2007 Vivek Mohan + * All rights reserved. + * See (LICENSE) + * ----------------------------------------------------------------------------- + */ +#include "console_obj.h" +#include +#include +#include +#include +#include "udis86.h" +#include "config.h" + +#if defined(__amd64__) || defined(__x86_64__) +# define FMT "l" +#else +# define FMT "ll" +#endif + +#if defined(__DJGPP__) || defined(_WIN32) +# include +# include +#endif + +#ifdef __DJGPP__ +# include /* for isatty() */ +//# define _setmode setmode +# define _fileno fileno +# define _O_BINARY O_BINARY +#endif + +/* help string */ +static char help[] = +{ + "Usage: %s [-option[s]] file\n" + "Options:\n" + " -16 : Set the disassembly mode to 16 bits. \n" + " -32 : Set the disassembly mode to 32 bits. (default)\n" + " -64 : Set the disassembly mode to 64 bits.\n" + " -intel : Set the output to INTEL (NASM like) syntax. (default)\n" + " -att : Set the output to AT&T (GAS like) syntax.\n" + " -v : Set vendor. = {intel, amd}.\n" + " -o : Set the value of program counter to . (default = 0)\n" + " -s : Set the number of bytes to skip before disassembly to .\n" + " -c : Set the number of bytes to disassemble to .\n" + " -x : Set the input mode to whitespace seperated 8-bit numbers in\n" + " hexadecimal representation. Example: 0f 01 ae 00\n" + " -noff : Do not display the offset of instructions.\n" + " -nohex : Do not display the hexadecimal code of instructions.\n" + " -h : Display this help message.\n" + " --version: Show version.\n" + "\n" + "Udcli is a front-end to the Udis86 Disassembler Library.\n" + "http://udis86.sourceforge.net/\n" +}; + +FILE* fptr = NULL; +uint64_t o_skip = 0; +uint64_t o_count = 0; +unsigned char o_do_count= 0; +unsigned char o_do_off = 1; +unsigned char o_do_hex = 1; +unsigned char o_do_x = 0; +unsigned o_vendor = UD_VENDOR_AMD; + +int input_hook_x(ud_t* u); +int input_hook_file(ud_t* u); + +int main(int argc, char **argv) +{ + load_console(); + con_set_title("udi disassembler"); + FILE *stream; + stream = fopen( "disasm.out", "w" ); + + char *prog_path = *argv; + char *s; + ud_t ud_obj; + int mode = 0; + + /* initialize */ + ud_init(&ud_obj); + ud_set_mode(&ud_obj, 32); + ud_set_syntax(&ud_obj, UD_SYN_INTEL); + +#ifdef __DJGPP__ + if ( !isatty( fileno( stdin ) ) ) +#endif +#if defined(__DJGPP) || defined(_WIN32) +// _setmode(_fileno(stdin), _O_BINARY); +#endif + + fptr = stdin; + + argv++; + + /* loop through the args */ + while(--argc > 0) { + if (strcmp(*argv,"-16") == 0) { + ud_set_mode(&ud_obj, 16); + mode = 16; + } else if (strcmp(*argv,"-32") == 0) { + ud_set_mode(&ud_obj, 32); + mode = 32; + } else if (strcmp(*argv,"-64") == 0) { + ud_set_mode(&ud_obj, 64); + mode = 64; + } else if (strcmp(*argv,"-intel") == 0) + ud_set_syntax(&ud_obj, UD_SYN_INTEL); + else if (strcmp(*argv,"-att") == 0) + ud_set_syntax(&ud_obj, UD_SYN_ATT); + else if (strcmp(*argv,"-noff") == 0) + o_do_off = 0; + else if (strcmp(*argv,"-nohex") == 0) + o_do_hex = 0; + else if (strcmp(*argv,"-x") == 0) + o_do_x = 1; + else if (strcmp(*argv,"-s") == 0) + if (--argc) { + s = *(++argv); + if (sscanf(s, "%" FMT "d", &o_skip) == 0) + printf( "Invalid value given for -s.\n"); + } else { + printf( "No value given for -s.\n"); + printf(help, prog_path); + exit(EXIT_FAILURE); + } + else if (strcmp(*argv,"-c") == 0) + if (--argc) { + o_do_count= 1; + s = *(++argv); + if (sscanf(s, "%" FMT "d", &o_count) == 0) + printf( "Invalid value given for -c.\n"); + } else { + printf( "No value given for -c.\n"); + printf(help, prog_path); + exit(EXIT_FAILURE); + } + else if (strcmp(*argv,"-v") == 0) + if (--argc) { + s = *(++argv); + if (*s == 'i') + ud_set_vendor(&ud_obj, UD_VENDOR_INTEL); + } else { + printf( "No value given for -v.\n"); + printf(help, prog_path); + exit(EXIT_FAILURE); + } + else if (strcmp(*argv,"-o") == 0) { + if (--argc) { + uint64_t pc = 0; + s = *(++argv); + if (sscanf(s, "%" FMT "x", &pc) == 0) + printf( "Invalid value given for -o.\n"); + ud_set_pc(&ud_obj, pc); + } else { + printf( "No value given for -o.\n"); + printf(help, prog_path); + exit(EXIT_FAILURE); + } + } else if ( strcmp( *argv, "--version" ) == 0 ) { + printf( "%s\n", PACKAGE_STRING ); + exit(0); + } else if((*argv)[0] == '-') { + printf( "Invalid option %s.\n", *argv); + printf(help, prog_path); + exit(EXIT_FAILURE); + } else { + static int i = 0; + s = *argv; + if (i) { + printf( "Multiple files specified.\n"); + exit(EXIT_FAILURE); + } else i = 1; + if ((fptr = fopen(s, "rb")) == NULL) { + printf( "Failed to open file: %s.\n", s); + exit(EXIT_FAILURE); + } + } + argv++; + } + + if (o_do_x) + ud_set_input_hook(&ud_obj, input_hook_x); + else ud_set_input_hook(&ud_obj, input_hook_file); + + if (o_skip) { + o_count += o_skip; + ud_input_skip(&ud_obj, o_skip); + } + + /* disassembly loop */ + while (ud_disassemble(&ud_obj)) { + if (o_do_off) + {printf("%016" FMT "x ", ud_insn_off(&ud_obj)); + fprintf(stream,"%016" FMT "x ", ud_insn_off(&ud_obj));} + if (o_do_hex) { + char* hex1, *hex2; + char c; + hex1 = ud_insn_hex(&ud_obj); + hex2 = hex1 + 16; + c = hex1[16]; + hex1[16] = 0; + printf("%-16s %-24s", hex1, ud_insn_asm(&ud_obj)); + fprintf(stream,"%-16s %-24s", hex1, ud_insn_asm(&ud_obj)); + hex1[16] = c; + if (strlen(hex1) > 16) { + printf("\n"); + if (o_do_off) + printf("%15s -", ""); + printf("%-16s", hex2); + } + } + else {printf(" %-24s", ud_insn_asm(&ud_obj)); + fprintf(stream," %-24s", ud_insn_asm(&ud_obj)); +} + + printf("\n"); + fprintf(stream,"\n"); + } + + exit(EXIT_SUCCESS); +fclose( stream ); + + return 0; +} + +int input_hook_x(ud_t* u) +{ + unsigned int c, i; + + if (o_do_count) { + if (! o_count) + return UD_EOI; + else --o_count; + } + + i = fscanf(fptr, "%x", &c); + + if (i == EOF) + return UD_EOI; + if (i == 0) { + printf( "Error: Invalid input, should be in hexadecimal form (8-bit).\n"); + return UD_EOI; + } + if (c > 0xFF) + printf( "Warning: Casting non-8-bit input (%x), to %x.\n", c, c & 0xFF); + return (int) (c & 0xFF); +} + +int input_hook_file(ud_t* u) +{ + int c; + + if (o_do_count) { + if (! o_count) { + return -1; + } else o_count -- ; + } + + if ((c = fgetc(fptr)) == EOF) + return UD_EOI; + return c; +} diff --git a/contrib/other/udcli/udis86.h b/contrib/other/udcli/udis86.h new file mode 100755 index 0000000000..2228778fdf --- /dev/null +++ b/contrib/other/udcli/udis86.h @@ -0,0 +1,16 @@ +/* ----------------------------------------------------------------------------- + * udis86.h + * + * Copyright (c) 2002, 2003, 2004 Vivek Mohan + * All rights reserved. See (LICENSE) + * ----------------------------------------------------------------------------- + */ + +#ifndef UDIS86_H +#define UDIS86_H + +#include "libudis86/types.h" +#include "libudis86/extern.h" +#include "libudis86/itab.h" + +#endif