'Load all ROP gadgets for the selected ELF files'

def __load(self):

    pop = re.compile('^pop    (.{3})')
    add = re.compile('^add    .sp,    (\\S+)$')
    ret = re.compile('^ret$')
    leave = re.compile('^leave$')
    int80 = re.compile('int    +0x80')
    syscall = re.compile('^syscall$')
    sysenter = re.compile('^sysenter$')
    valid = (lambda insn: any(map((lambda pattern: pattern.match(insn)), [pop, add, ret, leave, int80, syscall, sysenter])))
    argv = sys.argv
    stdout = sys.stdout
    class Wrapper:
        def __init__(self, fd):
            self._fd = fd
        def write(self, s):
            pass
        def __getattr__(self, k):
            return self._fd.__getattribute__(k)
    gadgets = {}
    for elf in self.elfs:
        cache = self.__cache_load(elf)
        if cache:
            gadgets.update(cache)
            continue
        log.info_once(('Loading    gadgets    for    %r' % elf.path))
        try:
            sys.stdout = Wrapper(sys.stdout)
            import ropgadget
            sys.argv = ['ropgadget', '--binary', elf.path, '--only', 'sysenter|syscall|int|add|pop|leave|ret', '--nojop']
            args = ropgadget.args.Args().getArgs()
            core = ropgadget.core.Core(args)
            core.do_binary(elf.path)
            core.do_load(0)
        finally:
            sys.argv = argv
            sys.stdout = stdout
        elf_gadgets = {}
        for gadget in core._Core__gadgets:
            address = ((gadget['vaddr'] - elf.load_addr) + elf.address)
            insns = [g.strip() for g in gadget['gadget'].split(';')]
            if all(map(valid, insns)):
                elf_gadgets[address] = insns
        self.__cache_save(elf, elf_gadgets)
        gadgets.update(elf_gadgets)
    self.gadgets = {}
    self.pivots = {}
    frame_regs = {4: ['ebp', 'esp'], 8: ['rbp', 'rsp']}[context.bytes]
    for (addr, insns) in gadgets.items():
        sp_move = 0
        regs = []
        for insn in insns:
            if pop.match(insn):
                regs.append(pop.match(insn).group(1))
                sp_move += context.bytes
            elif add.match(insn):
                sp_move += int(add.match(insn).group(1), 16)
            elif ret.match(insn):
                sp_move += context.bytes
            elif leave.match(insn):
                sp_move += 9999999999
                regs += frame_regs
        self.gadgets[addr] = Gadget(addr, insns, regs, sp_move)
        if (not (set(['rsp', 'esp']) & set(regs))):
            self.pivots[sp_move] = addr
    leave = self.search(regs=frame_regs, order='regs')
    if (leave and (leave.regs != frame_regs)):
        leave = None
    self.leave = leave
