Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Binutils >= 2.40 #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pybfd3/bfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// Copyright (c) 2013 Groundworks Technologies
//

// In order to use '#' variant of formats, we must define this on python >= 3.10
#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include <stdio.h>
Expand Down
4 changes: 4 additions & 0 deletions pybfd3/bfd_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ enum PYBFD3_DISASM_CALLBACK_RESULT {
};

#endif /* PYBFD3_HEADERS */

#ifndef BFD_VMA_FMT
#define BFD_VMA_FMT PRIx64
#endif
30 changes: 26 additions & 4 deletions pybfd3/opcodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// Copyright (c) 2013 Groundworks Technologies
//

// In order to use '#' variant of formats, we must define this on python >= 3.10
#define PY_SSIZE_T_CLEAN

#include <Python.h>

#include <stdio.h>
Expand Down Expand Up @@ -95,6 +98,17 @@ get_disassemble_function( unsigned long long ull_arch,
return (disassembler_ftype)NULL;
}

#ifdef PYBFD3_LIBBFD_INIT_DISASM_INFO_FOUR_ARGS_SIGNATURE
// bpftrace no operation fprintf for init_disassemble_info
static int fprintf_styled_nop(void *out __attribute__((unused)),
enum disassembler_style s __attribute__((unused)),
const char *fmt __attribute__((unused)),
...)
{
return 0;
}
#endif

//
// Name : initialize_opcodes
//
Expand Down Expand Up @@ -123,8 +137,13 @@ initialize_opcodes()
memset(pdisasm_ptr, 0, sizeof(disassembler_pointer));

// Zero out structure members
init_disassemble_info(&pdisasm_ptr->dinfo, &pdisasm_ptr->sfile,
(fprintf_ftype)__disassemle_printf);
#ifdef PYBFD3_LIBBFD_INIT_DISASM_INFO_FOUR_ARGS_SIGNATURE
init_disassemble_info(&pdisasm_ptr->dinfo, &pdisasm_ptr->sfile,
(fprintf_ftype)__disassemle_printf, fprintf_styled_nop);
#else
init_disassemble_info(&pdisasm_ptr->dinfo, &pdisasm_ptr->sfile,
(fprintf_ftype)__disassemle_printf);
#endif


pdisasm_ptr->pfn_disassemble = NULL;
Expand Down Expand Up @@ -471,8 +490,11 @@ start_smart_disassemble(disassembler_pointer* pdisasm_ptr, unsigned long offset,
offset += n; // update the offset for the next disassemble operation.
disassembled_bytes += n; // keep track of the number of bytes
// processed.

py_result = PyEval_CallObject(callback, py_args);
#if PY_VERSION_HEX >= 0x03090000
py_result = PyObject_Call(callback, py_args, NULL);
#else
py_result = PyEval_CallObject(callback, py_args);
#endif
Py_XDECREF(py_args);

if (!py_result) {
Expand Down
8 changes: 8 additions & 0 deletions pybfd3/test_init_disassemble_info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#define PACKAGE "pybfd3"
#define PACKAGE_VERSION "0.1.5"

#include <dis-asm.h>
int main(void) {
init_disassemble_info(NULL, NULL, NULL);
return 0;
}
16 changes: 13 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,29 @@ def generate_source_files( self ):
self.with_static_binutils == None)

source_bfd_archs_c = generate_supported_architectures_source(supported_archs, supported_machines)
macros = []
print("[+] Testing for print_insn_i386...")
try:
objects = self.compiler.compile(
[os.path.join(PACKAGE_DIR, "test_print_insn_i386.c"), ],
include_dirs = [self.includes,],
)
if len(objects) > 0:
macros = None
else:
if len(objects) == 0:
macros = [("PYBFD3_BFD_GE_2_29", None)]
except:
macros = [("PYBFD3_BFD_GE_2_29", None)]

print("[+] Testing for init_disassemble_info...")
try:
objects = self.compiler.compile(
[os.path.join(PACKAGE_DIR, "test_init_disassemble_info.c"), ],
include_dirs = [self.includes,],
)
if len(objects) == 0:
macros.append(("PYBFD3_LIBBFD_INIT_DISASM_INFO_FOUR_ARGS_SIGNATURE", None))
except:
macros.append(("PYBFD3_LIBBFD_INIT_DISASM_INFO_FOUR_ARGS_SIGNATURE", None))

print("[+] Generating .C files...")
gen_file = os.path.join(PACKAGE_DIR, "gen_bfd_archs.c")
with io.open(gen_file, "w+") as fd:
Expand Down