blob: 85b8f0b1da530e59f62be5d9746c90df5fc0676e [file] [log] [blame]
Eli Bendersky3edefab2011-09-16 14:52:54 +03001#!/usr/bin/env python
Eli Bendersky933f6992011-09-09 08:11:06 +03002#-------------------------------------------------------------------------------
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +03003# scripts/readelf.py
Eli Bendersky933f6992011-09-09 08:11:06 +03004#
5# A clone of 'readelf' in Python, based on the pyelftools library
6#
7# Eli Bendersky (eliben@gmail.com)
8# This code is in the public domain
9#-------------------------------------------------------------------------------
Eli Bendersky53e86db2011-09-18 06:04:30 +030010import os, sys
Eli Bendersky933f6992011-09-09 08:11:06 +030011from optparse import OptionParser
Eli Bendersky53e86db2011-09-18 06:04:30 +030012import string
13
Eli Bendersky933f6992011-09-09 08:11:06 +030014
15# If elftools is not installed, maybe we're running from the root or scripts
16# dir of the source distribution
17#
18try:
19 import elftools
20except ImportError:
21 sys.path.extend(['.', '..'])
22
Eli Bendersky0b27ba42011-09-17 06:44:02 +030023from elftools import __version__
Eli Bendersky933f6992011-09-09 08:11:06 +030024from elftools.common.exceptions import ELFError
25from elftools.elf.elffile import ELFFile
Eli Bendersky3f4de3e2011-09-14 05:58:06 +030026from elftools.elf.segments import InterpSegment
Eli Bendersky84066b22011-09-20 06:48:52 +030027from elftools.elf.sections import SymbolTableSection, RelocationSection
Eli Bendersky933f6992011-09-09 08:11:06 +030028from elftools.elf.descriptions import (
29 describe_ei_class, describe_ei_data, describe_ei_version,
Eli Benderskyde8d71e2011-09-09 08:22:35 +030030 describe_ei_osabi, describe_e_type, describe_e_machine,
Eli Bendersky26de2ac2011-09-13 06:50:28 +030031 describe_e_version_numeric, describe_p_type, describe_p_flags,
Eli Bendersky377bd862011-09-16 11:10:44 +030032 describe_sh_type, describe_sh_flags,
Eli Bendersky3edefab2011-09-16 14:52:54 +030033 describe_symbol_type, describe_symbol_bind, describe_symbol_visibility,
Eli Bendersky89a824f2011-09-23 10:59:59 +030034 describe_symbol_shndx, describe_reloc_type,
Eli Bendersky933f6992011-09-09 08:11:06 +030035 )
eliben0fc47af2011-10-02 13:47:12 +020036from elftools.dwarf.dwarfinfo import DWARFInfo, DebugSectionLocator
elibenaee11d22011-10-26 10:42:34 +020037from elftools.dwarf.descriptions import describe_attr_value
Eli Bendersky933f6992011-09-09 08:11:06 +030038
39
40class ReadElf(object):
41 """ display_* methods are used to emit output into the output stream
42 """
43 def __init__(self, file, output):
44 """ file:
45 stream object with the ELF file to read
46
47 output:
48 output stream to write to
49 """
50 self.elffile = ELFFile(file)
51 self.output = output
eliben0fc47af2011-10-02 13:47:12 +020052
53 # Lazily initialized if a debug dump is requested
54 self._dwarfinfo = None
Eli Bendersky933f6992011-09-09 08:11:06 +030055
56 def display_file_header(self):
57 """ Display the ELF file header
58 """
59 self._emitline('ELF Header:')
60 self._emit(' Magic: ')
61 self._emitline(' '.join('%2.2x' % ord(b)
62 for b in self.elffile.e_ident_raw))
63 header = self.elffile.header
64 e_ident = header['e_ident']
65 self._emitline(' Class: %s' %
66 describe_ei_class(e_ident['EI_CLASS']))
67 self._emitline(' Data: %s' %
68 describe_ei_data(e_ident['EI_DATA']))
69 self._emitline(' Version: %s' %
70 describe_ei_version(e_ident['EI_VERSION']))
71 self._emitline(' OS/ABI: %s' %
72 describe_ei_osabi(e_ident['EI_OSABI']))
73 self._emitline(' ABI Version: %d' %
74 e_ident['EI_ABIVERSION'])
75 self._emitline(' Type: %s' %
76 describe_e_type(header['e_type']))
Eli Benderskyde8d71e2011-09-09 08:22:35 +030077 self._emitline(' Machine: %s' %
78 describe_e_machine(header['e_machine']))
79 self._emitline(' Version: %s' %
80 describe_e_version_numeric(header['e_version']))
Eli Benderskyd62928d2011-09-09 10:05:57 +030081 self._emitline(' Entry point address: %s' %
Eli Bendersky26de2ac2011-09-13 06:50:28 +030082 self._format_hex(header['e_entry']))
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +030083 self._emit(' Start of program headers: %s' %
Eli Benderskyd62928d2011-09-09 10:05:57 +030084 header['e_phoff'])
85 self._emitline(' (bytes into file)')
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +030086 self._emit(' Start of section headers: %s' %
Eli Benderskyd62928d2011-09-09 10:05:57 +030087 header['e_shoff'])
88 self._emitline(' (bytes into file)')
89 self._emitline(' Flags: %s' %
Eli Bendersky26de2ac2011-09-13 06:50:28 +030090 self._format_hex(header['e_flags']))
Eli Benderskyd62928d2011-09-09 10:05:57 +030091 self._emitline(' Size of this header: %s (bytes)' %
92 header['e_ehsize'])
93 self._emitline(' Size of program headers: %s (bytes)' %
94 header['e_phentsize'])
95 self._emitline(' Number of program headers: %s' %
96 header['e_phnum'])
97 self._emitline(' Size of section headers: %s (bytes)' %
98 header['e_shentsize'])
99 self._emitline(' Number of section headers: %s' %
100 header['e_shnum'])
101 self._emitline(' Section header string table index: %s' %
102 header['e_shstrndx'])
Eli Bendersky933f6992011-09-09 08:11:06 +0300103
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300104 def display_program_headers(self, show_heading=True):
105 """ Display the ELF program headers.
106 If show_heading is True, displays the heading for this information
107 (Elf file type is...)
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300108 """
109 self._emitline()
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300110 if self.elffile.num_segments() == 0:
111 self._emitline('There are no program headers in this file.')
112 return
113
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300114 elfheader = self.elffile.header
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300115 if show_heading:
116 self._emitline('Elf file type is %s' %
117 describe_e_type(elfheader['e_type']))
118 self._emitline('Entry point is %s' %
119 self._format_hex(elfheader['e_entry']))
120 # readelf weirness - why isn't e_phoff printed as hex? (for section
121 # headers, it is...)
122 self._emitline('There are %s program headers, starting at offset %s' % (
123 elfheader['e_phnum'], elfheader['e_phoff']))
124 self._emitline()
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300125
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300126 self._emitline('Program Headers:')
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300127
128 # Now comes the table of program headers with their attributes. Note
129 # that due to different formatting constraints of 32-bit and 64-bit
130 # addresses, there are some conditions on elfclass here.
131 #
132 # First comes the table heading
133 #
134 if self.elffile.elfclass == 32:
135 self._emitline(' Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align')
136 else:
137 self._emitline(' Type Offset VirtAddr PhysAddr')
138 self._emitline(' FileSiz MemSiz Flags Align')
139
140 # Now the entries
141 #
142 for segment in self.elffile.iter_segments():
143 self._emit(' %-14s ' % describe_p_type(segment['p_type']))
144
145 if self.elffile.elfclass == 32:
146 self._emitline('%s %s %s %s %s %-3s %s' % (
147 self._format_hex(segment['p_offset'], fieldsize=6),
148 self._format_hex(segment['p_vaddr'], fullhex=True),
149 self._format_hex(segment['p_paddr'], fullhex=True),
150 self._format_hex(segment['p_filesz'], fieldsize=5),
151 self._format_hex(segment['p_memsz'], fieldsize=5),
152 describe_p_flags(segment['p_flags']),
153 self._format_hex(segment['p_align'])))
Eli Benderskya41c3c02011-09-14 06:18:28 +0300154 else: # 64
155 self._emitline('%s %s %s' % (
156 self._format_hex(segment['p_offset'], fullhex=True),
157 self._format_hex(segment['p_vaddr'], fullhex=True),
158 self._format_hex(segment['p_paddr'], fullhex=True)))
159 self._emitline(' %s %s %-3s %s' % (
160 self._format_hex(segment['p_filesz'], fullhex=True),
161 self._format_hex(segment['p_memsz'], fullhex=True),
162 describe_p_flags(segment['p_flags']),
163 # lead0x set to False for p_align, to mimic readelf.
164 # No idea why the difference from 32-bit mode :-|
165 self._format_hex(segment['p_align'], lead0x=False)))
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300166
Eli Bendersky3f4de3e2011-09-14 05:58:06 +0300167 if isinstance(segment, InterpSegment):
168 self._emitline(' [Requesting program interpreter: %s]' %
169 segment.get_interp_name())
170
Eli Bendersky58585b02011-09-15 07:07:54 +0300171 # Sections to segments mapping
172 #
173 if self.elffile.num_sections() == 0:
174 # No sections? We're done
175 return
176
177 self._emitline('\n Section to Segment mapping:')
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300178 self._emitline(' Segment Sections...')
Eli Bendersky58585b02011-09-15 07:07:54 +0300179
180 for nseg, segment in enumerate(self.elffile.iter_segments()):
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300181 self._emit(' %2.2d ' % nseg)
Eli Bendersky58585b02011-09-15 07:07:54 +0300182
183 for section in self.elffile.iter_sections():
184 if ( not section.is_null() and
185 segment.section_in_segment(section)):
186 self._emit('%s ' % section.name)
187
188 self._emitline('')
189
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300190 def display_section_headers(self, show_heading=True):
Eli Bendersky377bd862011-09-16 11:10:44 +0300191 """ Display the ELF section headers
192 """
193 elfheader = self.elffile.header
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300194 if show_heading:
195 self._emitline('There are %s section headers, starting at offset %s' % (
196 elfheader['e_shnum'], self._format_hex(elfheader['e_shoff'])))
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300197
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300198 self._emitline('\nSection Header%s:' % (
Eli Bendersky377bd862011-09-16 11:10:44 +0300199 's' if elfheader['e_shnum'] > 1 else ''))
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300200
Eli Bendersky377bd862011-09-16 11:10:44 +0300201 # Different formatting constraints of 32-bit and 64-bit addresses
202 #
203 if self.elffile.elfclass == 32:
204 self._emitline(' [Nr] Name Type Addr Off Size ES Flg Lk Inf Al')
205 else:
206 self._emitline(' [Nr] Name Type Address Offset')
207 self._emitline(' Size EntSize Flags Link Info Align')
208
209 # Now the entries
210 #
211 for nsec, section in enumerate(self.elffile.iter_sections()):
212 self._emit(' [%2u] %-17.17s %-15.15s ' % (
213 nsec, section.name, describe_sh_type(section['sh_type'])))
214
215 if self.elffile.elfclass == 32:
216 self._emitline('%s %s %s %s %3s %2s %3s %2s' % (
217 self._format_hex(section['sh_addr'], fieldsize=8, lead0x=False),
218 self._format_hex(section['sh_offset'], fieldsize=6, lead0x=False),
219 self._format_hex(section['sh_size'], fieldsize=6, lead0x=False),
220 self._format_hex(section['sh_entsize'], fieldsize=2, lead0x=False),
221 describe_sh_flags(section['sh_flags']),
222 section['sh_link'], section['sh_info'],
223 section['sh_addralign']))
224 else: # 64
225 self._emitline(' %s %s' % (
226 self._format_hex(section['sh_addr'], fullhex=True, lead0x=False),
227 self._format_hex(section['sh_offset'],
228 fieldsize=16 if section['sh_offset'] > 0xffffffff else 8,
229 lead0x=False)))
230 self._emitline(' %s %s %3s %2s %3s %s' % (
231 self._format_hex(section['sh_size'], fullhex=True, lead0x=False),
232 self._format_hex(section['sh_entsize'], fullhex=True, lead0x=False),
233 describe_sh_flags(section['sh_flags']),
234 section['sh_link'], section['sh_info'],
235 section['sh_addralign']))
236
237 self._emitline('Key to Flags:')
Eli Bendersky93e630d2011-11-16 07:22:57 +0200238 self._emit(' W (write), A (alloc), X (execute), M (merge), S (strings)')
239 if self.elffile['e_machine'] in ('EM_X86_64', 'EM_L10M'):
240 self._emitline(', l (large)')
241 else:
242 self._emitline()
243 self._emitline(' I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)')
Eli Bendersky377bd862011-09-16 11:10:44 +0300244 self._emitline(' O (extra OS processing required) o (OS specific), p (processor specific)')
245
Eli Bendersky3edefab2011-09-16 14:52:54 +0300246 def display_symbol_tables(self):
247 """ Display the symbol tables contained in the file
248 """
249 for section in self.elffile.iter_sections():
250 if not isinstance(section, SymbolTableSection):
251 continue
252
253 if section['sh_entsize'] == 0:
254 self._emitline("\nSymbol table '%s' has a sh_entsize of zero!" % (
255 section.name))
256 continue
257
258 self._emitline("\nSymbol table '%s' contains %s entries:" % (
259 section.name, section.num_symbols()))
260
261 if self.elffile.elfclass == 32:
262 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
263 else: # 64
264 self._emitline(' Num: Value Size Type Bind Vis Ndx Name')
265
266 for nsym, symbol in enumerate(section.iter_symbols()):
Eli Benderskyb6fa3652011-09-16 15:20:20 +0300267 # symbol names are truncated to 25 chars, similarly to readelf
268 self._emitline('%6d: %s %5d %-7s %-6s %-7s %4s %.25s' % (
Eli Bendersky3edefab2011-09-16 14:52:54 +0300269 nsym,
270 self._format_hex(symbol['st_value'], fullhex=True, lead0x=False),
271 symbol['st_size'],
272 describe_symbol_type(symbol['st_info']['type']),
273 describe_symbol_bind(symbol['st_info']['bind']),
274 describe_symbol_visibility(symbol['st_other']['visibility']),
275 describe_symbol_shndx(symbol['st_shndx']),
276 symbol.name))
277
Eli Bendersky84066b22011-09-20 06:48:52 +0300278 def display_relocations(self):
279 """ Display the relocations contained in the file
280 """
281 has_relocation_sections = False
282 for section in self.elffile.iter_sections():
283 if not isinstance(section, RelocationSection):
284 continue
285
286 has_relocation_sections = True
287 self._emitline("\nRelocation section '%s' at offset %s contains %s entries:" % (
Eli Bendersky7c1ffa62011-09-22 06:37:07 +0300288 section.name,
289 self._format_hex(section['sh_offset']),
290 section.num_relocations()))
Eli Bendersky89a824f2011-09-23 10:59:59 +0300291 if section.is_RELA():
292 self._emitline(" Offset Info Type Sym. Value Sym. Name + Addend")
293 else:
294 self._emitline(" Offset Info Type Sym.Value Sym. Name")
295
296 # The symbol table section pointed to in sh_link
297 symtable = self.elffile.get_section(section['sh_link'])
298
Eli Bendersky84066b22011-09-20 06:48:52 +0300299 for rel in section.iter_relocations():
Eli Bendersky89a824f2011-09-23 10:59:59 +0300300 hexwidth = 8 if self.elffile.elfclass == 32 else 12
Eli Bendersky099d48f2011-09-23 12:03:48 +0300301 self._emit('%s %s %-17.17s' % (
Eli Bendersky89a824f2011-09-23 10:59:59 +0300302 self._format_hex(rel['r_offset'],
303 fieldsize=hexwidth, lead0x=False),
304 self._format_hex(rel['r_info'],
305 fieldsize=hexwidth, lead0x=False),
306 describe_reloc_type(
Eli Bendersky067b3fd2011-11-18 12:02:57 +0200307 rel['r_info_type'], self.elffile)))
Eli Bendersky099d48f2011-09-23 12:03:48 +0300308
309 if rel['r_info_sym'] == 0:
310 self._emitline()
311 continue
312
313 symbol = symtable.get_symbol(rel['r_info_sym'])
Eli Benderskyadf707a2011-09-23 15:23:41 +0300314 # Some symbols have zero 'st_name', so instead what's used is
315 # the name of the section they point at
316 if symbol['st_name'] == 0:
317 symsec = self.elffile.get_section(symbol['st_shndx'])
318 symbol_name = symsec.name
319 else:
320 symbol_name = symbol.name
Eli Bendersky6434a962011-09-23 17:14:08 +0300321 self._emit(' %s %s%22.22s' % (
Eli Bendersky89a824f2011-09-23 10:59:59 +0300322 self._format_hex(
323 symbol['st_value'],
324 fullhex=True, lead0x=False),
325 ' ' if self.elffile.elfclass == 32 else '',
Eli Benderskyadf707a2011-09-23 15:23:41 +0300326 symbol_name))
Eli Bendersky89a824f2011-09-23 10:59:59 +0300327 if section.is_RELA():
328 self._emit(' %s %x' % (
329 '+' if rel['r_addend'] >= 0 else '-',
330 abs(rel['r_addend'])))
331 self._emitline()
Eli Bendersky84066b22011-09-20 06:48:52 +0300332
333 if not has_relocation_sections:
334 self._emitline('\nThere are no relocations in this file.')
335
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300336 def display_hex_dump(self, section_spec):
337 """ Display a hex dump of a section. section_spec is either a section
338 number or a name.
339 """
eliben54e39b22011-09-19 13:10:57 +0300340 section = self._section_from_spec(section_spec)
341 if section is None:
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300342 self._emitline("Section '%s' does not exist in the file!" % (
343 section_spec))
344 return
345
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300346 self._emitline("\nHex dump of section '%s':" % section.name)
Eli Benderskyadf707a2011-09-23 15:23:41 +0300347 self._note_relocs_for_section(section)
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300348 addr = section['sh_addr']
349 data = section.data()
350 dataptr = 0
351
352 while dataptr < len(data):
353 bytesleft = len(data) - dataptr
354 # chunks of 16 bytes per line
355 linebytes = 16 if bytesleft > 16 else bytesleft
356
357 self._emit(' %s ' % self._format_hex(addr, fieldsize=8))
358 for i in range(16):
359 if i < linebytes:
360 self._emit('%2.2x' % ord(data[dataptr + i]))
361 else:
362 self._emit(' ')
363 if i % 4 == 3:
364 self._emit(' ')
365
366 for i in range(linebytes):
367 c = data[dataptr + i]
Eli Bendersky099d48f2011-09-23 12:03:48 +0300368 if c >= ' ' and ord(c) < 0x7f:
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300369 self._emit(c)
370 else:
371 self._emit('.')
372
373 self._emitline()
374 addr += linebytes
375 dataptr += linebytes
376
377 self._emitline()
378
Eli Bendersky53e86db2011-09-18 06:04:30 +0300379 def display_string_dump(self, section_spec):
380 """ Display a strings dump of a section. section_spec is either a
381 section number or a name.
382 """
eliben54e39b22011-09-19 13:10:57 +0300383 section = self._section_from_spec(section_spec)
384 if section is None:
Eli Bendersky53e86db2011-09-18 06:04:30 +0300385 self._emitline("Section '%s' does not exist in the file!" % (
386 section_spec))
387 return
388
389 printables = set(string.printable)
Eli Bendersky53e86db2011-09-18 06:04:30 +0300390 self._emitline("\nString dump of section '%s':" % section.name)
391
392 found = False
393 data = section.data()
394 dataptr = 0
395
396 while dataptr < len(data):
397 while dataptr < len(data) and data[dataptr] not in printables:
398 dataptr += 1
399
400 if dataptr >= len(data):
401 break
402
403 endptr = dataptr
404 while endptr < len(data) and data[endptr] != '\x00':
405 endptr += 1
406
407 found = True
408 self._emitline(' [%6x] %s' % (
409 dataptr, data[dataptr:endptr]))
410
411 dataptr = endptr
412
413 if not found:
414 self._emitline(' No strings found in this section.')
415 else:
416 self._emitline()
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300417
eliben0fc47af2011-10-02 13:47:12 +0200418 def display_debug_dump(self, section_name):
419 """ Dump a DWARF section
420 """
421 self._init_dwarfinfo()
422 if self._dwarfinfo is None:
423 return
424
425 if section_name == 'info':
426 self._dump_debug_info()
427 else:
428 self._emitline('debug dump not yet supported for "%s"' % section_name)
429
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300430 def _format_hex(self, addr, fieldsize=None, fullhex=False, lead0x=True):
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300431 """ Format an address into a hexadecimal string.
Eli Benderskyd62928d2011-09-09 10:05:57 +0300432
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300433 fieldsize:
434 Size of the hexadecimal field (with leading zeros to fit the
435 address into. For example with fieldsize=8, the format will
436 be %08x
437 If None, the minimal required field size will be used.
438
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300439 fullhex:
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300440 If True, override fieldsize to set it to the maximal size
441 needed for the elfclass
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300442
443 lead0x:
444 If True, leading 0x is added
Eli Benderskyd62928d2011-09-09 10:05:57 +0300445 """
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300446 s = '0x' if lead0x else ''
447 if fullhex:
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300448 fieldsize = 8 if self.elffile.elfclass == 32 else 16
449 if fieldsize is None:
450 field = '%x'
Eli Bendersky6a12cde2011-09-09 10:23:16 +0300451 else:
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300452 field = '%' + '0%sx' % fieldsize
453 return s + field % addr
Eli Benderskyd62928d2011-09-09 10:05:57 +0300454
eliben54e39b22011-09-19 13:10:57 +0300455 def _section_from_spec(self, spec):
456 """ Retrieve a section given a "spec" (either number or name).
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300457 Return None if no such section exists in the file.
458 """
459 try:
460 num = int(spec)
eliben54e39b22011-09-19 13:10:57 +0300461 if num < self.elffile.num_sections():
462 return self.elffile.get_section(num)
463 else:
464 return None
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300465 except ValueError:
466 # Not a number. Must be a name then
eliben54e39b22011-09-19 13:10:57 +0300467 return self.elffile.get_section_by_name(spec)
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300468
Eli Benderskyadf707a2011-09-23 15:23:41 +0300469 def _note_relocs_for_section(self, section):
470 """ If there are relocation sections pointing to the givne section,
471 emit a note about it.
472 """
473 for relsec in self.elffile.iter_sections():
474 if isinstance(relsec, RelocationSection):
475 info_idx = relsec['sh_info']
476 if self.elffile.get_section(info_idx) == section:
477 self._emitline(' Note: This section has relocations against it, but these have NOT been applied to this dump.')
478 return
479
eliben0fc47af2011-10-02 13:47:12 +0200480 def _init_dwarfinfo(self):
481 """ Initialize the DWARF info contained in the file and assign it to
482 self._dwarfinfo.
483 Leave self._dwarfinfo at None if no DWARF info was found in the file
484 """
485 if self._dwarfinfo is not None:
486 return
487
488 if self.elffile.has_dwarf_info():
489 self._dwarfinfo = self.elffile.get_dwarf_info()
490 else:
491 self._dwarfinfo = None
492
493 def _dump_debug_info(self):
494 """ Dump the debugging info section.
495 """
eliben985c2c12011-11-14 17:53:23 +0200496 self._emitline('Contents of the .debug_info section:\n')
497
eliben0fc47af2011-10-02 13:47:12 +0200498 # Offset of the .debug_info section in the stream
499 section_offset = self._dwarfinfo.debug_info_loc.offset
Eli Bendersky6062bf72011-11-23 06:54:40 +0200500
501 print '&&& section_offset', section_offset
eliben0fc47af2011-10-02 13:47:12 +0200502
503 for cu in self._dwarfinfo.iter_CUs():
eliben985c2c12011-11-14 17:53:23 +0200504 self._emitline(' Compilation Unit @ offset %s:' %
eliben0fc47af2011-10-02 13:47:12 +0200505 self._format_hex(cu.cu_offset - section_offset))
506 self._emitline(' Length: %s (%s)' % (
507 self._format_hex(cu['unit_length']),
508 '%s-bit' % cu.dwarf_format()))
509 self._emitline(' Version: %s' % cu['version']),
510 self._emitline(' Abbrev Offset: %s' % cu['debug_abbrev_offset']),
511 self._emitline(' Pointer Size: %s' % cu['address_size'])
512
513 # The nesting depth of each DIE within the tree of DIEs must be
514 # displayed. To implement this, a counter is incremented each time
515 # the current DIE has children, and decremented when a null die is
516 # encountered. Due to the way the DIE tree is serialized, this will
517 # correctly reflect the nesting depth
518 #
519 die_depth = 0
520 for die in cu.iter_DIEs():
521 if die.is_null():
522 die_depth -= 1
523 continue
elibenaee11d22011-10-26 10:42:34 +0200524 self._emitline(' <%s><%x>: Abbrev Number: %s (%s)' % (
525 die_depth,
526 die.offset - section_offset,
527 die.abbrev_code,
528 die.tag))
529
Eli Bendersky41c971a2011-10-27 14:29:32 +0200530 for attr in die.attributes.itervalues():
elibenaee11d22011-10-26 10:42:34 +0200531 self._emitline(' <%2x> %-18s: %s' % (
532 attr.offset - section_offset,
Eli Bendersky41c971a2011-10-27 14:29:32 +0200533 attr.name,
eliben3bc9c342011-10-26 13:10:58 +0200534 describe_attr_value(
Eli Bendersky5be3be82011-10-27 14:28:12 +0200535 attr, die, section_offset)))
eliben0fc47af2011-10-02 13:47:12 +0200536
537 if die.has_children:
538 die_depth += 1
elibenaee11d22011-10-26 10:42:34 +0200539
eliben0fc47af2011-10-02 13:47:12 +0200540
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300541 def _emit(self, s=''):
Eli Bendersky933f6992011-09-09 08:11:06 +0300542 """ Emit an object to output
543 """
544 self.output.write(str(s))
Eli Benderskyd62928d2011-09-09 10:05:57 +0300545
Eli Bendersky26de2ac2011-09-13 06:50:28 +0300546 def _emitline(self, s=''):
Eli Bendersky933f6992011-09-09 08:11:06 +0300547 """ Emit an object to output, followed by a newline
548 """
549 self.output.write(str(s) + '\n')
550
551
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300552SCRIPT_DESCRIPTION = 'Display information about the contents of ELF format files'
553VERSION_STRING = '%%prog: based on pyelftools %s' % __version__
554
555
Eli Bendersky933f6992011-09-09 08:11:06 +0300556def main():
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300557 # parse the command-line arguments and invoke ReadElf
Eli Bendersky40eb1702011-09-16 16:59:52 +0300558 optparser = OptionParser(
Eli Benderskyecde41b2011-09-16 17:16:20 +0300559 usage='usage: %prog [options] <elf-file>',
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300560 description=SCRIPT_DESCRIPTION,
Eli Bendersky40eb1702011-09-16 16:59:52 +0300561 add_help_option=False, # -h is a real option of readelf
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300562 prog='readelf.py',
563 version=VERSION_STRING)
Eli Bendersky40eb1702011-09-16 16:59:52 +0300564 optparser.add_option('-H', '--help',
565 action='store_true', dest='help',
566 help='Display this information')
567 optparser.add_option('-h', '--file-header',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300568 action='store_true', dest='show_file_header',
569 help='Display the ELF file header')
Eli Bendersky40eb1702011-09-16 16:59:52 +0300570 optparser.add_option('-l', '--program-headers', '--segments',
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300571 action='store_true', dest='show_program_header',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300572 help='Display the program headers')
Eli Bendersky40eb1702011-09-16 16:59:52 +0300573 optparser.add_option('-S', '--section-headers', '--sections',
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300574 action='store_true', dest='show_section_header',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300575 help="Display the sections' headers")
Eli Bendersky40eb1702011-09-16 16:59:52 +0300576 optparser.add_option('-e', '--headers',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300577 action='store_true', dest='show_all_headers',
578 help='Equivalent to: -h -l -S')
Eli Bendersky40eb1702011-09-16 16:59:52 +0300579 optparser.add_option('-s', '--symbols', '--syms',
Eli Benderskyecde41b2011-09-16 17:16:20 +0300580 action='store_true', dest='show_symbols',
581 help='Display the symbol table')
Eli Bendersky84066b22011-09-20 06:48:52 +0300582 optparser.add_option('-r', '--relocs',
583 action='store_true', dest='show_relocs',
584 help='Display the relocations (if present)')
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300585 optparser.add_option('-x', '--hex-dump',
586 action='store', dest='show_hex_dump', metavar='<number|name>',
587 help='Dump the contents of section <number|name> as bytes')
Eli Bendersky53e86db2011-09-18 06:04:30 +0300588 optparser.add_option('-p', '--string-dump',
589 action='store', dest='show_string_dump', metavar='<number|name>',
590 help='Dump the contents of section <number|name> as strings')
eliben0fc47af2011-10-02 13:47:12 +0200591 optparser.add_option('--debug-dump',
592 action='store', dest='debug_dump_section', metavar='<section>',
593 help='Display the contents of DWARF debug sections')
Eli Bendersky53e86db2011-09-18 06:04:30 +0300594
Eli Bendersky933f6992011-09-09 08:11:06 +0300595 options, args = optparser.parse_args()
596
Eli Bendersky40eb1702011-09-16 16:59:52 +0300597 if options.help or len(args) == 0:
598 optparser.print_help()
599 sys.exit(0)
600
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300601 if options.show_all_headers:
602 do_file_header = do_section_header = do_program_header = True
603 else:
604 do_file_header = options.show_file_header
605 do_section_header = options.show_section_header
606 do_program_header = options.show_program_header
607
Eli Bendersky933f6992011-09-09 08:11:06 +0300608 with open(args[0], 'rb') as file:
609 try:
610 readelf = ReadElf(file, sys.stdout)
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300611 if do_file_header:
612 readelf.display_file_header()
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300613 if do_section_header:
614 readelf.display_section_headers(
615 show_heading=not do_file_header)
Eli Bendersky2fc0f2a2011-09-17 10:39:29 +0300616 if do_program_header:
617 readelf.display_program_headers(
618 show_heading=not do_file_header)
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300619 if options.show_symbols:
620 readelf.display_symbol_tables()
Eli Bendersky84066b22011-09-20 06:48:52 +0300621 if options.show_relocs:
622 readelf.display_relocations()
Eli Benderskyc4a4c072011-09-17 15:28:28 +0300623 if options.show_hex_dump:
624 readelf.display_hex_dump(options.show_hex_dump)
Eli Bendersky53e86db2011-09-18 06:04:30 +0300625 if options.show_string_dump:
626 readelf.display_string_dump(options.show_string_dump)
eliben0fc47af2011-10-02 13:47:12 +0200627 if options.debug_dump_section:
628 readelf.display_debug_dump(options.debug_dump_section)
Eli Bendersky933f6992011-09-09 08:11:06 +0300629 except ELFError as ex:
Eli Bendersky0b27ba42011-09-17 06:44:02 +0300630 sys.stderr.write('ELF error: %s\n' % ex)
Eli Bendersky933f6992011-09-09 08:11:06 +0300631 sys.exit(1)
632
633
634#-------------------------------------------------------------------------------
635if __name__ == '__main__':
636 main()
637