def __init__(self, gfile, byte_ordering, word_size): self.gfile = gfile self.gfile.set_byte_ordering(byte_ordering) self.byte_order = byte_ordering self.word_size = word_size self.header = ElfFile.HEADER_MAP[self.word_size]() self.header.read_from_poker(BitPoker.new_with_gfile(self.gfile, 0)) # Setup the parts of the file # ... program headers self.pheaders = ElfFileProgramHeaderContainer \ (gfile, word_size, self.header.ai.e_phoff, self.header.ai.e_phentsize, self.header.ai.e_phnum.get(), ElfFile.PROGRAM_HEADER_MAP[word_size], elffile=self) # ... section headers self.sheaders = ElfFileSectionHeaderContainer \ (gfile, word_size, self.header.ai.e_shoff, self.header.ai.e_shentsize, self.header.ai.e_shnum.get(), ElfFile.SECTION_HEADER_MAP[word_size], elffile=self) # ... string table if self.header.ai.e_shstrndx != 0: self.string_table = ElfFileStringTable \ (self.gfile, self.sheaders[self.header.ai.e_shstrndx]) # ... symbol table self.symtable = None for header in self.sheaders: if header.get_name() == ".symtab": self.symtable = ElfFileSymbolTable(self.gfile, header)
def from_file(filename): gfile = GFile.existing(filename) poker = BitPoker() poker.set_mmapfile(gfile.mapping, 0) # Offset of 0 from start of file. poker.set_byte_ordering('lsb') # Use default because we don't (yet) know header = Elf32Header() # Once again, use a default size. header.read_from_poker(poker) # Examine the header for info we need. # Check the magic first. If we don't and the file is non-ELF, chances are # class & data won't match which will result in a confusing error message if header.ai.ei_magic != Elf32Header.EI_MAGIC: raise ElfFileNotElfException("Wanted magic %r, got %r" \ % (Elf32Header.EI_MAGIC, header.ai.ei_magic)) word_size = ElfFile.WORD_SIZE_MAP[header.ai.ei_class.get()] byte_order = ElfFile.BYTE_ORDER_MAP[header.ai.ei_data.get()] return ElfFile(gfile, byte_order, word_size)
def __getitem__(self, num): assert num >= 0 assert num < self.number inst = self.cls(self.word_size) poker = BitPoker.new_with_gfile(self.gfile, self.offset + (self.entsize * num)) inst.read_from_poker(poker) return inst
def append(self, infostruct): print "length of self is", len(self) newpos = len(self) offset = (infostruct.struct_size() * newpos) + self.kcp.ai.MemoryInfo['MemDescPtr'] newpoker = BitPoker.new_with_poker(self.kcp.poker, offset) infostruct.set_poker(newpoker) self.items[newpos] = infostruct self.kcp.ai.MemoryInfo['n'] = newpos + 1
def __getitem__(self, key): if key == self.kcp.ai.MemoryInfo['n']: raise StopIteration() assert key >= 0 and key < self.kcp.ai.MemoryInfo['n'] if key in self.items: return self.items[key] # Return the cached copy else: newitem = KCPMeminfoStruct(word_size_in_bits = self.kcp.word_size_in_bits) offset = (newitem.struct_size() * key) + self.kcp.ai.MemoryInfo['MemDescPtr'] newpoker = BitPoker.new_with_poker(self.kcp.poker, offset) newitem.read_from_poker(newpoker) self.items[key] = newitem return newitem
def __getitem__(self, key): if key == self.kcp.ai.MemoryInfo['n']: raise StopIteration() assert key >= 0 and key < self.kcp.ai.MemoryInfo['n'] if key in self.items: return self.items[key] # Return the cached copy else: newitem = KCPMeminfoStruct( word_size_in_bits=self.kcp.word_size_in_bits) offset = (newitem.struct_size() * key) + self.kcp.ai.MemoryInfo['MemDescPtr'] newpoker = BitPoker.new_with_poker(self.kcp.poker, offset) newitem.read_from_poker(newpoker) self.items[key] = newitem return newitem
def __getitem__(self, idx): if type(idx) == type(""): # Act like a dictionary for each in self: if str(each.ai.sh_name) == idx: return each raise "badness" else: if self.mutated: print "mutated", idx, self.container return self.container[idx] else: num = idx assert num >= 0 if num >= self.number: raise StopIteration() inst = self.cls(self.word_size, index=num, **self.kwargs) poker = BitPoker.new_with_gfile(self.gfile, self.offset + (self.entsize * num)) inst.read_from_poker(poker) return inst
def __init__(self, gfile, byte_ordering, word_size): self.gfile = gfile self.gfile.set_byte_ordering(byte_ordering) self.byte_order = byte_ordering self.word_size = word_size self.header = ElfFile.HEADER_MAP[self.word_size]() self.header.read_from_poker(BitPoker.new_with_gfile(self.gfile, 0)) # Setup the parts of the file # ... program headers self.pheaders = ElfFileContainer \ (gfile, word_size, self.header.ai.e_phoff, self.header.ai.e_phentsize, self.header.ai.e_phnum, ElfFile.PROGRAM_HEADER_MAP[word_size]) # ... section headers self.sheaders = ElfFileContainer \ (gfile, word_size, self.header.ai.e_shoff, self.header.ai.e_shentsize, self.header.ai.e_shnum, ElfFile.SECTION_HEADER_MAP[word_size]) # ... string table if self.header.ai.e_shstrndx != 0: self.string_table = ElfFileStringTable \ (self.gfile, self.sheaders[self.header.ai.e_shstrndx])
def __getitem__(self, idx): if type(idx) == type(""): # Act like a dictionary for each in self: if str(each.ai.sh_name) == idx: return each raise "badness" else: if self.mutated: print "mutated", idx, self.container return self.container[idx] else: num = idx assert num >= 0 if num >= self.number: raise StopIteration() inst = self.cls(self.word_size, index=num, **self.kwargs) poker = BitPoker.new_with_gfile( self.gfile, self.offset + (self.entsize * num)) inst.read_from_poker(poker) return inst
def from_file(filename): gfile = GFile.existing(filename) poker = BitPoker() poker.set_mmapfile(gfile.mapping, 0) # Offset of 0 from start of file. poker.set_byte_ordering('lsb') # Use default because we don't (yet) know header = Elf32Header() # Once again, use a default size. header.read_from_poker(poker) # Examine the header for info we need. # Check the magic first. If we don't and the file is non-ELF, chances are # class & data won't match which will result in a confusing error message if header.ai.ei_magic != Elf32Header.EI_MAGIC: raise ElfFileNotElfException("Wanted magic %r, got %r" \ % (Elf32Header.EI_MAGIC, header.ai.ei_magic)) word_size = ElfFile.WORD_SIZE_MAP[header.ai.ei_class] byte_order = ElfFile.BYTE_ORDER_MAP[header.ai.ei_data] return ElfFile(gfile, byte_order, word_size)
def find_kcp_in_elf_file(self, filename): # Find the KCP in an ELF file by looking for the section named ".kip" # This is not foolproof. Another non-foolproof way is to look for the # symbol named "kip". Other, even less foolproof methods are left # as an exercise. elf_file = ElfFile.from_file(filename) kip_section = None for s_header in elf_file.sheaders: section_header_name = elf_file.string_table.read(s_header.ai.sh_name) if section_header_name == '.kip': kip_section = s_header break if kip_section is None: raise KIPNotFound() # If this breaks our "offset" flag is wrong. assert kip_section.ai.sh_offset != 0 poker = BitPoker.new_with_gfile(elf_file.gfile, kip_section.ai.sh_offset) kcp = KCP(word_size_in_bits = elf_file.word_size) kcp.read_from_poker(poker) if list(kcp.ai.magic) != list(kcp.MAGIC): return None # Not a KCP, despite what the section header says. return kcp
def find_kcp_in_elf_file(self, filename): # Find the KCP in an ELF file by looking for the section named ".kip" # This is not foolproof. Another non-foolproof way is to look for the # symbol named "kip". Other, even less foolproof methods are left # as an exercise. elf_file = ElfFile.from_file(filename) kip_section = None for s_header in elf_file.sheaders: section_header_name = elf_file.string_table.read( s_header.ai.sh_name) if section_header_name == '.kip': kip_section = s_header break if kip_section is None: raise KIPNotFound() # If this breaks our "offset" flag is wrong. assert kip_section.ai.sh_offset != 0 poker = BitPoker.new_with_gfile(elf_file.gfile, kip_section.ai.sh_offset) kcp = KCP(word_size_in_bits=elf_file.word_size) kcp.read_from_poker(poker) if list(kcp.ai.magic) != list(kcp.MAGIC): return None # Not a KCP, despite what the section header says. return kcp
def __init__(self, gfile, section_header): self.header = section_header file_offset = section_header.ai.sh_offset self.poker = BitPoker.new_with_gfile(gfile, file_offset)
def __init__(self, gfile, section_header): file_offset = section_header.ai.sh_offset self.poker = BitPoker.new_with_gfile(gfile, file_offset)