Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building of dbg binary fails when there are no section headers #29

Open
zwilcox opened this issue Jan 4, 2025 · 1 comment
Open

Building of dbg binary fails when there are no section headers #29

zwilcox opened this issue Jan 4, 2025 · 1 comment

Comments

@zwilcox
Copy link

zwilcox commented Jan 4, 2025

I'm getting the following error when the script tries to build the new dbg binary:

Traceback (most recent call last):
  File "/my/path/ghidra2dwarf/src/ghidra2dwarf.py", line 521, in <module>
    add_sections_to_elf(exe_path, out_path, sections)
  File "/my/path/ghidra2dwarf/src/elf.py", line 195, in add_sections_to_elf
    out = e.generate_updated_elf()
  File "/my/path/ghidra2dwarf/src/elf.py", line 155, in generate_updated_elf
    section_headers = self.extract_section_headers()
  File "/my/path/ghidra2dwarf/src/elf.py", line 140, in extract_section_headers
    self.section_names = self.extract_section(self.section_headers[h.shstrndx])
IndexError: index out of range: 0
ghidra2dwarf.py> Finished!

It appears this occurs when there are no section headers in the elf binary:

readelf -h /another/path/squashfs-root/bin/busybox
ELF Header:
  Magic:   7f 45 4c 46 01 02 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, big endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           MIPS R3000
  Version:                           0x1
  Entry point address:               0x403220
  Start of program headers:          52 (bytes into file)
  Start of section headers:          0 (bytes into file)
  Flags:                             0x70001007, noreorder, pic, cpic, o32, mips32r2
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         8
  Size of section headers:           0 (bytes)
  Number of section headers:         0
  Section header string table index: 0

Would it be reasonable to add the following to elf.py

if self.section_headers:
  self.section_names = self.extract_section(self.section_headers[h.shstrndx])
@zwilcox
Copy link
Author

zwilcox commented Jan 4, 2025

This seemed to fix it:

diff --git a/src/elf.py b/src/elf.py
index 63f21f7..e64bdde 100644
--- a/src/elf.py
+++ b/src/elf.py
@@ -92,6 +92,7 @@ class Elf:
        def __init__(self, bytes):
                self.bytes = bytearray(bytes)
                self.extract_ident()
+               self.section_names = bytearray()
                bits = '64' if self.ident.elf_class == ElfClass.ELFCLASS64 else '32'
                #bits = '64' if ElfClass[self.ident.elf_class] == ElfClass.ELFCLASS64 else '32'
                endianness = 'le' if self.ident.elf_data == ElfData.ELFDATA2LSB else 'be'
@@ -137,7 +138,10 @@ class Elf:
                h = self.extract_header()
                for i in range(h.shnum):
                        self.section_headers.append(self._dump_struct(ElfSectionHeader, h.shoff + i * h.shentsize))
-               self.section_names = self.extract_section(self.section_headers[h.shstrndx])
+
+               if self.section_headers:
+                       self.section_names = self.extract_section(self.section_headers[h.shstrndx])
+
Done.
ELF saved to /another/path//bin/busybox_dbg
C source saved to /another/path//bin/busybox_dbg.c
ghidra2dwarf.py> Finished!

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant