From: Eli Bendersky Date: Fri, 23 Sep 2011 12:23:41 +0000 (+0300) Subject: * added another test file (real-life object file borrowed from libelf) X-Git-Tag: v0.10~94 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=adf707a0a76d7e0b54c447ed41afc12ae7d21e52;p=pyelftools.git * added another test file (real-life object file borrowed from libelf) * print out notification about sections having relocations against them in the file * munge run_tests to ignore case when comparing lines --- diff --git a/elftools/elf/sections.py b/elftools/elf/sections.py index 10b99f9..d545ec2 100644 --- a/elftools/elf/sections.py +++ b/elftools/elf/sections.py @@ -39,6 +39,9 @@ class Section(object): """ return self.header[name] + def __eq__(self, other): + return self.header == other.header + class NullSection(Section): """ ELF NULL section diff --git a/scripts/readelf.py b/scripts/readelf.py index 8f1c27f..d90b5bf 100755 --- a/scripts/readelf.py +++ b/scripts/readelf.py @@ -302,12 +302,19 @@ class ReadElf(object): continue symbol = symtable.get_symbol(rel['r_info_sym']) + # Some symbols have zero 'st_name', so instead what's used is + # the name of the section they point at + if symbol['st_name'] == 0: + symsec = self.elffile.get_section(symbol['st_shndx']) + symbol_name = symsec.name + else: + symbol_name = symbol.name self._emit(' %s %s%s' % ( self._format_hex( symbol['st_value'], fullhex=True, lead0x=False), ' ' if self.elffile.elfclass == 32 else '', - symbol.name)) + symbol_name)) if section.is_RELA(): self._emit(' %s %x' % ( '+' if rel['r_addend'] >= 0 else '-', @@ -328,7 +335,7 @@ class ReadElf(object): return self._emitline("\nHex dump of section '%s':" % section.name) - + self._note_relocs_for_section(section) addr = section['sh_addr'] data = section.data() dataptr = 0 @@ -438,6 +445,17 @@ class ReadElf(object): # Not a number. Must be a name then return self.elffile.get_section_by_name(spec) + def _note_relocs_for_section(self, section): + """ If there are relocation sections pointing to the givne section, + emit a note about it. + """ + for relsec in self.elffile.iter_sections(): + if isinstance(relsec, RelocationSection): + info_idx = relsec['sh_info'] + if self.elffile.get_section(info_idx) == section: + self._emitline(' Note: This section has relocations against it, but these have NOT been applied to this dump.') + return + def _emit(self, s=''): """ Emit an object to output """ diff --git a/tests/run_tests.py b/tests/run_tests.py index fda2371..b0b24a0 100755 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -84,10 +84,12 @@ def compare_output(s1, s2): Note: this function contains some rather horrible hacks to ignore differences which are not important for the verification of pyelftools. This is due to some intricacies of binutils's readelf which pyelftools - doesn't currently implement. Read the documentation for more details. + doesn't currently implement, or silly inconsistencies in the output of + readelf, which I was reluctant to replicate. + Read the documentation for more details. """ - lines1 = s1.splitlines() - lines2 = s2.splitlines() + lines1 = s1.lower().splitlines() + lines2 = s2.lower().splitlines() if len(lines1) != len(lines2): return False, 'Number of lines different: %s vs %s' % ( len(lines1), len(lines2)) diff --git a/tests/testfiles/update32.o.elf b/tests/testfiles/update32.o.elf new file mode 100644 index 0000000..ae68ef9 Binary files /dev/null and b/tests/testfiles/update32.o.elf differ