# Buggy code
def check_elf_built(f):
    has_pi = False
    has_sp = False
    has_pi_fg = {
        3: [8],  # EM_386=3,    R_386_RELATIVE=8,
        62: [8],  # EM_X86_64=62,    R_X86_64_RELATIVE=8,
        40: [23, 3],  # EM_ARM=40,    R_ARM_RELATIVE=23,R_ARM_REL32=3,
        183: [1027, 3],  # EM_AARCH64=183,
        # R_AARCH64_RELATIVE=1027,R_ARM_REL32=3,
        8: [3],  # EM_MIPS=8,    R_MIPS_REL32=3,
    }
    elffile = TinyELFFile(f)
    for i in range(elffile.header["e_shnum"]):
        section_header = elffile.decode_shdr(
            elffile.header["e_shoff"] + i * elffile.header["e_shentsize"]
        )
        sectype = section_header["sh_type"]
        if sectype in (4, 9):  # SHT_RELA=4,SHT_REL=9,
            if section_header["sh_entsize"] > 0:
                siz = section_header["sh_size"] // section_header["sh_entsize"]
                for i in range(siz):
                    elffile.stream.seek(
                        section_header["sh_offset"] + i * section_header["sh_entsize"]
                    )
                    if section_header["sh_type"] == 9:
                        entry = elffile.decode_rel(
                            section_header["sh_offset"]
                            + i * section_header["sh_entsize"]
                        )
                    elif section_header["sh_type"] == 4:
                        entry = elffile.decode_rela(
                            section_header["sh_offset"]
                            + i * section_header["sh_entsize"]
                        )
                    else:
                        continue
                    if entry["r_info_type"] in has_pi_fg.get(
                        elffile.header["e_machine"], []
                    ):
                        if entry["r_info_sym"] == 0:
                            has_pi = True
                            break
    return has_pi, has_sp


# Traceback (FileNotFoundError)
MobSFy Android Instance Traceback (most recent call last): File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 408, in mobsfy_init self.mobsf_agents_setup('frida') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 418, in mobsf_agents_setup self.install_mobsf_ca('install') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 140, in install_mobsf_ca pem = open(get_ca_dir(), 'rb').read() FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Lijoy KFH\\.mitmproxy\\mitmproxy-ca-cert.pem' [ERROR] 12/Apr/2020 18:53:11 – Failed to MobSFy the instance [ERROR] 12/Apr/2020 18:53:11 – Internal Server Error: /android_dynamic/"

# Fixed code
def check_elf_built(f):
    has_pi = False
    has_sp = False
    has_pi_fg = {
        3: [8],  # EM_386=3,    R_386_RELATIVE=8,
        62: [8],  # EM_X86_64=62,    R_X86_64_RELATIVE=8,
        40: [23, 3],  # EM_ARM=40,    R_ARM_RELATIVE=23,R_ARM_REL32=3,
        183: [1027, 3],  # EM_AARCH64=183,
        # R_AARCH64_RELATIVE=1027,R_ARM_REL32=3,
        8: [3],  # EM_MIPS=8,    R_MIPS_REL32=3,
    }
    elffile = TinyELFFile(f)
    for j in range(elffile.header["e_shnum"]):
        section_header = elffile.decode_shdr(
            elffile.header["e_shoff"] + j * elffile.header["e_shentsize"]
        )
        sectype = section_header["sh_type"]
        if sectype in (4, 9):  # SHT_RELA=4,SHT_REL=9,
            if section_header["sh_entsize"] > 0:
                siz = section_header["sh_size"] // section_header["sh_entsize"]
                for i in range(siz):
                    elffile.stream.seek(
                        section_header["sh_offset"] + i * section_header["sh_entsize"]
                    )
                    if section_header["sh_type"] == 9:
                        entry = elffile.decode_rel(
                            section_header["sh_offset"]
                            + i * section_header["sh_entsize"]
                        )
                    elif section_header["sh_type"] == 4:
                        entry = elffile.decode_rela(
                            section_header["sh_offset"]
                            + i * section_header["sh_entsize"]
                        )
                    else:
                        continue
                    if entry["r_info_type"] in has_pi_fg.get(
                        elffile.header["e_machine"], []
                    ):
                        if entry["r_info_sym"] == 0:
                            has_pi = True
                            break
    return has_pi, has_sp