1 #-------------------------------------------------------------------------------
2 # elftools: elf/segments.py
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
9 from ..construct import CString
10 from ..common.utils import struct_parse
11 from .constants import SH_FLAGS
14 class Segment(object):
15 def __init__(self, header, stream):
20 """ The segment data from the file.
22 self.stream.seek(self['p_offset'])
23 return self.stream.read(self['p_filesz'])
25 def __getitem__(self, name):
26 """ Implement dict-like access to header entries
28 return self.header[name]
30 def section_in_segment(self, section):
31 """ Is the given section contained in this segment?
33 Note: this tries to reproduce the intricate rules of the
34 ELF_SECTION_IN_SEGMENT_STRICT macro of the header
35 elf/include/internal.h in the source of binutils.
37 # Only the 'strict' checks from ELF_SECTION_IN_SEGMENT_1 are included
38 segtype = self['p_type']
39 sectype = section['sh_type']
40 secflags = section['sh_flags']
42 # Only PT_LOAD, PT_GNU_RELR0 and PT_TLS segments can contain SHF_TLS
44 if ( secflags & SH_FLAGS.SHF_TLS and
45 segtype in ('PT_TLS', 'PT_GNU_RELR0', 'PT_LOAD')):
47 # PT_TLS segment contains only SHF_TLS sections, PT_PHDR no sections
49 elif ( (secflags & SH_FLAGS.SHF_TLS) != 0 and
50 segtype not in ('PT_TLS', 'PT_PHDR')):
53 # In ELF_SECTION_IN_SEGMENT_STRICT the flag check_vma is on, so if
54 # this is an alloc section, check whether its VMA is in bounds.
55 if secflags & SH_FLAGS.SHF_ALLOC:
56 secaddr = section['sh_addr']
57 vaddr = self['p_vaddr']
59 # This checks that the section is wholly contained in the segment.
60 # The third condition is the 'strict' one - an empty section will
61 # not match at the very end of the segment (unless the segment is
62 # also zero size, which is handled by the second condition).
63 if not (secaddr >= vaddr and
64 secaddr - vaddr + section['sh_size'] <= self['p_memsz'] and
65 secaddr - vaddr <= self['p_memsz'] - 1):
68 # If we've come this far and it's a NOBITS section, it's in the segment
69 if sectype == 'SHT_NOBITS':
72 secoffset = section['sh_offset']
73 poffset = self['p_offset']
75 # Same logic as with secaddr vs. vaddr checks above, just on offsets in
77 return (secoffset >= poffset and
78 secoffset - poffset + section['sh_size'] <= self['p_filesz'] and
79 secoffset - poffset <= self['p_filesz'] - 1)
82 class InterpSegment(Segment):
83 """ INTERP segment. Knows how to obtain the path to the interpreter used
86 def __init__(self, header, stream):
87 super(InterpSegment, self).__init__(header, stream)
89 def get_interp_name(self):
90 """ Obtain the interpreter path used for this ELF file.
92 path_offset = self['p_offset']
96 stream_pos=path_offset)