Beispiel #1
0
    def __init__(self, binary):

        self.binary = binary

        self.bfd_file_handle = binary.file_handle()
        filename = self.bfd_file_handle.name

        # we manually fixup the section start address if a base address is set
        baseAddr = self.binary.options.base_address
        if baseAddr:
            baseAddr = int(baseAddr, 16)
        else:
            baseAddr = 0

        # internal BFD object
        assert (binary.options.architecture != UNKNOWN_ARCH)
        self.bfd = bfd.Bfd(filename, binary.options.target,
                           binary.options.architecture)

        self.sections = sorted(list(self.bfd.sections.values()),
                               key=lambda s: s.vma)
        for s in self.sections:
            # manually add in the base address, so the parcels know their real vma
            s.vma += baseAddr

            # iterate over the flags and convert them to boolean attributes
            for f in bfd.FLAGS:
                if f.name in s.flags:
                    setattr(self, f.name,
                            True)  # i.e, section.SEC_ALLOC = True
                else:
                    setattr(self, f.name,
                            False)  # i.e, section.SEC_ALLOC = False
    def __call__(self, parser, namespace, values, option_string=None):
        # Initialize BFD instance and proceed to display the requested
        # information.
        self.bfd = bfd.Bfd()

        self.do_action(parser, namespace, values, option_string)

        global done
        done = True
Beispiel #3
0
def dump(path, target, arch, numLines=None, start=None, end=None):

    print('dumping %s' % path)
    b=bfd.Bfd(path, target, arch)

    print('\nFile: %s' % path)
    print('Arch ID: %d' % b.archId)
    print('Architecture: %s' % b.arch)
    print('Machine: %s' % b.mach)
    print('Target: %s' % b.target)

    print('\nSections:\n')
    print('Index %-32s %-10s %-10s %-10s %s' % ('Name', 'Size', 'VMA', 'Flags', 'Alignment'))
    i = 0
    for sec in list(b.sections.values()):
        print('   %2d %-32s 0x%08x 0x%08x %s 2**%d' % (i, sec.name, sec.size, sec.vma, ', '.join([f.abbrev for f in sec.flags]), sec.alignment))
        #print sec.SEC_ALLOC
        i += 1

    print('\nSymbols:\n')
    print(b.syms_by_name)
    #for sym in b.syms_by_name.values():
    #    print('  0x%08x %s %s' % (sym.value, sym.type, sym.name))

    if '.text' in b.sections:
        sec_name = '.text'
    elif '.app_text' in b.sections:
        sec_name = '.app_text'
    elif '.data' in b.sections:
        sec_name = '.data'
    elif '.sec1' in b.sections:
        sec_name = '.sec1'
    else:
        sec_name = list(b.sections.keys())[0]

    print('\nDisassembly of %s:\n' % sec_name)
    sec = b.sections[sec_name]
    (dis,nextAddr, lineCnt) = b.disassemble(sec, start, end, numLines, funcFmtAddr, funcFmtLine, {})
    print('nextAddr is: 0x%08x\n' % nextAddr)
    print('lineCnt is: 0x%08x\n' % lineCnt)
    print('disassembly is:\n%s' % dis)
    print('Next address to disassemble is: 0x%08x' % nextAddr)
    def __call__(self, parser, namespace, values, option_string=None):
        # Initialize BFD instance and proceed to display the requested
        # information.
        for fd in values:
            try:
                # Create a new BFD from the current file descriptor.
                self.bfd = bfd.Bfd(fd)

                # Display current filename and corresponding architecture.
                print "\n%s:     file format %s\n" % \
                    (self.bfd.filename, self.bfd.architecture)

                # Process the BFD with the user-requested action.
                self.do_action(parser, namespace, values, option_string)

                # Close BFD file and move on.
                self.bfd.close()

                fd.close()

            except BfdException, err:
                print err
            except Exception, err:
                pass
Beispiel #5
0
#!/usr/bin/env python3
import bfd
import sys
import os

filename = '/bin/mkdir'
output = bfd.guess_target_arch(filename)
print(output)

bfd_out = bfd.Bfd(filename, 'elf64-x86-64', 'i386:x86-64')


# Called each time there is an address referenced in an instruction,
#   such as move r0, 0x12345678
#
# We get a chance to format that value however we want.  In the example below,
#
def funcFmtAddr(bfd, addr):
    addrStr = '0x%08x' % addr
    if bfd.syms_by_addr.has_key(addr):
        addrStr += ' <%s>' % bfd.syms_by_addr[addr].name
    return addrStr

def funcFmtLine(addr, rawData, instr, abfd):
    #print 'addr: 0x%08x, instr: %s\n' % (addr, instr)
    ret = ''
    if addr in abfd.syms_by_addr:
        ret += '\n0%08x <%s>:\n' % (addr, abfd.syms_by_addr[addr].name)
    if abfd.bpc > 1 and abfd.endian is bfd.ENDIAN_LITTLE:
        bytes = ''.join(['%02x ' % i for i in reversed(rawData)])
    else: