コード例 #1
0
ファイル: test_structures.py プロジェクト: BruceYi/okl4
 def test_expected(self):
     for file_name in elf_ph_expected:
         f = File(file_name, "rb")
         ident = ElfIdentification()
         ident.fromdata(f.get_data(0, 16))
         if ident.wordsize == 32:
             data = f.get_data(0, Elf32Header.size())
             header = Elf32Header()
             PhClass = Elf32ProgramHeader
             header.fromdata(data)
         elif ident.wordsize == 64:
             data = f.get_data(0, Elf64Header.size())
             header = Elf64Header()
             header.fromdata(data)
             PhClass = Elf64ProgramHeader
         else:
             raise "Problem"
         self.assertEqual(header.e_phnum, len(elf_ph_expected[file_name]))
         entsize = header.e_phentsize
         wordsize = ident.wordsize
         hdr = ELF_HEADER_CLASSES[wordsize]()
         for i in range(header.e_phnum):
             ph_expect = elf_ph_expected[file_name][i]
             ph_data = f.get_data(header.e_phoff + (i * entsize), entsize)
             ph = PhClass(header.ident.endianess)
             ph.fromdata(ph_data, hdr)
             self.assertEqual(ph.todata(), ph_data)
             self.assertEqual(ph.p_type, ph_expect[0])
             self.assertEqual(ph.p_offset, ph_expect[1])
             self.assertEqual(ph.p_vaddr, ph_expect[2])
             self.assertEqual(ph.p_paddr, ph_expect[3])
             self.assertEqual(ph.p_filesz, ph_expect[4])
             self.assertEqual(ph.p_memsz, ph_expect[5])
             self.assertEqual(ph.p_flags, ph_expect[6])
             self.assertEqual(ph.p_align, ph_expect[7])
コード例 #2
0
ファイル: test_structures.py プロジェクト: BruceYi/okl4
 def setUp(self):
     self.elf_headers = {}
     for file_name in elf_files:
         f = File(file_name, "rb")
         ident = ElfIdentification()
         ident.fromdata(f.get_data(0, 16))
         if ident.wordsize == 32:
             data = f.get_data(0, Elf32Header.size())
             header = Elf32Header()
             header.fromdata(data)
         elif ident.wordsize == 64:
             data = f.get_data(0, Elf64Header.size())
             header = Elf64Header()
             header.fromdata(data)
         else:
             raise "Problem"
         self.elf_headers[file_name] = (header, data)
     self.bogus_data_size = f.get_data(0, Elf32Header.size() - 1)
コード例 #3
0
ファイル: test_structures.py プロジェクト: BruceYi/okl4
    def setUp(self):
        f = File("data/arm_exec", "rb")
        self.valid_ident_data = f.get_data(0, 16)

        self.valid_32_little = copy_array(self.valid_ident_data)
        self.valid_32_little_ident = ElfIdentification()
        self.valid_32_little_ident.fromdata(self.valid_32_little)

        self.valid_64_little = copy_array(self.valid_ident_data)
        self.valid_64_little[ElfIdentification.EI_CLASS] = ELFCLASS64
        self.valid_64_little_ident = ElfIdentification()
        self.valid_64_little_ident.fromdata(self.valid_64_little)

        self.valid_32_big = copy_array(self.valid_ident_data)
        self.valid_32_big[ElfIdentification.EI_DATA] = ELFDATA2MSB
        self.valid_32_big_ident = ElfIdentification()
        self.valid_32_big_ident.fromdata(self.valid_32_big)

        self.valid_64_big = copy_array(self.valid_ident_data)
        self.valid_64_big[ElfIdentification.EI_CLASS] = ELFCLASS64
        self.valid_64_big[ElfIdentification.EI_DATA] = ELFDATA2MSB
        self.valid_64_big_ident = ElfIdentification()
        self.valid_64_big_ident.fromdata(self.valid_64_big)

        self.bogus_size = f.get_data(0, 10)

        self.bogus_magic = copy_array(self.valid_ident_data)
        self.bogus_magic[0] = 0

        self.bogus_data = copy_array(self.valid_ident_data)
        self.bogus_data[ElfIdentification.EI_DATA] = 0

        self.bogus_class = copy_array(self.valid_ident_data)
        self.bogus_class[ElfIdentification.EI_CLASS] = 0

        self.unknown_class = copy_array(self.valid_ident_data)
        self.unknown_class[ElfIdentification.EI_CLASS] = 200
        self.unknown_class_ident = ElfIdentification()
        self.unknown_class_ident.fromdata(self.unknown_class)

        self.unknown_data = copy_array(self.valid_ident_data)
        self.unknown_data[ElfIdentification.EI_DATA] = 200
        self.unknown_data_ident = ElfIdentification()
        self.unknown_data_ident.fromdata(self.unknown_data)
コード例 #4
0
ファイル: test_structures.py プロジェクト: saif1413/L4OS
    def setUp(self):
        f = File("data/arm_exec", "rb")
        self.valid_ident_data = f.get_data(0, 16)

        self.valid_32_little = copy_array(self.valid_ident_data)
        self.valid_32_little_ident = ElfIdentification()
        self.valid_32_little_ident.fromdata(self.valid_32_little)

        self.valid_64_little = copy_array(self.valid_ident_data)
        self.valid_64_little[ElfIdentification.EI_CLASS] = ELFCLASS64
        self.valid_64_little_ident = ElfIdentification()
        self.valid_64_little_ident.fromdata(self.valid_64_little)

        self.valid_32_big = copy_array(self.valid_ident_data)
        self.valid_32_big[ElfIdentification.EI_DATA] = ELFDATA2MSB
        self.valid_32_big_ident = ElfIdentification()
        self.valid_32_big_ident.fromdata(self.valid_32_big)

        self.valid_64_big = copy_array(self.valid_ident_data)
        self.valid_64_big[ElfIdentification.EI_CLASS] = ELFCLASS64
        self.valid_64_big[ElfIdentification.EI_DATA] = ELFDATA2MSB
        self.valid_64_big_ident = ElfIdentification()
        self.valid_64_big_ident.fromdata(self.valid_64_big)

        self.bogus_size = f.get_data(0, 10)

        self.bogus_magic = copy_array(self.valid_ident_data)
        self.bogus_magic[0] = 0

        self.bogus_data = copy_array(self.valid_ident_data)
        self.bogus_data[ElfIdentification.EI_DATA] = 0

        self.bogus_class = copy_array(self.valid_ident_data)
        self.bogus_class[ElfIdentification.EI_CLASS] = 0

        self.unknown_class = copy_array(self.valid_ident_data)
        self.unknown_class[ElfIdentification.EI_CLASS] = 200
        self.unknown_class_ident = ElfIdentification()
        self.unknown_class_ident.fromdata(self.unknown_class)

        self.unknown_data = copy_array(self.valid_ident_data)
        self.unknown_data[ElfIdentification.EI_DATA] = 200
        self.unknown_data_ident = ElfIdentification()
        self.unknown_data_ident.fromdata(self.unknown_data)
コード例 #5
0
ファイル: test_core.py プロジェクト: paromix/okl4
 def test_null_elf(self):
     for endianess, wordsize, expectedsize in \
         [ ('<', 32, 52), ('>', 32, 52),
           ('<', 64, 64), ('>', 64, 64) ]:
         ef = UnpreparedElfFile()
         self.assertEqual(len(ef.sections), 1)
         self.assertEqual(ef.has_segments(), False)
         ef = ef.prepare(wordsize, endianess)
         f = File("test.elf", "wb")
         data = ef.todata()
         for offset, _dat in data:
             f.seek(offset)
             _dat.tofile(f)
         f.flush()
         self.assertEqual(f.size(), expectedsize)
         f.close()
         os.remove("test.elf")
コード例 #6
0
ファイル: test_structures.py プロジェクト: saif1413/L4OS
 def test_expected(self):
     for file_name in elf_ph_expected:
         f = File(file_name, "rb")
         ident = ElfIdentification()
         ident.fromdata(f.get_data(0, 16))
         if ident.wordsize == 32:
             data = f.get_data(0, Elf32Header.size())
             header = Elf32Header()
             PhClass = Elf32ProgramHeader
             header.fromdata(data)
         elif ident.wordsize == 64:
             data = f.get_data(0, Elf64Header.size())
             header = Elf64Header()
             header.fromdata(data)
             PhClass = Elf64ProgramHeader
         else:
             raise "Problem"
         self.assertEqual(header.e_phnum, len(elf_ph_expected[file_name]))
         entsize = header.e_phentsize
         wordsize = ident.wordsize
         hdr = ELF_HEADER_CLASSES[wordsize]()
         for i in range(header.e_phnum):
             ph_expect = elf_ph_expected[file_name][i]
             ph_data = f.get_data(header.e_phoff + (i * entsize), entsize)
             ph = PhClass(header.ident.endianess)
             ph.fromdata(ph_data, hdr)
             self.assertEqual(ph.todata(), ph_data)
             self.assertEqual(ph.p_type, ph_expect[0])
             self.assertEqual(ph.p_offset, ph_expect[1])
             self.assertEqual(ph.p_vaddr, ph_expect[2])
             self.assertEqual(ph.p_paddr, ph_expect[3])
             self.assertEqual(ph.p_filesz, ph_expect[4])
             self.assertEqual(ph.p_memsz, ph_expect[5])
             self.assertEqual(ph.p_flags, ph_expect[6])
             self.assertEqual(ph.p_align, ph_expect[7])
コード例 #7
0
ファイル: test_core.py プロジェクト: gapry/L4OS
 def test_null_elf(self):
     for endianess, wordsize, expectedsize in [("<", 32, 52), (">", 32, 52), ("<", 64, 64), (">", 64, 64)]:
         ef = UnpreparedElfFile()
         self.assertEqual(len(ef.sections), 1)
         self.assertEqual(ef.has_segments(), False)
         ef = ef.prepare(wordsize, endianess)
         f = File("test.elf", "wb")
         data = ef.todata()
         for offset, _dat in data:
             f.seek(offset)
             _dat.tofile(f)
         f.flush()
         self.assertEqual(f.size(), expectedsize)
         f.close()
         os.remove("test.elf")
コード例 #8
0
ファイル: test_structures.py プロジェクト: BruceYi/okl4
 def test_expected(self):
     for file_name in elf_sh_expected:
         f = File(file_name, "rb")
         ident = ElfIdentification()
         ident.fromdata(f.get_data(0, 16))
         wordsize = ident.wordsize
         hdr = ELF_HEADER_CLASSES[wordsize]()
         if ident.wordsize == 32:
             data = f.get_data(0, Elf32Header.size())
             header = Elf32Header()
             ShClass = Elf32SectionHeader
             header.fromdata(data)
         elif ident.wordsize == 64:
             data = f.get_data(0, Elf64Header.size())
             header = Elf64Header()
             header.fromdata(data)
             ShClass = Elf64SectionHeader
         else:
             raise "Problem"
         self.assertEqual(header.e_shnum, len(elf_sh_expected[file_name]))
         entsize = header.e_shentsize
         for i in range(header.e_shnum):
             sh_expect = elf_sh_expected[file_name][i]
             sh_data = f.get_data(header.e_shoff + (i * entsize), entsize)
             sh = ShClass(header.ident.endianess)
             sh.fromdata(sh_data, hdr)
             self.assertEqual(sh.todata(), sh_data)
             self.assertEqual(sh.sh_name, sh_expect[0])
             self.assertEqual(sh.sh_type, sh_expect[1])
             self.assertEqual(sh.sh_addr, sh_expect[2])
             self.assertEqual(sh.sh_offset, sh_expect[3])
             self.assertEqual(sh.sh_size, sh_expect[4])
             self.assertEqual(sh.sh_entsize, sh_expect[5])
             self.assertEqual(sh.sh_flags, sh_expect[6])
             self.assertEqual(sh.sh_link, sh_expect[7])
             self.assertEqual(sh.sh_info, sh_expect[8])
             self.assertEqual(sh.sh_addralign, sh_expect[9])
コード例 #9
0
ファイル: modify.py プロジェクト: openbox00/oktest
def write_binary_file(sections, filename):
    """
    Write a list of sections to a file.
    'sections' is a list of tuples, the first element is a key:
    If the key is "section", the second element is an elf section
    If the key is "padding", the second element is amount to pad
    """

    # We write out the ByteArrays
    outfile = File(filename, "wb", 0)
    #pos = 0

    for (tag, section) in sections:
        if tag == "section":
            outfile.write(section.get_data())
        elif tag == "segment padding" or tag == "section padding":
            outfile.seek(section, 1)

    outfile.close()
    # For compatibility with objcopy, finally make the file "rwxr-xr-x".
    chmod(filename, (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
コード例 #10
0
ファイル: test_structures.py プロジェクト: saif1413/L4OS
 def setUp(self):
     self.elf_headers = {}
     for file_name in elf_files:
         f = File(file_name, "rb")
         ident = ElfIdentification()
         ident.fromdata(f.get_data(0, 16))
         if ident.wordsize == 32:
             data = f.get_data(0, Elf32Header.size())
             header = Elf32Header()
             header.fromdata(data)
         elif ident.wordsize == 64:
             data = f.get_data(0, Elf64Header.size())
             header = Elf64Header()
             header.fromdata(data)
         else:
             raise "Problem"
         self.elf_headers[file_name] = (header, data)
     self.bogus_data_size = f.get_data(0, Elf32Header.size() - 1)
コード例 #11
0
ファイル: test_structures.py プロジェクト: saif1413/L4OS
 def test_expected(self):
     for file_name in elf_sh_expected:
         f = File(file_name, "rb")
         ident = ElfIdentification()
         ident.fromdata(f.get_data(0, 16))
         wordsize = ident.wordsize
         hdr = ELF_HEADER_CLASSES[wordsize]()
         if ident.wordsize == 32:
             data = f.get_data(0, Elf32Header.size())
             header = Elf32Header()
             ShClass = Elf32SectionHeader
             header.fromdata(data)
         elif ident.wordsize == 64:
             data = f.get_data(0, Elf64Header.size())
             header = Elf64Header()
             header.fromdata(data)
             ShClass = Elf64SectionHeader
         else:
             raise "Problem"
         self.assertEqual(header.e_shnum, len(elf_sh_expected[file_name]))
         entsize = header.e_shentsize
         for i in range(header.e_shnum):
             sh_expect = elf_sh_expected[file_name][i]
             sh_data = f.get_data(header.e_shoff + (i * entsize), entsize)
             sh = ShClass(header.ident.endianess)
             sh.fromdata(sh_data, hdr)
             self.assertEqual(sh.todata(), sh_data)
             self.assertEqual(sh.sh_name, sh_expect[0])
             self.assertEqual(sh.sh_type, sh_expect[1])
             self.assertEqual(sh.sh_addr, sh_expect[2])
             self.assertEqual(sh.sh_offset, sh_expect[3])
             self.assertEqual(sh.sh_size, sh_expect[4])
             self.assertEqual(sh.sh_entsize, sh_expect[5])
             self.assertEqual(sh.sh_flags, sh_expect[6])
             self.assertEqual(sh.sh_link, sh_expect[7])
             self.assertEqual(sh.sh_info, sh_expect[8])
             self.assertEqual(sh.sh_addralign, sh_expect[9])
コード例 #12
0
 def init_and_prepare_from_filename(self, filename):
     """This method returns a new Elf file object initialised from
     a file."""
     return self.init_and_prepare_from_file(File(filename, "rb"))
コード例 #13
0
 def to_filename(self, filename):
     """This method outputs the ELf file to a given filename."""
     self.to_file(File(filename, "wb"))
コード例 #14
0
ファイル: test_structures.py プロジェクト: saif1413/L4OS
    def test_against_readelf(self):
        if not have_readelf:
            return
        for file_name in elf_ph_expected:
            f = File(file_name, "rb")
            ident = ElfIdentification()
            ident.fromdata(f.get_data(0, 16))
            wordsize = ident.wordsize
            hdr = ELF_HEADER_CLASSES[wordsize]()
            lines = os.popen("readelf -l %s" % file_name).readlines()

            good_lines = []

            if ident.wordsize == 32:
                data = f.get_data(0, Elf32Header.size())
                header = Elf32Header()
                PhClass = Elf32ProgramHeader
                header.fromdata(data)

                got_start = False
                for line in lines:
                    if got_start:
                        if line.strip() == "":
                            break
                        good_lines.append(line)
                    else:
                        # Looking for start
                        if line.startswith("  Type"):
                            got_start = True
            elif ident.wordsize == 64:
                data = f.get_data(0, Elf64Header.size())
                header = Elf64Header()
                header.fromdata(data)
                PhClass = Elf64ProgramHeader

                got_start = False
                for line in lines:
                    if got_start:
                        if line.strip() == "":
                            break
                        if not line.strip().startswith("["):
                            good_lines.append(line)
                    else:
                        # Looking for start
                        if line.strip().startswith("FileSiz"):
                            got_start = True
                merged_lines = []
                i = 0
                while i < len(good_lines):
                    merged_lines.append(good_lines[i] + good_lines[i + 1])
                    i += 2
                good_lines = merged_lines

            entsize = header.e_phentsize
            for i in range(header.e_phnum):
                ph_data = f.get_data(header.e_phoff + (i * entsize), entsize)
                ph = PhClass(header.ident.endianess)
                ph.fromdata(ph_data, hdr)

                out = StringIO.StringIO()
                ph.output(out)
                out.getvalue()
                self.assertEqual(out.getvalue(), good_lines[i])
コード例 #15
0
ファイル: test_structures.py プロジェクト: BruceYi/okl4
    def test_against_readelf(self):
        if not have_readelf:
            return 
        for file_name in elf_sh_files:
            f = File(file_name, "rb")
            ident = ElfIdentification()
            ident.fromdata(f.get_data(0, 16))
            wordsize = ident.wordsize
            hdr = ELF_HEADER_CLASSES[wordsize]()
            lines = os.popen("readelf -S %s" % file_name).readlines()

            good_lines = []

            if ident.wordsize == 32:
                data = f.get_data(0, Elf32Header.size())
                header = Elf32Header()
                ShClass = Elf32SectionHeader
                header.fromdata(data)
                
                got_start = False
                for line in lines:
                    if got_start:
                        if line.strip().startswith("Key to Flags"):
                            break
                        good_lines.append(line)
                    else:
                        # Looking for start
                        if line.strip().startswith("[Nr]"):
                            got_start = True
            elif ident.wordsize == 64:
                data = f.get_data(0, Elf64Header.size())
                header = Elf64Header()
                header.fromdata(data)
                ShClass = Elf64SectionHeader

                got_start = False
                for line in lines:
                    if got_start:
                        if line.strip().startswith("Key"):
                            break
                        good_lines.append(line)
                    else:
                        # Looking for start
                        if line.strip().startswith("Siz"):
                            got_start = True
                merged_lines = []
                i = 0
                while i < len(good_lines):
                    merged_lines.append(good_lines[i] + good_lines[i+1])
                    i += 2
                good_lines = merged_lines

            good_lines = [line[25:] for line in good_lines]
            entsize = header.e_shentsize
            for i in range(header.e_shnum):
                sh_data = f.get_data(header.e_shoff + (i * entsize), entsize)
                sh = ShClass(header.ident.endianess)
                sh.fromdata(sh_data, hdr)

                out = StringIO.StringIO()
                sh.output(out)
                out.getvalue()
                self.assertEqual(out.getvalue(), good_lines[i])
コード例 #16
0
ファイル: test_structures.py プロジェクト: saif1413/L4OS
 def test_bogus_data64_size(self):
     ph = Elf32SectionHeader("<")
     f = File("data/arm_exec", "rb")
     self.assertRaises(elf.structures.ElfFormatError, ph.fromdata,
                       f.get_data(0, 16), ph)
コード例 #17
0
ファイル: test_structures.py プロジェクト: BruceYi/okl4
 def test_bogus_data64_size(self):
     ph = Elf32SectionHeader("<")
     f = File("data/arm_exec", "rb")
     self.assertRaises(elf.structures.ElfFormatError, ph.fromdata, f.get_data(0, 16), ph)
コード例 #18
0
ファイル: test_structures.py プロジェクト: BruceYi/okl4
    def test_against_readelf(self):
        if not have_readelf:
            return 
        for file_name in elf_ph_expected:
            f = File(file_name, "rb")
            ident = ElfIdentification()
            ident.fromdata(f.get_data(0, 16))
            wordsize = ident.wordsize
            hdr = ELF_HEADER_CLASSES[wordsize]()
            lines = os.popen("readelf -l %s" % file_name).readlines()

            good_lines = []

            if ident.wordsize == 32:
                data = f.get_data(0, Elf32Header.size())
                header = Elf32Header()
                PhClass = Elf32ProgramHeader
                header.fromdata(data)
                
                got_start = False
                for line in lines:
                    if got_start:
                        if line.strip() == "":
                            break
                        good_lines.append(line)
                    else:
                        # Looking for start
                        if line.startswith("  Type"):
                            got_start = True
            elif ident.wordsize == 64:
                data = f.get_data(0, Elf64Header.size())
                header = Elf64Header()
                header.fromdata(data)
                PhClass = Elf64ProgramHeader

                got_start = False
                for line in lines:
                    if got_start:
                        if line.strip() == "":
                            break
                        if not line.strip().startswith("["):
                            good_lines.append(line)
                    else:
                        # Looking for start
                        if line.strip().startswith("FileSiz"):
                            got_start = True
                merged_lines = []
                i = 0
                while i < len(good_lines):
                    merged_lines.append(good_lines[i] + good_lines[i+1])
                    i += 2
                good_lines = merged_lines

            entsize = header.e_phentsize
            for i in range(header.e_phnum):
                ph_data = f.get_data(header.e_phoff + (i * entsize), entsize)
                ph = PhClass(header.ident.endianess)
                ph.fromdata(ph_data, hdr)

                out = StringIO.StringIO()
                ph.output(out)
                out.getvalue()
                self.assertEqual(out.getvalue(), good_lines[i])
コード例 #19
0
ファイル: test_structures.py プロジェクト: saif1413/L4OS
    def test_against_readelf(self):
        if not have_readelf:
            return
        for file_name in elf_sh_files:
            f = File(file_name, "rb")
            ident = ElfIdentification()
            ident.fromdata(f.get_data(0, 16))
            wordsize = ident.wordsize
            hdr = ELF_HEADER_CLASSES[wordsize]()
            lines = os.popen("readelf -S %s" % file_name).readlines()

            good_lines = []

            if ident.wordsize == 32:
                data = f.get_data(0, Elf32Header.size())
                header = Elf32Header()
                ShClass = Elf32SectionHeader
                header.fromdata(data)

                got_start = False
                for line in lines:
                    if got_start:
                        if line.strip().startswith("Key to Flags"):
                            break
                        good_lines.append(line)
                    else:
                        # Looking for start
                        if line.strip().startswith("[Nr]"):
                            got_start = True
            elif ident.wordsize == 64:
                data = f.get_data(0, Elf64Header.size())
                header = Elf64Header()
                header.fromdata(data)
                ShClass = Elf64SectionHeader

                got_start = False
                for line in lines:
                    if got_start:
                        if line.strip().startswith("Key"):
                            break
                        good_lines.append(line)
                    else:
                        # Looking for start
                        if line.strip().startswith("Siz"):
                            got_start = True
                merged_lines = []
                i = 0
                while i < len(good_lines):
                    merged_lines.append(good_lines[i] + good_lines[i + 1])
                    i += 2
                good_lines = merged_lines

            good_lines = [line[25:] for line in good_lines]
            entsize = header.e_shentsize
            for i in range(header.e_shnum):
                sh_data = f.get_data(header.e_shoff + (i * entsize), entsize)
                sh = ShClass(header.ident.endianess)
                sh.fromdata(sh_data, hdr)

                out = StringIO.StringIO()
                sh.output(out)
                out.getvalue()
                self.assertEqual(out.getvalue(), good_lines[i])