<commit_before>#!/usr/bin/env python

import pysex
import idalink
import z3
import subprocess
import sys
import logging
from elftools.elf.elffile import ELFFile
from elftools.elf.dynamic import DynamicSection, DynamicSegment
from elftools.common.py3compat import bytes2str
import pdb

logging.basicConfig()
l = logging.getLogger("loader")
l.setLevel(logging.DEBUG)

def load_binary(ida):
    mem = pysex.s_memory.Memory()
    dst = z3.BitVec('dst', mem.get_bit_address())

    # dynamic stuff
    dobj = get_dynamicOBJ(ida.get_filename())
    print dobj
    # static stuff
    for k in ida.mem.keys():
        bit_len = 8 #idalink handles bytes
        mem.store(dst, z3.BitVecVal(ida.mem[k], bit_len), [dst == k], 5)


    return mem

def load_ELF(mem, filename):
    elf_file = ELFFile(open(filename, "rb"))

    for section in elf_file.iter_sections():
        if not isinstance(section, DynamicSection):
            continue

        for tag in section.iter_tags():
            if tag.entry.d_tag == 'DT_NEEDED':
                l.info("Shared library: [%s]" % bytes2str(tag.needed))

def get_dynamicOBJ(filename):
    p_nm = subprocess.Popen(["nm", "-D", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result_nm = p_nm.stdout.readlines()
    p_ldd = subprocess.Popen(["ldd", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result_ldd = p_ldd.stdout.readlines()

    dyn = {}

    for nm_out in result_nm:
        sym_entry = nm_out.split()
        if len(sym_entry) >= 2 and sym_entry[0 if len(sym_entry) == 2 else 1] == "U":
            sym = sym_entry[1 if len(sym_entry) == 2 else 2]
            for lld_out in result_ldd:
                lib_entry = lld_out.split()
                if "=>" in lld_out and len(lib_entry) > 3: # virtual library
                    lib = lib_entry[2]
                    ls_nm = subprocess.Popen(["nm", "-D", lib], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    result_lsnm = ls_nm.stdout.readlines()
                    for ls_nm_out in result_lsnm:
                        lib_symbol = ls_nm_out.split()
                        if len(lib_symbol) >= 2 and lib_symbol[0 if len(lib_symbol) == 2 else 1] == "T":
                            if sym == lib_symbol[1 if len(lib_symbol) == 2 else 2]:
                                dyn[sym] = lib

    return dyn
<commit_msg>getting the function address on the shared library<commit_after>#!/usr/bin/env python

import pysex
import idalink
import z3
import subprocess
import sys
import logging
from elftools.elf.elffile import ELFFile
from elftools.elf.dynamic import DynamicSection, DynamicSegment
from elftools.common.py3compat import bytes2str
import pdb

logging.basicConfig()
l = logging.getLogger("loader")
l.setLevel(logging.DEBUG)

def load_binary(ida):
    mem = pysex.s_memory.Memory()
    dst = z3.BitVec('dst', mem.get_bit_address())

    # dynamic stuff
    dobj = get_dynamicOBJ(ida.get_filename())
    print dobj
    # static stuff
    for k in ida.mem.keys():
        bit_len = 8 #idalink handles bytes

        mem.store(dst, z3.BitVecVal(ida.mem[k], bit_len), [dst == k], 5)

    return mem

def load_ELF(mem, filename):
    elf_file = ELFFile(open(filename, "rb"))

    for section in elf_file.iter_sections():
        if not isinstance(section, DynamicSection):
            continue

        for tag in section.iter_tags():
            if tag.entry.d_tag == 'DT_NEEDED':
                l.info("Shared library: [%s]" % bytes2str(tag.needed))

# o.s. has to provide "nm" and "ldd" commands
def get_dynamicOBJ(filename):
    p_nm = subprocess.Popen(["nm", "-D", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result_nm = p_nm.stdout.readlines()
    p_ldd = subprocess.Popen(["ldd", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result_ldd = p_ldd.stdout.readlines()
    dyn = {}

    for nm_out in result_nm:
        sym_entry = nm_out.split()
        if len(sym_entry) >= 2 and sym_entry[0 if len(sym_entry) == 2 else 1] == "U":
            sym = sym_entry[1 if len(sym_entry) == 2 else 2]
            found = False
            for lld_out in result_ldd:
                lib_entry = lld_out.split()
                if "=>" in lld_out and len(lib_entry) > 3: # virtual library
                    lib = lib_entry[2]
                    ls_nm = subprocess.Popen(["nm", "-D", lib], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    result_lsnm = ls_nm.stdout.readlines()
                    for ls_nm_out in result_lsnm:
                        lib_symbol = ls_nm_out.split()
                        if len(lib_symbol) >= 2 and lib_symbol[0 if len(lib_symbol) == 2 else 1] == "T":
                            if sym == lib_symbol[1 if len(lib_symbol) == 2 else 2]:
                                dyn[sym] = {}
                                dyn[sym]['lib'] = lib
                                dyn[sym]['addr'] = lib_symbol[0] if len(lib_symbol) == 3 else 0
                                found = True
            if found == False:
                l.error("Extern function has not been matched with a valid shared libraries")
                pdb.set_trace()

    return dyn

{'fraction_non_alphanumeric': 0.11306193324329539, 'fraction_numerical': 0.009839861084314104, 'mean_word_length': 2.2893401015228427, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5159013', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1643, 'n_words': 414}