from ..construct import ConstructError, CString\r
from .structs import ELFStructs\r
from .sections import Section\r
+from .segments import Segment\r
\r
\r
class ELFFile(object):\r
self._stringtable = self._get_stringtable()\r
\r
def num_sections(self):
- """ Get the number of sections in the file
+ """ Number of sections in the file
"""\r
return self['e_shnum']\r
\r
name = self._get_section_name(section_header)\r
return Section(section_header, name, self.stream)\r
\r
+ def iter_sections(self):
+ """ Yield all the sections in the file
+ """\r
+ for i in range(self.num_sections()):\r
+ yield self.get_section(i)\r
+ \r
+ def num_segments(self):
+ """ Number of segments in the file
+ """\r
+ return self['e_phnum']\r
+ \r
+ def get_segment(self, n):
+ """ Get the segment at index #n from the file (Segment object)
+ """\r
+ segment_header = self._get_segment_header(n)\r
+ return Segment(segment_header, self.stream)\r
+ \r
+ def iter_segments(self):
+ """ Yield all the segments in the file
+ """\r
+ for i in range(self.num_segments()):\r
+ yield self.get_segment(i)\r
+ \r
#-------------------------------- PRIVATE --------------------------------#\r
\r
def __getitem__(self, name):
"""\r
return self['e_shoff'] + n * self['e_shentsize']\r
\r
+ def _segment_offset(self, n):
+ """ Compute the offset of segment #n in the file
+ """\r
+ return self['e_phoff'] + n * self['e_phentsize']\r
+ \r
def _get_section_header(self, n):
""" Find the header of section #n, parse it and return the struct
"""\r
self.stream.seek(self._section_offset(n))\r
return self._struct_parse(self.structs.Elf_Shdr)\r
\r
+ def _get_segment_header(self, n):
+ """ Find the header of segment #n, parse it and return the struct
+ """\r
+ self.stream.seek(self._segment_offset(n))\r
+ return self._struct_parse(self.structs.Elf_Phdr)\r
+ \r
def _get_section_name(self, section_header):
""" Given a section header, find this section's name in the file's\r
string table, and return it as a normal Python string.
--- /dev/null
+#-------------------------------------------------------------------------------\r
+# elftools: elf/segments.py\r
+#\r
+# ELF segments\r
+#\r
+# Eli Bendersky (eliben@gmail.com)\r
+# This code is in the public domain\r
+#-------------------------------------------------------------------------------\r
+\r
+class Segment(object):\r
+ def __init__(self, header, stream):\r
+ self.header = header\r
+ self.stream = stream\r
+ \r
+ def data(self):\r
+ """ The segment data from the file.\r
+ """\r
+ self.stream.seek(self['p_offset'])\r
+ return self.stream.read(self['p_filesz'])\r
+\r
+ def __getitem__(self, name):\r
+ """ Implement dict-like access to header entries\r
+ """\r
+ return self.header[name]\r
+\r
\r
efile = ELFFile(stream)\r
\r
-print 'num', efile.num_sections()\r
-sec = efile.get_section(39)\r
+for sec in efile.iter_sections():\r
+ print sec.name\r
+\r
+for seg in efile.iter_segments():\r
+ print seg['p_type'], seg['p_offset']\r
+\r
+#~ print 'num', efile.num_sections()\r
+#~ sec = efile.get_section(39)\r
#~ print sec.header\r
-print sec.name\r
-print sec['sh_type']\r
-print map(ord, sec.data())\r
+#~ print sec.name\r
+#~ print sec['sh_type']\r
+#~ print map(ord, sec.data())\r
\r
#~ print sec.stream\r
#~ print map(ord, efile._stringtable)\r