def test_set_name_offset(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(hasattr(sect, "name_offset"), False) # FIMXE: is this really what we want to check sect = sect.prepare(0, 0, 5) self.assertEqual(sect.name_offset, 5) sect.name_offset = 25 self.assertEqual(sect.name_offset, 25)
def test_copy_into(self): elf_from = UnpreparedElfFile() elf_to = UnpreparedElfFile() sect = BaseElfSection(elf_from, "test") # FIXME #self.assertRaises(NotImplementedError, sect.copy_into, elf_to) sect = UnpreparedElfSection(elf_from, "test") new_sect = sect.copy_into(elf_to) self.assertEquals(sect.name, new_sect.name) prep_sect = sect.prepare(0, 0, 0) # FIXME #self.assertRaises(NotImplementedError, prep_sect.copy_into, elf_to) sect = UnpreparedElfSection(elf_from, "test", SHT_NOBITS) new_sect = sect.copy_into(elf_to) self.assertEquals(sect.name, new_sect.name) sect = UnpreparedElfStringTable(elf_from, "string") strings = ["foo", "bar", "baz"] for string in strings: sect.add_string(string) new_sect = sect.copy_into(elf_to) for i in range(len(strings)): self.assertEquals(sect.get_string_idx(i), new_sect.get_string_idx(i))
def test_get_link(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(sect.link, None) sect2 = UnpreparedElfSection(None, "test_link", link = sect) self.assertEqual(sect2.link, sect) sect3 = self.assertRaises(InvalidArgument, UnpreparedElfSection, "test_link", link = 80)
def test_replace_section(self): elf_from = UnpreparedElfFile() elf_to = UnpreparedElfFile() seg = SectionedElfSegment(elf_from, sections=[UnpreparedElfSection(elf_from, "test")]) old_section = seg.get_sections()[0] new_section = UnpreparedElfSection(elf_to, "new") seg.replace_section(old_section, new_section) self.assertEqual(seg.get_sections(), [new_section]) new_seg = seg.copy_into(elf_to) self.assertRaises(InvalidArgument, seg.replace_section, None, new_section)
def test_add_symbols(self): ef = UnpreparedElfFile() sect = UnpreparedElfSection(ef, "test_sect") symtab = UnpreparedElfSymbolTable(ef, ".symtab") for name in ["foo", "bar"]: symtab.add_symbol(ElfSymbol(name, sect)) ef = UnpreparedElfFile() sect = UnpreparedElfSection(ef, "test_ect") strtab = UnpreparedElfStringTable(ef, ".strtab") symtab = UnpreparedElfSymbolTable(ef, ".symtab", link=strtab) for name in ["foo", "bar"]: symtab.add_symbol(ElfSymbol(name, sect))
def __init__(self, attrs, elf, _, pools, kernel_seg): ImageKernelHeap.__init__(self, attrs, pools) self.segment = None self.kernel_segment = kernel_seg data = self.attrs.data = ByteArray([]) if attrs.size is not None and len(data) < self.attrs.size: data.extend([0] * (self.attrs.size - len(data))) self.attrs.size = data.buffer_info()[1] * data.itemsize section_name = "kernel.heap" sect = UnpreparedElfSection(elf, section_name, SHT_PROGBITS, self.attrs.virt_addr, data=data, flags=SHF_WRITE | SHF_ALLOC) elf.add_section(sect) self.segment = SectionedElfSegment(elf, PT_LOAD, self.attrs.virt_addr, self.attrs.phys_addr, PF_R | PF_W, self.attrs.align, sections=[sect]) elf.add_segment(self.segment)
def test_get_section_header(self): # FIXME: This test uses broken semantics now that elf sections can't # live on their own in the wild sect = UnpreparedElfSection(None, "test") self.assertEqual(hasattr(sect, "get_section_header"), False) """
def _get_segments(self): elf_file = UnpreparedElfFile() sections = [UnpreparedElfSection(elf_file, "test1", data=ByteArray("test1 data")), UnpreparedElfSection(elf_file, "test2", data=ByteArray("test2 data"))] empty_sec_seg = SectionedElfSegment(None) full_sec_seg = SectionedElfSegment(elf_file, sections=sections) head_seg = HeaderElfSegment(None) prep_head_seg = HeaderElfSegment(None) prep_head_seg.prepare(37, PROG_HEADER_SIZE) data = ByteArray("pants") full_data_seg = DataElfSegment(None, vaddr=DATA_BASE, paddr=PHYS_BASE, data=data) nobits_data_seg = DataElfSegment(None, vaddr=DATA_BASE, paddr=PHYS_BASE, data=data, memsz=10) return empty_sec_seg, full_sec_seg, head_seg, prep_head_seg, full_data_seg, nobits_data_seg
def clone_section(self, section, address): """ Returns a new section based on the provided section. This avoids a circular dependency in the elfweaver linker's linker script code. """ return UnpreparedElfSection(self, section.name, section.type, address, None, section.flags, section.addralign, section.info, section.link, section.entsize)
def test_remove_section(self): empty_sec_seg, full_sec_seg, head_seg, prep_head_seg, full_data_seg, nobits_data_seg = self._get_segments() section = UnpreparedElfSection(None) self.assertRaises(InvalidArgument, empty_sec_seg.remove_section, section) empty_sec_seg.add_section(section) self.assertEqual(section in empty_sec_seg.sections, True) empty_sec_seg.remove_section(section) self.assertEqual(section in empty_sec_seg.sections, False) self.assertRaises(InvalidArgument, empty_sec_seg.remove_section, section) section = UnpreparedElfSection(None) self.assertRaises(InvalidArgument, full_sec_seg.remove_section, section) full_sec_seg.add_section(section) self.assertEqual(section in full_sec_seg.sections, True) full_sec_seg.remove_section(section) self.assertEqual(section in full_sec_seg.sections, False) self.assertRaises(InvalidArgument, full_sec_seg.remove_section, section)
def test_data_append(self): data = ByteArray("pants") sect = UnpreparedElfSection(None, "test", data = data) self.assertEquals(sect.get_data(), data) sect.append_data(ByteArray("foo")) self.assertEquals(sect.get_data(), ByteArray("pantsfoo")) sect = sect.prepare(0, 0, 0) self.assertEqual(hasattr(sect, "data_append"), False)
def test_add_section(self): empty_sec_seg, full_sec_seg, head_seg, prep_head_seg, full_data_seg, nobits_data_seg = self._get_segments() section = UnpreparedElfSection(None) # Adding to a section segment should work empty_sec_seg.add_section(section) self.assertEqual(section in empty_sec_seg.sections, True) full_sec_seg.add_section(section) self.assertEqual(section in full_sec_seg.sections, True)
def __init__(self, filename=None): BaseElfFile.__init__(self) if filename is not None: self.init_and_prepare_from_filename(filename) else: self.sections = [UnpreparedElfSection()] self.segments = [] self._sh_offset = None self._sh_strndx = 0 self.special_symbols = []
def test_prepare(self): ef = UnpreparedElfFile() sect = UnpreparedElfSection(ef, "test_sect") symtab = UnpreparedElfSymbolTable(ef, ".symtab") for name in ["foo", "bar"]: symtab.add_symbol(ElfSymbol(name, sect)) ef.wordsize = 32 ef.endianess = "<" symtab = symtab.prepare(0x1000, 1, 0)
def __init__(self, filename=None): BaseElfFile.__init__(self) if filename is not None: self.init_and_prepare_from_filename(filename) else: self.sections = [UnpreparedElfSection(self)] self.segments = [] self._sh_offset = None self._sh_strndx = 0 # Hold a list of the symbols in the got table self.got_table = []
def allocate_section(self, sect_name): """ Returns the section requested, creating a new one if required. """ sect = self.find_section_named(sect_name) if sect == None: if sect_name.find('bss') != -1: sect = UnpreparedElfSection(self, sect_name, SHT_NOBITS, 0, 0, SHF_WRITE | SHF_ALLOC, 8) elif sect_name == '.got': sect = UnpreparedElfSection(self, sect_name, SHT_PROGBITS, 0, ByteArray(), SHF_WRITE | SHF_ALLOC, 4, entsize=4) got_plt = UnpreparedElfSection(self, '.got.plt', SHT_PROGBITS, 0, ByteArray('\0' * 4 * 3), SHF_WRITE | SHF_ALLOC, 4, entsize=4) self.add_section(got_plt) else: sect = UnpreparedElfSection(self, sect_name, SHT_PROGBITS, 0, ByteArray(), SHF_WRITE | SHF_ALLOC, 4, entsize=4) self.add_section(sect) return sect
def test_copy_into(self): ef, sect, symbol = self._build_test_symbol() self._set_symbol_values(symbol) sect = UnpreparedElfSection(ef, "new_section") new_symbol = symbol.copy_into(sect) self.assertEqual(symbol.value, new_symbol.value) self.assertEqual(symbol.size, new_symbol.size) self.assertEqual(symbol.type, new_symbol.type) self.assertEqual(symbol.bind, new_symbol.bind) self.assertEqual(symbol.other, new_symbol.other) self.assertEqual(symbol.shndx, new_symbol.shndx) self.assertEqual(symbol.name, new_symbol.name) self.assertEqual(new_symbol.section, sect)
def test_repr(self): ef, sect, symbol = self._build_test_symbol() s1 = str(symbol) # We expect a differnt result when we have actual values symbol.value = 37 symbol.size = 100 s2 = str(symbol) self.assertNotEqual(s1, s2) # Same symbol, different section, should give differnt string sect = UnpreparedElfSection(ef, "test_section") symbol2 = ElfSymbol("test_symbol", sect, old_sym=symbol) s3 = str(symbol2) self.assertNotEqual(s2, s3)
def test_copy_into(self): elf_from = UnpreparedElfFile() elf_to = UnpreparedElfFile() seg = DataElfSegment(elf_from, ByteArray("pants")) new_seg = seg.copy_into(elf_to) seg = SectionedElfSegment(elf_from) seg.sections = [UnpreparedElfSection(elf_from, "test")] new_seg = seg.copy_into(elf_to) seg = DataElfSegment(elf_from, ByteArray("pants")) seg._data = ByteArray() new_seg = seg.copy_into(elf_to) seg = DataElfSegment(elf_from, ByteArray("pants")) seg.prepare(34) new_seg = seg.copy_into(elf_to) seg = HeaderElfSegment(elf_from) new_seg = seg.copy_into(elf_to)
def test_init(self): ElfSegment(None) ElfSegment(None, PT_LOAD) ElfSegment(None, program_header_type = PT_LOAD) sections = [UnpreparedElfSection(None, "foo")] self.assertRaises(TypeError, ElfSegment, data = ByteArray("pants"), sections = sections) seg = ElfSegment(None, program_header_type=PT_LOAD, vaddr=None, paddr=None) self.assertEquals(seg.vaddr, 0) self.assertEquals(seg.paddr, 0) prog_header = ElfProgramHeader("<") prog_header.p_type = PT_LOAD prog_header.p_vaddr = 0x1000 prog_header.p_paddr = 0x10000 prog_header.p_flags = 0x100 prog_header.p_align = 0x4000 seg = ElfSegment(None, prog_header=prog_header) self.assertEquals(prog_header.p_type, seg.type) self.assertEquals(prog_header.p_vaddr, seg.vaddr) self.assertEquals(prog_header.p_paddr, seg.paddr) self.assertEquals(prog_header.p_flags, seg.flags) self.assertEquals(prog_header.p_align, seg.align)
def test_set_flags(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(sect.flags, 0) sect.flags = 0xff self.assertEqual(sect.flags, 0xff)
def test_setname(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(sect.name, "test") sect.name = "pants" self.assertEqual(sect.name, "pants")
def test_init(self): UnpreparedElfSection(None, "test") UnpreparedElfSection(None, "test", SHT_PROGBITS) UnpreparedElfSection(None, "test", section_type = SHT_PROGBITS) sec = UnpreparedElfSection(None, "test", address = None) self.assertEquals(sec.address, 0)
def test_setname_prepared(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(sect.name, "test") sect = sect.prepare(0, 0, 0) self.assertEqual(hasattr(sect, "set_name"), False) # FIXME: this isn't the right thing to check
def test_set_addralign(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(sect.addralign, 0) sect.addralign = 13 self.assertEqual(sect.addralign, 13)
def test_set_info(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(sect.info, None) sect.info = 13 self.assertEqual(sect.info, 13)
def test_repr(self): sect = UnpreparedElfSection(None, "test") self.assertEquals(repr(sect), "<UnpreparedElfSection NULL test>")
def test_get_size(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(sect.get_size(), 0) sect = UnpreparedElfSection(None, "test", data = ByteArray("pants")) self.assertEqual(sect.get_size(), 5)
def test_get_offset(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(hasattr(sect, "get_offset"), False) sect = sect.prepare(15, 0, 0) self.assertEqual(sect.offset, 15)
def test_get_data(self): data = ByteArray("pants") sect = UnpreparedElfSection(None, "test", data = data) self.assertEquals(sect.get_data(), data)
def test_get_index(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(hasattr(sect, "get_index"), False) sect = sect.prepare(0, 15, 0) self.assertEqual(sect.index, 15)
def test_settype(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(sect.type, SHT_NULL) sect.type = SHT_PROGBITS self.assertEqual(sect.type, SHT_PROGBITS)
def test_set_address(self): sect = UnpreparedElfSection(None, "test") self.assertEqual(sect.address, 0) sect.address = 25 self.assertEqual(sect.address, 25)