Initial s390x relocation support (#515)
[pyelftools.git] / elftools / elf / elffile.py
1 #-------------------------------------------------------------------------------
2 # elftools: elf/elffile.py
3 #
4 # ELFFile - main class for accessing ELF files
5 #
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
9 import io
10 from io import BytesIO
11 import os
12 import struct
13 import zlib
14
15 try:
16 import resource
17 PAGESIZE = resource.getpagesize()
18 except ImportError:
19 try:
20 # Windows system
21 import mmap
22 PAGESIZE = mmap.PAGESIZE
23 except ImportError:
24 # Jython
25 PAGESIZE = 4096
26
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, RISCVAttributesSection)
34 from .dynamic import DynamicSection, DynamicSegment
35 from .relocation import (RelocationSection, RelocationHandler,
36 RelrRelocationSection)
37 from .gnuversions import (
38 GNUVerNeedSection, GNUVerDefSection,
39 GNUVerSymSection)
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
45
46 class ELFFile(object):
47 """ Creation: the constructor accepts a stream (file-like object) with the
48 contents of an ELF file.
49
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.
55
56 Accessible attributes:
57
58 stream:
59 The stream holding the data of the file - must be a binary
60 stream (bytes, not string).
61
62 elfclass:
63 32 or 64 - specifies the word size of the target machine
64
65 little_endian:
66 boolean - specifies the target machine's endianness
67
68 elftype:
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
71 unparsed value
72
73 header:
74 the complete ELF file header
75
76 e_ident_raw:
77 the raw e_ident field of the header
78 """
79 def __init__(self, stream, stream_loader=None):
80 self.stream = stream
81 self.stream.seek(0, io.SEEK_END)
82 self.stream_len = self.stream.tell()
83
84 self._identify_file()
85 self.structs = ELFStructs(
86 little_endian=self.little_endian,
87 elfclass=self.elfclass)
88
89 self.structs.create_basic_structs()
90 self.header = self._parse_elf_header()
91 self.structs.create_advanced_structs(
92 self['e_type'],
93 self['e_machine'],
94 self['e_ident']['EI_OSABI'])
95 self.stream.seek(0)
96 self.e_ident_raw = self.stream.read(16)
97
98 self._section_header_stringtable = \
99 self._get_section_header_stringtable()
100 self._section_name_map = None
101 self.stream_loader = stream_loader
102
103 @classmethod
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
107 original file.
108 """
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,
114 elf_path)
115 return open(elf_path, 'rb')
116 stream = open(path, 'rb')
117 return ELFFile(stream, loader)
118
119 def num_sections(self):
120 """ Number of sections in the file
121 """
122 if self['e_shoff'] == 0:
123 return 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']
135
136 def get_section(self, n):
137 """ Get the section at index #n from the file (Section object or a
138 subclass)
139 """
140 section_header = self._get_section_header(n)
141 return self._make_section(section_header)
142
143 def get_section_by_name(self, name):
144 """ Get a section from the file, by name. Return None if no such
145 section exists.
146 """
147 # The first time this method is called, construct a name to number
148 # mapping
149 #
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)
154
155 def get_section_index(self, section_name):
156 """ Gets the index of the section by name. Return None if no such
157 section name exists.
158 """
159 # The first time this method is called, construct a name to number
160 # mapping
161 #
162 if self._section_name_map is None:
163 self._make_section_name_map()
164 return self._section_name_map.get(section_name, None)
165
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.
171 'SHT_SYMTAB'.
172 """
173 for i in range(self.num_sections()):
174 section = self.get_section(i)
175 if type is None or section['sh_type'] == type:
176 yield section
177
178 def num_segments(self):
179 """ Number of segments in the file
180 """
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
187 # index 0.
188 if self['e_phnum'] < 0xffff:
189 return self['e_phnum']
190 else:
191 return self.get_section(0)['sh_info']
192
193 def get_segment(self, n):
194 """ Get the segment at index #n from the file (Segment object)
195 """
196 segment_header = self._get_segment_header(n)
197 return self._make_segment(segment_header)
198
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.
204 'PT_LOAD'.
205 """
206 for i in range(self.num_segments()):
207 segment = self.get_segment(i)
208 if type is None or segment['p_type'] == type:
209 yield segment
210
211 def address_offsets(self, start, size=1):
212 """ Yield a file offset for each ELF segment containing a memory region.
213
214 A memory region is defined by the range [start...start+size). The
215 offset of the region is yielded.
216 """
217 end = start + size
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']
223
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.
228 """
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'))
232
233 def get_dwarf_info(self, relocate_dwarf_sections=True, follow_links=True):
234 """ Return a DWARFInfo object representing the debugging information in
235 this file.
236
237 If relocate_dwarf_sections is True, relocations for DWARF sections
238 are looked up and applied.
239
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.
242 """
243 # Expect that has_dwarf_info was called, so at least .debug_info is
244 # present.
245 # Sections that aren't found will be passed as None to DWARFInfo.
246
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')
254
255 compressed = bool(self.get_section_by_name('.zdebug_info'))
256 if compressed:
257 section_names = tuple(map(lambda x: '.z' + x[1:], section_names))
258
259 # As it is loaded in the process image, .eh_frame cannot be compressed
260 section_names += ('.eh_frame', )
261
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
268
269 debug_sections = {}
270 for secname in section_names:
271 section = self.get_section_by_name(secname)
272 if section is None:
273 debug_sections[secname] = None
274 else:
275 dwarf_section = self._read_dwarf_section(
276 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
281
282 # Lookup if we have any of the .gnu_debugaltlink (GNU proprietary
283 # implementation) or .debug_sup sections, referencing a supplementary
284 # DWARF file
285
286 dwarfinfo = DWARFInfo(
287 config=DwarfConfig(
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]
309 )
310 if follow_links:
311 dwarfinfo.supplementary_dwarfinfo = self.get_supplementary_dwarfinfo(dwarfinfo)
312 return dwarfinfo
313
314
315 def get_supplementary_dwarfinfo(self, dwarfinfo):
316 """
317 Read supplementary dwarfinfo, from either the standared .debug_sup
318 section or the GNU proprietary .gnu_debugaltlink.
319 """
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()
325 stream.close()
326 return dwarf_info
327 return None
328
329
330 def has_ehabi_info(self):
331 """ Check whether this file appears to have arm exception handler index table.
332 """
333 return any(self.iter_sections(type='SHT_ARM_EXIDX'))
334
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.
339 """
340 _ret = []
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
347
348 def get_machine_arch(self):
349 """ Return the machine architecture, as detected from the ELF header.
350 """
351 architectures = {
352 'EM_M32' : 'AT&T WE 32100',
353 'EM_SPARC' : 'SPARC',
354 'EM_386' : 'x86',
355 'EM_68K' : 'Motorola 68000',
356 'EM_88K' : 'Motorola 88000',
357 'EM_IAMCU' : 'Intel MCU',
358 'EM_860' : 'Intel 80860',
359 'EM_MIPS' : 'MIPS',
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 S/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',
374 'EM_ARM' : 'ARM',
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',
396 'EM_X86_64' : 'x64',
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',
436 'EM_MAX' : 'MAX',
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',
442 'EM_SEP' : 'Sharp',
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',
535 'EM_CSKY' : 'C-SKY',
536 'EM_LOONGARCH' : 'LoongArch',
537 'EM_FRV' : 'Fujitsu FR-V'
538 }
539
540 return architectures.get(self['e_machine'], '<unknown>')
541
542 def get_shstrndx(self):
543 """ Find the string table section index for the section header table
544 """
545 # From https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html:
546 # If the section name string table section index is greater than or
547 # equal to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX
548 # (0xffff) and the actual index of the section name string table section
549 # is contained in the sh_link field of the section header at index 0.
550 if self['e_shstrndx'] != SHN_INDICES.SHN_XINDEX:
551 return self['e_shstrndx']
552 else:
553 return self._get_section_header(0)['sh_link']
554
555 #-------------------------------- PRIVATE --------------------------------#
556
557 def __getitem__(self, name):
558 """ Implement dict-like access to header entries
559 """
560 return self.header[name]
561
562 def _identify_file(self):
563 """ Verify the ELF file and identify its class and endianness.
564 """
565 # Note: this code reads the stream directly, without using ELFStructs,
566 # since we don't yet know its exact format. ELF was designed to be
567 # read like this - its e_ident field is word-size and endian agnostic.
568 self.stream.seek(0)
569 magic = self.stream.read(4)
570 elf_assert(magic == b'\x7fELF', 'Magic number does not match')
571
572 ei_class = self.stream.read(1)
573 if ei_class == b'\x01':
574 self.elfclass = 32
575 elif ei_class == b'\x02':
576 self.elfclass = 64
577 else:
578 raise ELFError('Invalid EI_CLASS %s' % repr(ei_class))
579
580 ei_data = self.stream.read(1)
581 if ei_data == b'\x01':
582 self.little_endian = True
583 elif ei_data == b'\x02':
584 self.little_endian = False
585 else:
586 raise ELFError('Invalid EI_DATA %s' % repr(ei_data))
587
588 def _section_offset(self, n):
589 """ Compute the offset of section #n in the file
590 """
591 return self['e_shoff'] + n * self['e_shentsize']
592
593 def _segment_offset(self, n):
594 """ Compute the offset of segment #n in the file
595 """
596 return self['e_phoff'] + n * self['e_phentsize']
597
598 def _make_segment(self, segment_header):
599 """ Create a Segment object of the appropriate type
600 """
601 segtype = segment_header['p_type']
602 if segtype == 'PT_INTERP':
603 return InterpSegment(segment_header, self.stream)
604 elif segtype == 'PT_DYNAMIC':
605 return DynamicSegment(segment_header, self.stream, self)
606 elif segtype == 'PT_NOTE':
607 return NoteSegment(segment_header, self.stream, self)
608 else:
609 return Segment(segment_header, self.stream)
610
611 def _get_section_header(self, n):
612 """ Find the header of section #n, parse it and return the struct
613 """
614
615 stream_pos = self._section_offset(n)
616 if stream_pos > self.stream_len:
617 return None
618
619 return struct_parse(
620 self.structs.Elf_Shdr,
621 self.stream,
622 stream_pos=stream_pos)
623
624 def _get_section_name(self, section_header):
625 """ Given a section header, find this section's name in the file's
626 string table
627 """
628 if self._section_header_stringtable is None:
629 raise ELFParseError("String Table not found")
630
631 name_offset = section_header['sh_name']
632 return self._section_header_stringtable.get_string(name_offset)
633
634 def _make_section(self, section_header):
635 """ Create a section object of the appropriate type
636 """
637 name = self._get_section_name(section_header)
638 sectype = section_header['sh_type']
639
640 if sectype == 'SHT_STRTAB':
641 return StringTableSection(section_header, name, self)
642 elif sectype == 'SHT_NULL':
643 return NullSection(section_header, name, self)
644 elif sectype in ('SHT_SYMTAB', 'SHT_DYNSYM', 'SHT_SUNW_LDYNSYM'):
645 return self._make_symbol_table_section(section_header, name)
646 elif sectype == 'SHT_SYMTAB_SHNDX':
647 return self._make_symbol_table_index_section(section_header, name)
648 elif sectype == 'SHT_SUNW_syminfo':
649 return self._make_sunwsyminfo_table_section(section_header, name)
650 elif sectype == 'SHT_GNU_verneed':
651 return self._make_gnu_verneed_section(section_header, name)
652 elif sectype == 'SHT_GNU_verdef':
653 return self._make_gnu_verdef_section(section_header, name)
654 elif sectype == 'SHT_GNU_versym':
655 return self._make_gnu_versym_section(section_header, name)
656 elif sectype in ('SHT_REL', 'SHT_RELA'):
657 return RelocationSection(section_header, name, self)
658 elif sectype == 'SHT_DYNAMIC':
659 return DynamicSection(section_header, name, self)
660 elif sectype == 'SHT_NOTE':
661 return NoteSection(section_header, name, self)
662 elif sectype == 'SHT_PROGBITS' and name == '.stab':
663 return StabSection(section_header, name, self)
664 elif sectype == 'SHT_ARM_ATTRIBUTES':
665 return ARMAttributesSection(section_header, name, self)
666 elif sectype == 'SHT_RISCV_ATTRIBUTES':
667 return RISCVAttributesSection(section_header, name, self)
668 elif sectype == 'SHT_HASH':
669 return self._make_elf_hash_section(section_header, name)
670 elif sectype == 'SHT_GNU_HASH':
671 return self._make_gnu_hash_section(section_header, name)
672 elif sectype == 'SHT_RELR':
673 return RelrRelocationSection(section_header, name, self)
674 else:
675 return Section(section_header, name, self)
676
677 def _make_section_name_map(self):
678 self._section_name_map = {}
679 for i, sec in enumerate(self.iter_sections()):
680 self._section_name_map[sec.name] = i
681
682 def _make_symbol_table_section(self, section_header, name):
683 """ Create a SymbolTableSection
684 """
685 linked_strtab_index = section_header['sh_link']
686 strtab_section = self.get_section(linked_strtab_index)
687 return SymbolTableSection(
688 section_header, name,
689 elffile=self,
690 stringtable=strtab_section)
691
692 def _make_symbol_table_index_section(self, section_header, name):
693 """ Create a SymbolTableIndexSection object
694 """
695 linked_symtab_index = section_header['sh_link']
696 return SymbolTableIndexSection(
697 section_header, name, elffile=self,
698 symboltable=linked_symtab_index)
699
700 def _make_sunwsyminfo_table_section(self, section_header, name):
701 """ Create a SUNWSyminfoTableSection
702 """
703 linked_strtab_index = section_header['sh_link']
704 strtab_section = self.get_section(linked_strtab_index)
705 return SUNWSyminfoTableSection(
706 section_header, name,
707 elffile=self,
708 symboltable=strtab_section)
709
710 def _make_gnu_verneed_section(self, section_header, name):
711 """ Create a GNUVerNeedSection
712 """
713 linked_strtab_index = section_header['sh_link']
714 strtab_section = self.get_section(linked_strtab_index)
715 return GNUVerNeedSection(
716 section_header, name,
717 elffile=self,
718 stringtable=strtab_section)
719
720 def _make_gnu_verdef_section(self, section_header, name):
721 """ Create a GNUVerDefSection
722 """
723 linked_strtab_index = section_header['sh_link']
724 strtab_section = self.get_section(linked_strtab_index)
725 return GNUVerDefSection(
726 section_header, name,
727 elffile=self,
728 stringtable=strtab_section)
729
730 def _make_gnu_versym_section(self, section_header, name):
731 """ Create a GNUVerSymSection
732 """
733 linked_strtab_index = section_header['sh_link']
734 strtab_section = self.get_section(linked_strtab_index)
735 return GNUVerSymSection(
736 section_header, name,
737 elffile=self,
738 symboltable=strtab_section)
739
740 def _make_elf_hash_section(self, section_header, name):
741 linked_symtab_index = section_header['sh_link']
742 symtab_section = self.get_section(linked_symtab_index)
743 return ELFHashSection(
744 section_header, name, self, symtab_section
745 )
746
747 def _make_gnu_hash_section(self, section_header, name):
748 linked_symtab_index = section_header['sh_link']
749 symtab_section = self.get_section(linked_symtab_index)
750 return GNUHashSection(
751 section_header, name, self, symtab_section
752 )
753
754 def _get_segment_header(self, n):
755 """ Find the header of segment #n, parse it and return the struct
756 """
757 return struct_parse(
758 self.structs.Elf_Phdr,
759 self.stream,
760 stream_pos=self._segment_offset(n))
761
762 def _get_section_header_stringtable(self):
763 """ Get the string table section corresponding to the section header
764 table.
765 """
766 stringtable_section_num = self.get_shstrndx()
767
768 stringtable_section_header = self._get_section_header(stringtable_section_num)
769 if stringtable_section_header is None:
770 return None
771
772 return StringTableSection(
773 header=stringtable_section_header,
774 name='',
775 elffile=self)
776
777 def _parse_elf_header(self):
778 """ Parses the ELF file header and assigns the result to attributes
779 of this object.
780 """
781 return struct_parse(self.structs.Elf_Ehdr, self.stream, stream_pos=0)
782
783 def _read_dwarf_section(self, section, relocate_dwarf_sections):
784 """ Read the contents of a DWARF section from the stream and return a
785 DebugSectionDescriptor. Apply relocations if asked to.
786 """
787 # The section data is read into a new stream, for processing
788 section_stream = BytesIO()
789 section_stream.write(section.data())
790
791 if relocate_dwarf_sections:
792 reloc_handler = RelocationHandler(self)
793 reloc_section = reloc_handler.find_relocations_for_section(section)
794 if reloc_section is not None:
795 reloc_handler.apply_section_relocations(
796 section_stream, reloc_section)
797
798 return DebugSectionDescriptor(
799 stream=section_stream,
800 name=section.name,
801 global_offset=section['sh_offset'],
802 size=section.data_size,
803 address=section['sh_addr'])
804
805 @staticmethod
806 def _decompress_dwarf_section(section):
807 """ Returns the uncompressed contents of the provided DWARF section.
808 """
809 # TODO: support other compression formats from readelf.c
810 assert section.size > 12, 'Unsupported compression format.'
811
812 section.stream.seek(0)
813 # According to readelf.c the content should contain "ZLIB"
814 # followed by the uncompressed section size - 8 bytes in
815 # big-endian order
816 compression_type = section.stream.read(4)
817 assert compression_type == b'ZLIB', \
818 'Invalid compression type: %r' % (compression_type)
819
820 uncompressed_size = struct.unpack('>Q', section.stream.read(8))[0]
821
822 decompressor = zlib.decompressobj()
823 uncompressed_stream = BytesIO()
824 while True:
825 chunk = section.stream.read(PAGESIZE)
826 if not chunk:
827 break
828 uncompressed_stream.write(decompressor.decompress(chunk))
829 uncompressed_stream.write(decompressor.flush())
830
831 uncompressed_stream.seek(0, io.SEEK_END)
832 size = uncompressed_stream.tell()
833 assert uncompressed_size == size, \
834 'Wrong uncompressed size: expected %r, but got %r' % (
835 uncompressed_size, size,
836 )
837
838 return section._replace(stream=uncompressed_stream, size=size)
839
840 def close(self):
841 self.stream.close()
842
843 def __enter__(self):
844 return self
845
846 def __exit__(self, type, value, traceback):
847 self.close()