1 #-------------------------------------------------------------------------------
2 # elftools: elf/elffile.py
4 # ELFFile - main class for accessing ELF files
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
16 PAGESIZE
= resource
.getpagesize()
21 PAGESIZE
= mmap
.PAGESIZE
26 from ..common
.py3compat
import BytesIO
27 from ..common
.exceptions
import ELFError
, ELFParseError
28 from ..common
.utils
import struct_parse
, elf_assert
29 from .structs
import ELFStructs
30 from .sections
import (
31 Section
, StringTableSection
, SymbolTableSection
,
32 SymbolTableIndexSection
, SUNWSyminfoTableSection
, NullSection
,
33 NoteSection
, StabSection
, ARMAttributesSection
)
34 from .dynamic
import DynamicSection
, DynamicSegment
35 from .relocation
import (RelocationSection
, RelocationHandler
,
36 RelrRelocationSection
)
37 from .gnuversions
import (
38 GNUVerNeedSection
, GNUVerDefSection
,
40 from .segments
import Segment
, InterpSegment
, NoteSegment
41 from ..dwarf
.dwarfinfo
import DWARFInfo
, DebugSectionDescriptor
, DwarfConfig
42 from ..ehabi
.ehabiinfo
import EHABIInfo
43 from .hash import ELFHashSection
, GNUHashSection
44 from .constants
import SHN_INDICES
46 class ELFFile(object):
47 """ Creation: the constructor accepts a stream (file-like object) with the
48 contents of an ELF file.
50 Optionally, a stream_loader function can be passed as the second
51 argument. This stream_loader function takes a relative file path to
52 load a supplementary object file, and returns a stream suitable for
53 creating a new ELFFile. Currently, the only such relative file path is
54 obtained from the supplementary object files.
56 Accessible attributes:
59 The stream holding the data of the file - must be a binary
60 stream (bytes, not string).
63 32 or 64 - specifies the word size of the target machine
66 boolean - specifies the target machine's endianness
69 string or int, either known value of E_TYPE enum defining ELF
70 type (e.g. executable, dynamic library or core dump) or integral
74 the complete ELF file header
77 the raw e_ident field of the header
79 def __init__(self
, stream
, stream_loader
=None):
81 self
.stream
.seek(0, io
.SEEK_END
)
82 self
.stream_len
= self
.stream
.tell()
85 self
.structs
= ELFStructs(
86 little_endian
=self
.little_endian
,
87 elfclass
=self
.elfclass
)
89 self
.structs
.create_basic_structs()
90 self
.header
= self
._parse
_elf
_header
()
91 self
.structs
.create_advanced_structs(
94 self
['e_ident']['EI_OSABI'])
96 self
.e_ident_raw
= self
.stream
.read(16)
98 self
._section
_header
_stringtable
= \
99 self
._get
_section
_header
_stringtable
()
100 self
._section
_name
_map
= None
101 self
.stream_loader
= stream_loader
104 def load_from_path(cls
, path
):
105 """Takes a path to a file on the local filesystem, and returns an
106 ELFFile from it, setting up a correct stream_loader relative to the
109 base_directory
= os
.path
.dirname(path
)
110 def loader(elf_path
):
111 # FIXME: use actual path instead of str/bytes
112 if not os
.path
.isabs(elf_path
):
113 elf_path
= os
.path
.join(base_directory
,
115 return open(elf_path
, 'rb')
116 stream
= open(path
, 'rb')
117 return ELFFile(stream
, loader
)
119 def num_sections(self
):
120 """ Number of sections in the file
122 if self
['e_shoff'] == 0:
124 # From the ELF ABI documentation at
125 # https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.sheader.html:
126 # "e_shnum normally tells how many entries the section header table
127 # contains. [...] If the number of sections is greater than or equal to
128 # SHN_LORESERVE (0xff00), e_shnum has the value SHN_UNDEF (0) and the
129 # actual number of section header table entries is contained in the
130 # sh_size field of the section header at index 0 (otherwise, the sh_size
131 # member of the initial entry contains 0)."
132 if self
['e_shnum'] == 0:
133 return self
._get
_section
_header
(0)['sh_size']
134 return self
['e_shnum']
136 def get_section(self
, n
):
137 """ Get the section at index #n from the file (Section object or a
140 section_header
= self
._get
_section
_header
(n
)
141 return self
._make
_section
(section_header
)
143 def get_section_by_name(self
, name
):
144 """ Get a section from the file, by name. Return None if no such
147 # The first time this method is called, construct a name to number
150 if self
._section
_name
_map
is None:
151 self
._make
_section
_name
_map
()
152 secnum
= self
._section
_name
_map
.get(name
, None)
153 return None if secnum
is None else self
.get_section(secnum
)
155 def get_section_index(self
, section_name
):
156 """ Gets the index of the section by name. Return None if no such
159 # The first time this method is called, construct a name to number
162 if self
._section
_name
_map
is None:
163 self
._make
_section
_name
_map
()
164 return self
._section
_name
_map
.get(section_name
, None)
166 def iter_sections(self
, type=None):
167 """ Yield all the sections in the file. If the optional |type|
168 parameter is passed, this method will only yield sections of the
169 given type. The parameter value must be a string containing the
170 name of the type as defined in the ELF specification, e.g.
173 for i
in range(self
.num_sections()):
174 section
= self
.get_section(i
)
175 if type is None or section
['sh_type'] == type:
178 def num_segments(self
):
179 """ Number of segments in the file
181 # From: https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI
182 # Section: 4.1.2 Number of Program Headers
183 # If the number of program headers is greater than or equal to
184 # PN_XNUM (0xffff), this member has the value PN_XNUM
185 # (0xffff). The actual number of program header table entries
186 # is contained in the sh_info field of the section header at
188 if self
['e_phnum'] < 0xffff:
189 return self
['e_phnum']
191 return self
.get_section(0)['sh_info']
193 def get_segment(self
, n
):
194 """ Get the segment at index #n from the file (Segment object)
196 segment_header
= self
._get
_segment
_header
(n
)
197 return self
._make
_segment
(segment_header
)
199 def iter_segments(self
, type=None):
200 """ Yield all the segments in the file. If the optional |type|
201 parameter is passed, this method will only yield segments of the
202 given type. The parameter value must be a string containing the
203 name of the type as defined in the ELF specification, e.g.
206 for i
in range(self
.num_segments()):
207 segment
= self
.get_segment(i
)
208 if type is None or segment
['p_type'] == type:
211 def address_offsets(self
, start
, size
=1):
212 """ Yield a file offset for each ELF segment containing a memory region.
214 A memory region is defined by the range [start...start+size). The
215 offset of the region is yielded.
218 # consider LOAD only to prevent same address being yielded twice
219 for seg
in self
.iter_segments(type='PT_LOAD'):
220 if (start
>= seg
['p_vaddr'] and
221 end
<= seg
['p_vaddr'] + seg
['p_filesz']):
222 yield start
- seg
['p_vaddr'] + seg
['p_offset']
224 def has_dwarf_info(self
):
225 """ Check whether this file appears to have debugging information.
226 We assume that if it has the .debug_info or .zdebug_info section, it
227 has all the other required sections as well.
229 return bool(self
.get_section_by_name('.debug_info') or
230 self
.get_section_by_name('.zdebug_info') or
231 self
.get_section_by_name('.eh_frame'))
233 def get_dwarf_info(self
, relocate_dwarf_sections
=True, follow_links
=True):
234 """ Return a DWARFInfo object representing the debugging information in
237 If relocate_dwarf_sections is True, relocations for DWARF sections
238 are looked up and applied.
240 If follow_links is True, we will try to load the supplementary
241 object file (if any), and use it to resolve references and imports.
243 # Expect that has_dwarf_info was called, so at least .debug_info is
245 # Sections that aren't found will be passed as None to DWARFInfo.
247 section_names
= ('.debug_info', '.debug_aranges', '.debug_abbrev',
248 '.debug_str', '.debug_line', '.debug_frame',
249 '.debug_loc', '.debug_ranges', '.debug_pubtypes',
250 '.debug_pubnames', '.debug_addr',
251 '.debug_str_offsets', '.debug_line_str',
252 '.debug_loclists', '.debug_rnglists',
253 '.debug_sup', '.gnu_debugaltlink')
255 compressed
= bool(self
.get_section_by_name('.zdebug_info'))
257 section_names
= tuple(map(lambda x
: '.z' + x
[1:], section_names
))
259 # As it is loaded in the process image, .eh_frame cannot be compressed
260 section_names
+= ('.eh_frame', )
262 (debug_info_sec_name
, debug_aranges_sec_name
, debug_abbrev_sec_name
,
263 debug_str_sec_name
, debug_line_sec_name
, debug_frame_sec_name
,
264 debug_loc_sec_name
, debug_ranges_sec_name
, debug_pubtypes_name
,
265 debug_pubnames_name
, debug_addr_name
, debug_str_offsets_name
,
266 debug_line_str_name
, debug_loclists_sec_name
, debug_rnglists_sec_name
,
267 debug_sup_name
, gnu_debugaltlink_name
, eh_frame_sec_name
) = section_names
270 for secname
in section_names
:
271 section
= self
.get_section_by_name(secname
)
273 debug_sections
[secname
] = None
275 dwarf_section
= self
._read
_dwarf
_section
(
277 relocate_dwarf_sections
)
278 if compressed
and secname
.startswith('.z'):
279 dwarf_section
= self
._decompress
_dwarf
_section
(dwarf_section
)
280 debug_sections
[secname
] = dwarf_section
282 # Lookup if we have any of the .gnu_debugaltlink (GNU proprietary
283 # implementation) or .debug_sup sections, referencing a supplementary
286 dwarfinfo
= DWARFInfo(
288 little_endian
=self
.little_endian
,
289 default_address_size
=self
.elfclass
// 8,
290 machine_arch
=self
.get_machine_arch()),
291 debug_info_sec
=debug_sections
[debug_info_sec_name
],
292 debug_aranges_sec
=debug_sections
[debug_aranges_sec_name
],
293 debug_abbrev_sec
=debug_sections
[debug_abbrev_sec_name
],
294 debug_frame_sec
=debug_sections
[debug_frame_sec_name
],
295 eh_frame_sec
=debug_sections
[eh_frame_sec_name
],
296 debug_str_sec
=debug_sections
[debug_str_sec_name
],
297 debug_loc_sec
=debug_sections
[debug_loc_sec_name
],
298 debug_ranges_sec
=debug_sections
[debug_ranges_sec_name
],
299 debug_line_sec
=debug_sections
[debug_line_sec_name
],
300 debug_pubtypes_sec
=debug_sections
[debug_pubtypes_name
],
301 debug_pubnames_sec
=debug_sections
[debug_pubnames_name
],
302 debug_addr_sec
=debug_sections
[debug_addr_name
],
303 debug_str_offsets_sec
=debug_sections
[debug_str_offsets_name
],
304 debug_line_str_sec
=debug_sections
[debug_line_str_name
],
305 debug_loclists_sec
=debug_sections
[debug_loclists_sec_name
],
306 debug_rnglists_sec
=debug_sections
[debug_rnglists_sec_name
],
307 debug_sup_sec
=debug_sections
[debug_sup_name
],
308 gnu_debugaltlink_sec
=debug_sections
[gnu_debugaltlink_name
]
311 dwarfinfo
.supplementary_dwarfinfo
= self
.get_supplementary_dwarfinfo(dwarfinfo
)
315 def get_supplementary_dwarfinfo(self
, dwarfinfo
):
317 Read supplementary dwarfinfo, from either the standared .debug_sup
318 section or the GNU proprietary .gnu_debugaltlink.
320 supfilepath
= dwarfinfo
.parse_debugsupinfo()
321 if supfilepath
is not None and self
.stream_loader
is not None:
322 stream
= self
.stream_loader(supfilepath
)
323 supelffile
= ELFFile(stream
)
324 dwarf_info
= supelffile
.get_dwarf_info()
330 def has_ehabi_info(self
):
331 """ Check whether this file appears to have arm exception handler index table.
333 return any(self
.iter_sections(type='SHT_ARM_EXIDX'))
335 def get_ehabi_infos(self
):
336 """ Generally, shared library and executable contain 1 .ARM.exidx section.
337 Object file contains many .ARM.exidx sections.
338 So we must traverse every section and filter sections whose type is SHT_ARM_EXIDX.
341 if self
['e_type'] == 'ET_REL':
342 # TODO: support relocatable file
343 assert False, "Current version of pyelftools doesn't support relocatable file."
344 for section
in self
.iter_sections(type='SHT_ARM_EXIDX'):
345 _ret
.append(EHABIInfo(section
, self
.little_endian
))
346 return _ret
if len(_ret
) > 0 else None
348 def get_machine_arch(self
):
349 """ Return the machine architecture, as detected from the ELF header.
352 'EM_M32' : 'AT&T WE 32100',
353 'EM_SPARC' : 'SPARC',
355 'EM_68K' : 'Motorola 68000',
356 'EM_88K' : 'Motorola 88000',
357 'EM_IAMCU' : 'Intel MCU',
358 'EM_860' : 'Intel 80860',
360 'EM_S370' : 'IBM System/370',
361 'EM_MIPS_RS3_LE' : 'MIPS RS3000 Little-endian',
362 'EM_PARISC' : 'Hewlett-Packard PA-RISC',
363 'EM_VPP500' : 'Fujitsu VPP500',
364 'EM_SPARC32PLUS' : 'Enhanced SPARC',
365 'EM_960' : 'Intel 80960',
366 'EM_PPC' : 'PowerPC',
367 'EM_PPC64' : '64-bit PowerPC',
368 'EM_S390' : 'IBM System/390',
369 'EM_SPU' : 'IBM SPU/SPC',
370 'EM_V800' : 'NEC V800',
371 'EM_FR20' : 'Fujitsu FR20',
372 'EM_RH32' : 'TRW RH-32',
373 'EM_RCE' : 'Motorola RCE',
375 'EM_ALPHA' : 'Digital Alpha',
376 'EM_SH' : 'Hitachi SH',
377 'EM_SPARCV9' : 'SPARC Version 9',
378 'EM_TRICORE' : 'Siemens TriCore embedded processor',
379 'EM_ARC' : 'Argonaut RISC Core, Argonaut Technologies Inc.',
380 'EM_H8_300' : 'Hitachi H8/300',
381 'EM_H8_300H' : 'Hitachi H8/300H',
382 'EM_H8S' : 'Hitachi H8S',
383 'EM_H8_500' : 'Hitachi H8/500',
384 'EM_IA_64' : 'Intel IA-64',
385 'EM_MIPS_X' : 'MIPS-X',
386 'EM_COLDFIRE' : 'Motorola ColdFire',
387 'EM_68HC12' : 'Motorola M68HC12',
388 'EM_MMA' : 'Fujitsu MMA',
389 'EM_PCP' : 'Siemens PCP',
390 'EM_NCPU' : 'Sony nCPU',
391 'EM_NDR1' : 'Denso NDR1',
392 'EM_STARCORE' : 'Motorola Star*Core',
393 'EM_ME16' : 'Toyota ME16',
394 'EM_ST100' : 'STMicroelectronics ST100',
395 'EM_TINYJ' : 'Advanced Logic TinyJ',
397 'EM_PDSP' : 'Sony DSP',
398 'EM_PDP10' : 'Digital Equipment PDP-10',
399 'EM_PDP11' : 'Digital Equipment PDP-11',
400 'EM_FX66' : 'Siemens FX66',
401 'EM_ST9PLUS' : 'STMicroelectronics ST9+ 8/16 bit',
402 'EM_ST7' : 'STMicroelectronics ST7 8-bit',
403 'EM_68HC16' : 'Motorola MC68HC16',
404 'EM_68HC11' : 'Motorola MC68HC11',
405 'EM_68HC08' : 'Motorola MC68HC08',
406 'EM_68HC05' : 'Motorola MC68HC05',
407 'EM_SVX' : 'Silicon Graphics SVx',
408 'EM_ST19' : 'STMicroelectronics ST19 8-bit',
409 'EM_VAX' : 'Digital VAX',
410 'EM_CRIS' : 'Axis Communications 32-bit',
411 'EM_JAVELIN' : 'Infineon Technologies 32-bit',
412 'EM_FIREPATH' : 'Element 14 64-bit DSP',
413 'EM_ZSP' : 'LSI Logic 16-bit DSP',
414 'EM_MMIX' : 'Donald Knuth\'s educational 64-bit',
415 'EM_HUANY' : 'Harvard University machine-independent object files',
416 'EM_PRISM' : 'SiTera Prism',
417 'EM_AVR' : 'Atmel AVR 8-bit',
418 'EM_FR30' : 'Fujitsu FR30',
419 'EM_D10V' : 'Mitsubishi D10V',
420 'EM_D30V' : 'Mitsubishi D30V',
421 'EM_V850' : 'NEC v850',
422 'EM_M32R' : 'Mitsubishi M32R',
423 'EM_MN10300' : 'Matsushita MN10300',
424 'EM_MN10200' : 'Matsushita MN10200',
425 'EM_PJ' : 'picoJava',
426 'EM_OPENRISC' : 'OpenRISC 32-bit',
427 'EM_ARC_COMPACT' : 'ARC International ARCompact',
428 'EM_XTENSA' : 'Tensilica Xtensa',
429 'EM_VIDEOCORE' : 'Alphamosaic VideoCore',
430 'EM_TMM_GPP' : 'Thompson Multimedia',
431 'EM_NS32K' : 'National Semiconductor 32000 series',
432 'EM_TPC' : 'Tenor Network TPC',
433 'EM_SNP1K' : 'Trebia SNP 1000',
434 'EM_ST200' : 'STMicroelectronics ST200',
435 'EM_IP2K' : 'Ubicom IP2xxx',
437 'EM_CR' : 'National Semiconductor CompactRISC',
438 'EM_F2MC16' : 'Fujitsu F2MC16',
439 'EM_MSP430' : 'Texas Instruments msp430',
440 'EM_BLACKFIN' : 'Analog Devices Blackfin',
441 'EM_SE_C33' : 'Seiko Epson S1C33',
443 'EM_ARCA' : 'Arca RISC',
444 'EM_UNICORE' : 'PKU-Unity MPRC',
445 'EM_EXCESS' : 'eXcess',
446 'EM_DXP' : 'Icera Semiconductor Deep Execution Processor',
447 'EM_ALTERA_NIOS2' : 'Altera Nios II',
448 'EM_CRX' : 'National Semiconductor CompactRISC CRX',
449 'EM_XGATE' : 'Motorola XGATE',
450 'EM_C166' : 'Infineon C16x/XC16x',
451 'EM_M16C' : 'Renesas M16C',
452 'EM_DSPIC30F' : 'Microchip Technology dsPIC30F',
453 'EM_CE' : 'Freescale Communication Engine RISC core',
454 'EM_M32C' : 'Renesas M32C',
455 'EM_TSK3000' : 'Altium TSK3000',
456 'EM_RS08' : 'Freescale RS08',
457 'EM_SHARC' : 'Analog Devices SHARC',
458 'EM_ECOG2' : 'Cyan Technology eCOG2',
459 'EM_SCORE7' : 'Sunplus S+core7 RISC',
460 'EM_DSP24' : 'New Japan Radio (NJR) 24-bit DSP',
461 'EM_VIDEOCORE3' : 'Broadcom VideoCore III',
462 'EM_LATTICEMICO32' : 'Lattice FPGA RISC',
463 'EM_SE_C17' : 'Seiko Epson C17',
464 'EM_TI_C6000' : 'TI TMS320C6000',
465 'EM_TI_C2000' : 'TI TMS320C2000',
466 'EM_TI_C5500' : 'TI TMS320C55x',
467 'EM_TI_ARP32' : 'TI Application Specific RISC, 32bit',
468 'EM_TI_PRU' : 'TI Programmable Realtime Unit',
469 'EM_MMDSP_PLUS' : 'STMicroelectronics 64bit VLIW',
470 'EM_CYPRESS_M8C' : 'Cypress M8C',
471 'EM_R32C' : 'Renesas R32C',
472 'EM_TRIMEDIA' : 'NXP Semiconductors TriMedia',
473 'EM_QDSP6' : 'QUALCOMM DSP6',
474 'EM_8051' : 'Intel 8051',
475 'EM_STXP7X' : 'STMicroelectronics STxP7x',
476 'EM_NDS32' : 'Andes Technology RISC',
477 'EM_ECOG1' : 'Cyan Technology eCOG1X',
478 'EM_ECOG1X' : 'Cyan Technology eCOG1X',
479 'EM_MAXQ30' : 'Dallas Semiconductor MAXQ30',
480 'EM_XIMO16' : 'New Japan Radio (NJR) 16-bit',
481 'EM_MANIK' : 'M2000 Reconfigurable RISC',
482 'EM_CRAYNV2' : 'Cray Inc. NV2',
483 'EM_RX' : 'Renesas RX',
484 'EM_METAG' : 'Imagination Technologies META',
485 'EM_MCST_ELBRUS' : 'MCST Elbrus',
486 'EM_ECOG16' : 'Cyan Technology eCOG16',
487 'EM_CR16' : 'National Semiconductor CompactRISC CR16 16-bit',
488 'EM_ETPU' : 'Freescale',
489 'EM_SLE9X' : 'Infineon Technologies SLE9X',
490 'EM_L10M' : 'Intel L10M',
491 'EM_K10M' : 'Intel K10M',
492 'EM_AARCH64' : 'AArch64',
493 'EM_AVR32' : 'Atmel 32-bit',
494 'EM_STM8' : 'STMicroeletronics STM8 8-bit',
495 'EM_TILE64' : 'Tilera TILE64',
496 'EM_TILEPRO' : 'Tilera TILEPro',
497 'EM_MICROBLAZE' : 'Xilinx MicroBlaze 32-bit RISC',
498 'EM_CUDA' : 'NVIDIA CUDA',
499 'EM_TILEGX' : 'Tilera TILE-Gx',
500 'EM_CLOUDSHIELD' : 'CloudShield',
501 'EM_COREA_1ST' : 'KIPO-KAIST Core-A 1st generation',
502 'EM_COREA_2ND' : 'KIPO-KAIST Core-A 2nd generation',
503 'EM_ARC_COMPACT2' : 'Synopsys ARCompact V2',
504 'EM_OPEN8' : 'Open8 8-bit RISC',
505 'EM_RL78' : 'Renesas RL78',
506 'EM_VIDEOCORE5' : 'Broadcom VideoCore V',
507 'EM_78KOR' : 'Renesas 78KOR',
508 'EM_56800EX' : 'Freescale 56800EX',
509 'EM_BA1' : 'Beyond BA1',
510 'EM_BA2' : 'Beyond BA2',
511 'EM_XCORE' : 'XMOS xCORE',
512 'EM_MCHP_PIC' : 'Microchip 8-bit PIC',
513 'EM_INTEL205' : 'Reserved by Intel',
514 'EM_INTEL206' : 'Reserved by Intel',
515 'EM_INTEL207' : 'Reserved by Intel',
516 'EM_INTEL208' : 'Reserved by Intel',
517 'EM_INTEL209' : 'Reserved by Intel',
518 'EM_KM32' : 'KM211 KM32 32-bit',
519 'EM_KMX32' : 'KM211 KMX32 32-bit',
520 'EM_KMX16' : 'KM211 KMX16 16-bit',
521 'EM_KMX8' : 'KM211 KMX8 8-bit',
522 'EM_KVARC' : 'KM211 KVARC',
523 'EM_CDP' : 'Paneve CDP',
524 'EM_COGE' : 'Cognitive',
525 'EM_COOL' : 'Bluechip Systems CoolEngine',
526 'EM_NORC' : 'Nanoradio Optimized RISC',
527 'EM_CSR_KALIMBA' : 'CSR Kalimba',
528 'EM_Z80' : 'Zilog Z80',
529 'EM_VISIUM' : 'VISIUMcore',
530 'EM_FT32' : 'FTDI Chip FT32 32-bit RISC',
531 'EM_MOXIE' : 'Moxie',
532 'EM_AMDGPU' : 'AMD GPU',
533 'EM_RISCV' : 'RISC-V',
534 'EM_BPF' : 'Linux BPF - in-kernel virtual machine',
536 'EM_FRV' : 'Fujitsu FR-V'
539 return architectures
.get(self
['e_machine'], '<unknown>')
541 def get_shstrndx(self
):
542 """ Find the string table section index for the section header table
544 # From https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html:
545 # If the section name string table section index is greater than or
546 # equal to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX
547 # (0xffff) and the actual index of the section name string table section
548 # is contained in the sh_link field of the section header at index 0.
549 if self
['e_shstrndx'] != SHN_INDICES
.SHN_XINDEX
:
550 return self
['e_shstrndx']
552 return self
._get
_section
_header
(0)['sh_link']
554 #-------------------------------- PRIVATE --------------------------------#
556 def __getitem__(self
, name
):
557 """ Implement dict-like access to header entries
559 return self
.header
[name
]
561 def _identify_file(self
):
562 """ Verify the ELF file and identify its class and endianness.
564 # Note: this code reads the stream directly, without using ELFStructs,
565 # since we don't yet know its exact format. ELF was designed to be
566 # read like this - its e_ident field is word-size and endian agnostic.
568 magic
= self
.stream
.read(4)
569 elf_assert(magic
== b
'\x7fELF', 'Magic number does not match')
571 ei_class
= self
.stream
.read(1)
572 if ei_class
== b
'\x01':
574 elif ei_class
== b
'\x02':
577 raise ELFError('Invalid EI_CLASS %s' % repr(ei_class
))
579 ei_data
= self
.stream
.read(1)
580 if ei_data
== b
'\x01':
581 self
.little_endian
= True
582 elif ei_data
== b
'\x02':
583 self
.little_endian
= False
585 raise ELFError('Invalid EI_DATA %s' % repr(ei_data
))
587 def _section_offset(self
, n
):
588 """ Compute the offset of section #n in the file
590 return self
['e_shoff'] + n
* self
['e_shentsize']
592 def _segment_offset(self
, n
):
593 """ Compute the offset of segment #n in the file
595 return self
['e_phoff'] + n
* self
['e_phentsize']
597 def _make_segment(self
, segment_header
):
598 """ Create a Segment object of the appropriate type
600 segtype
= segment_header
['p_type']
601 if segtype
== 'PT_INTERP':
602 return InterpSegment(segment_header
, self
.stream
)
603 elif segtype
== 'PT_DYNAMIC':
604 return DynamicSegment(segment_header
, self
.stream
, self
)
605 elif segtype
== 'PT_NOTE':
606 return NoteSegment(segment_header
, self
.stream
, self
)
608 return Segment(segment_header
, self
.stream
)
610 def _get_section_header(self
, n
):
611 """ Find the header of section #n, parse it and return the struct
614 stream_pos
= self
._section
_offset
(n
)
615 if stream_pos
> self
.stream_len
:
619 self
.structs
.Elf_Shdr
,
621 stream_pos
=stream_pos
)
623 def _get_section_name(self
, section_header
):
624 """ Given a section header, find this section's name in the file's
627 if self
._section
_header
_stringtable
is None:
628 raise ELFParseError("String Table not found")
630 name_offset
= section_header
['sh_name']
631 return self
._section
_header
_stringtable
.get_string(name_offset
)
633 def _make_section(self
, section_header
):
634 """ Create a section object of the appropriate type
636 name
= self
._get
_section
_name
(section_header
)
637 sectype
= section_header
['sh_type']
639 if sectype
== 'SHT_STRTAB':
640 return StringTableSection(section_header
, name
, self
)
641 elif sectype
== 'SHT_NULL':
642 return NullSection(section_header
, name
, self
)
643 elif sectype
in ('SHT_SYMTAB', 'SHT_DYNSYM', 'SHT_SUNW_LDYNSYM'):
644 return self
._make
_symbol
_table
_section
(section_header
, name
)
645 elif sectype
== 'SHT_SYMTAB_SHNDX':
646 return self
._make
_symbol
_table
_index
_section
(section_header
, name
)
647 elif sectype
== 'SHT_SUNW_syminfo':
648 return self
._make
_sunwsyminfo
_table
_section
(section_header
, name
)
649 elif sectype
== 'SHT_GNU_verneed':
650 return self
._make
_gnu
_verneed
_section
(section_header
, name
)
651 elif sectype
== 'SHT_GNU_verdef':
652 return self
._make
_gnu
_verdef
_section
(section_header
, name
)
653 elif sectype
== 'SHT_GNU_versym':
654 return self
._make
_gnu
_versym
_section
(section_header
, name
)
655 elif sectype
in ('SHT_REL', 'SHT_RELA'):
656 return RelocationSection(section_header
, name
, self
)
657 elif sectype
== 'SHT_DYNAMIC':
658 return DynamicSection(section_header
, name
, self
)
659 elif sectype
== 'SHT_NOTE':
660 return NoteSection(section_header
, name
, self
)
661 elif sectype
== 'SHT_PROGBITS' and name
== '.stab':
662 return StabSection(section_header
, name
, self
)
663 elif sectype
== 'SHT_ARM_ATTRIBUTES':
664 return ARMAttributesSection(section_header
, name
, self
)
665 elif sectype
== 'SHT_HASH':
666 return self
._make
_elf
_hash
_section
(section_header
, name
)
667 elif sectype
== 'SHT_GNU_HASH':
668 return self
._make
_gnu
_hash
_section
(section_header
, name
)
669 elif sectype
== 'SHT_RELR':
670 return RelrRelocationSection(section_header
, name
, self
)
672 return Section(section_header
, name
, self
)
674 def _make_section_name_map(self
):
675 self
._section
_name
_map
= {}
676 for i
, sec
in enumerate(self
.iter_sections()):
677 self
._section
_name
_map
[sec
.name
] = i
679 def _make_symbol_table_section(self
, section_header
, name
):
680 """ Create a SymbolTableSection
682 linked_strtab_index
= section_header
['sh_link']
683 strtab_section
= self
.get_section(linked_strtab_index
)
684 return SymbolTableSection(
685 section_header
, name
,
687 stringtable
=strtab_section
)
689 def _make_symbol_table_index_section(self
, section_header
, name
):
690 """ Create a SymbolTableIndexSection object
692 linked_symtab_index
= section_header
['sh_link']
693 return SymbolTableIndexSection(
694 section_header
, name
, elffile
=self
,
695 symboltable
=linked_symtab_index
)
697 def _make_sunwsyminfo_table_section(self
, section_header
, name
):
698 """ Create a SUNWSyminfoTableSection
700 linked_strtab_index
= section_header
['sh_link']
701 strtab_section
= self
.get_section(linked_strtab_index
)
702 return SUNWSyminfoTableSection(
703 section_header
, name
,
705 symboltable
=strtab_section
)
707 def _make_gnu_verneed_section(self
, section_header
, name
):
708 """ Create a GNUVerNeedSection
710 linked_strtab_index
= section_header
['sh_link']
711 strtab_section
= self
.get_section(linked_strtab_index
)
712 return GNUVerNeedSection(
713 section_header
, name
,
715 stringtable
=strtab_section
)
717 def _make_gnu_verdef_section(self
, section_header
, name
):
718 """ Create a GNUVerDefSection
720 linked_strtab_index
= section_header
['sh_link']
721 strtab_section
= self
.get_section(linked_strtab_index
)
722 return GNUVerDefSection(
723 section_header
, name
,
725 stringtable
=strtab_section
)
727 def _make_gnu_versym_section(self
, section_header
, name
):
728 """ Create a GNUVerSymSection
730 linked_strtab_index
= section_header
['sh_link']
731 strtab_section
= self
.get_section(linked_strtab_index
)
732 return GNUVerSymSection(
733 section_header
, name
,
735 symboltable
=strtab_section
)
737 def _make_elf_hash_section(self
, section_header
, name
):
738 linked_symtab_index
= section_header
['sh_link']
739 symtab_section
= self
.get_section(linked_symtab_index
)
740 return ELFHashSection(
741 section_header
, name
, self
, symtab_section
744 def _make_gnu_hash_section(self
, section_header
, name
):
745 linked_symtab_index
= section_header
['sh_link']
746 symtab_section
= self
.get_section(linked_symtab_index
)
747 return GNUHashSection(
748 section_header
, name
, self
, symtab_section
751 def _get_segment_header(self
, n
):
752 """ Find the header of segment #n, parse it and return the struct
755 self
.structs
.Elf_Phdr
,
757 stream_pos
=self
._segment
_offset
(n
))
759 def _get_section_header_stringtable(self
):
760 """ Get the string table section corresponding to the section header
763 stringtable_section_num
= self
.get_shstrndx()
765 stringtable_section_header
= self
._get
_section
_header
(stringtable_section_num
)
766 if stringtable_section_header
is None:
769 return StringTableSection(
770 header
=stringtable_section_header
,
774 def _parse_elf_header(self
):
775 """ Parses the ELF file header and assigns the result to attributes
778 return struct_parse(self
.structs
.Elf_Ehdr
, self
.stream
, stream_pos
=0)
780 def _read_dwarf_section(self
, section
, relocate_dwarf_sections
):
781 """ Read the contents of a DWARF section from the stream and return a
782 DebugSectionDescriptor. Apply relocations if asked to.
784 # The section data is read into a new stream, for processing
785 section_stream
= BytesIO()
786 section_stream
.write(section
.data())
788 if relocate_dwarf_sections
:
789 reloc_handler
= RelocationHandler(self
)
790 reloc_section
= reloc_handler
.find_relocations_for_section(section
)
791 if reloc_section
is not None:
792 reloc_handler
.apply_section_relocations(
793 section_stream
, reloc_section
)
795 return DebugSectionDescriptor(
796 stream
=section_stream
,
798 global_offset
=section
['sh_offset'],
799 size
=section
.data_size
,
800 address
=section
['sh_addr'])
803 def _decompress_dwarf_section(section
):
804 """ Returns the uncompressed contents of the provided DWARF section.
806 # TODO: support other compression formats from readelf.c
807 assert section
.size
> 12, 'Unsupported compression format.'
809 section
.stream
.seek(0)
810 # According to readelf.c the content should contain "ZLIB"
811 # followed by the uncompressed section size - 8 bytes in
813 compression_type
= section
.stream
.read(4)
814 assert compression_type
== b
'ZLIB', \
815 'Invalid compression type: %r' % (compression_type
)
817 uncompressed_size
= struct
.unpack('>Q', section
.stream
.read(8))[0]
819 decompressor
= zlib
.decompressobj()
820 uncompressed_stream
= BytesIO()
822 chunk
= section
.stream
.read(PAGESIZE
)
825 uncompressed_stream
.write(decompressor
.decompress(chunk
))
826 uncompressed_stream
.write(decompressor
.flush())
828 uncompressed_stream
.seek(0, io
.SEEK_END
)
829 size
= uncompressed_stream
.tell()
830 assert uncompressed_size
== size
, \
831 'Wrong uncompressed size: expected %r, but got %r' % (
832 uncompressed_size
, size
,
835 return section
._replace
(stream
=uncompressed_stream
, size
=size
)
843 def __exit__(self
, type, value
, traceback
):