Skip to content

Commit

Permalink
make it work after being stripped
Browse files Browse the repository at this point in the history
  • Loading branch information
DennyDai committed Dec 7, 2024
1 parent 1c6775f commit 0cc18c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/patcherex2/components/binfmt_tools/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, p, binary_path: str) -> None:
self._file = open(binary_path, "rb")
self._elf = ELFFile(self._file)
self._segments = [segment.header for segment in self._elf.iter_segments()]
self._sections = [section.header for section in self._elf.iter_sections()]
self.file_updates = []

self.file_size = os.stat(self.binary_path).st_size
Expand Down
31 changes: 31 additions & 0 deletions src/patcherex2/targets/elf_arm_mimxrt1052.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def _init_memory_analysis(self):
* The flasher (LinkServer) seems only care about segment headers, so
we can safely ignore the section headers.
* The IDE will strip the binary then call the flasher, and strip will
remove the segment we added, so we need to add a fake corresponding
section to make sure the segment is not removed.
Type | Name | Alias | Location | Size
-------|---------------|-------|------------|----------
Flash | BOARD_FLASH | Flash | 0x60000000 | 0x4000000
Expand Down Expand Up @@ -81,6 +85,23 @@ def finalize(self):
)
)

self._sections.append(
Container(
**{
"sh_name": 0,
"sh_type": "SHT_PROGBITS",
"sh_flags": 2,
"sh_addr": block.mem_addr,
"sh_offset": block.file_addr,
"sh_size": block.size,
"sh_link": 0,
"sh_info": 0,
"sh_addralign": max_align,
"sh_entsize": 0,
}
)
)

# sort segments by p_offset
self._segments = sorted(self._segments, key=lambda x: x["p_offset"])

Expand Down Expand Up @@ -145,6 +166,16 @@ def finalize(self):
ehdr = self._elf.header
ehdr["e_phnum"] = len(self._segments)
ehdr["e_phoff"] = phdr_start

# generate new shdr at end of the file and update ehdr
shdr_start = phdr_start + len(new_phdr)
new_shdr = b""
for section in self._sections:
new_shdr += self._elf.structs.Elf_Shdr.build(section)
self.p.binfmt_tool.update_binary_content(shdr_start, new_shdr)

ehdr["e_shnum"] = len(self._sections)
ehdr["e_shoff"] = shdr_start
new_ehdr = self._elf.structs.Elf_Ehdr.build(ehdr)
self.p.binfmt_tool.update_binary_content(0, new_ehdr)

Expand Down

0 comments on commit 0cc18c4

Please sign in to comment.